Skip to content

Commit

Permalink
Merge pull request #51 from companieshouse/IDVA6-494-merge
Browse files Browse the repository at this point in the history
Idva6 494 merge
  • Loading branch information
kkonuganti-ch authored Mar 20, 2024
2 parents aaf4ddd + ad91c33 commit 5817ce5
Show file tree
Hide file tree
Showing 7 changed files with 1,841 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package uk.gov.companieshouse.accounts.association.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.companieshouse.accounts.association.AccountsAssociationServiceApplication;
import uk.gov.companieshouse.accounts.association.exceptions.BadRequestRuntimeException;
import uk.gov.companieshouse.accounts.association.service.AssociationsService;
import uk.gov.companieshouse.accounts.association.service.CompanyService;
import uk.gov.companieshouse.api.accounts.associations.api.AssociationsListForCompanyInterface;
import uk.gov.companieshouse.api.accounts.associations.model.AssociationsList;
import uk.gov.companieshouse.logging.Logger;
import uk.gov.companieshouse.logging.LoggerFactory;

@RestController
public class AssociationsListForCompanyController implements AssociationsListForCompanyInterface {

private final AssociationsService associationsService;
private final CompanyService companyService;

private static final Logger LOG = LoggerFactory.getLogger( AccountsAssociationServiceApplication.applicationNameSpace );

public AssociationsListForCompanyController(AssociationsService associationsService, CompanyService companyService) {
this.associationsService = associationsService;
this.companyService = companyService;
}

@Override
public ResponseEntity<AssociationsList> getAssociationsForCompany( final String companyNumber, final String xRequestId, final Boolean includeRemoved, final Integer pageIndex, final Integer itemsPerPage ) {

LOG.info( String.format( "%s: Attempting to fetch users that are associated with company %s. includeRemoved=%b, itemsPerPage=%d, and pageIndex=%d.", xRequestId, companyNumber, includeRemoved, itemsPerPage, pageIndex ) );

if ( pageIndex < 0 ){
LOG.error( "pageIndex was less then 0" );
throw new BadRequestRuntimeException( "Please check the request and try again" );
}

if ( itemsPerPage <= 0 ){
LOG.error( "itemsPerPage was less then 0" );
throw new BadRequestRuntimeException( "Please check the request and try again" );
}

final var companyProfile = companyService.fetchCompanyProfile( companyNumber );

final var associationsList = associationsService.fetchAssociatedUsers( companyNumber, companyProfile, includeRemoved, itemsPerPage, pageIndex );
final var associationsListIsEmpty = associationsList.getItems().isEmpty();

LOG.info( associationsListIsEmpty ? String.format( "%s: Could not find any associations", xRequestId ) : String.format( "%s: Successfully fetched associations", xRequestId ) );

return new ResponseEntity<>( associationsList, HttpStatus.OK );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public ResponseEntity<AssociationsList> fetchAssociationsBy(

LOG.infoContext(xRequestId, "Trying to fetch associations data for user in session :".concat(ericIdentity), null);

if ( pageIndex < 0 ){
LOG.error( "pageIndex was less then 0" );
throw new BadRequestRuntimeException( "Please check the request and try again" );
}

if ( itemsPerPage <= 0 ){
LOG.error( "itemsPerPage was less then 0" );
throw new BadRequestRuntimeException( "Please check the request and try again" );
}

final var user = usersService.fetchUserDetails(ericIdentity);
Optional.ofNullable(user).orElseThrow(() -> new BadRequestRuntimeException("Eric id is not valid"));//NOSONAR or else throw will be caught by controller advice
final AssociationsList associationsList = associationsService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package uk.gov.companieshouse.accounts.association.service;

import jakarta.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import uk.gov.companieshouse.accounts.association.mapper.AssociationMapper;
Expand All @@ -12,13 +15,15 @@
import uk.gov.companieshouse.accounts.association.models.AssociationDao;
import uk.gov.companieshouse.accounts.association.repositories.AssociationsRepository;
import uk.gov.companieshouse.api.accounts.associations.model.Association;
import uk.gov.companieshouse.api.accounts.associations.model.Association.StatusEnum;
import uk.gov.companieshouse.api.accounts.associations.model.AssociationsList;
import uk.gov.companieshouse.api.accounts.user.model.User;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import uk.gov.companieshouse.api.company.CompanyDetails;

@Service
public class AssociationsService {
Expand Down Expand Up @@ -62,5 +67,16 @@ public AssociationsList fetchAssociationsForUserStatusAndCompany(
return associationsListUserMapper.daoToDto(results, user);
}

public AssociationsList fetchAssociatedUsers( final String companyNumber, final CompanyDetails companyDetails, final boolean includeRemoved, final int itemsPerPage, final int pageIndex ){
final Pageable pageable = PageRequest.of( pageIndex, itemsPerPage );

final var statuses = new HashSet<>( Set.of( StatusEnum.CONFIRMED.getValue(), StatusEnum.AWAITING_APPROVAL.getValue() ) );
if ( includeRemoved )
statuses.add( StatusEnum.REMOVED.getValue() );

final var associations = associationsRepository.fetchAssociatedUsers( companyNumber, statuses, pageable );

return associationsListCompanyMapper.daoToDto( associations, companyDetails );
}

}
Loading

0 comments on commit 5817ce5

Please sign in to comment.