Skip to content

Commit

Permalink
[PNPG-218] feat: added logic to remove institutions already onboarded…
Browse files Browse the repository at this point in the history
… in getInstitutionsFromInfocamere (#351)
  • Loading branch information
empassaro authored Sep 30, 2024
1 parent c1e0f1f commit 7af1bc5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import it.pagopa.selfcare.onboarding.connector.model.InstitutionOnboardingData;
import it.pagopa.selfcare.onboarding.connector.model.RecipientCodeStatusResult;
import it.pagopa.selfcare.onboarding.connector.model.institutions.*;
import it.pagopa.selfcare.onboarding.connector.model.institutions.infocamere.BusinessInfoIC;
import it.pagopa.selfcare.onboarding.connector.model.institutions.infocamere.InstitutionInfoIC;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.GeographicTaxonomy;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.InstitutionLocation;
Expand Down Expand Up @@ -70,6 +71,7 @@ class InstitutionServiceImpl implements InstitutionService {
private static final String REQUIRED_AGGREGATE_INSTITUTIONS = "Aggregate institutions are required if given institution is an Aggregator";

private static final String ONBOARDING_COMPANY_NOT_ALLOWED = "The selected business does not belong to the user";
private final static String PROD_PNPG = "prod-pnpg";
static final String DESCRIPTION_TO_REPLACE_REGEX = " - COMUNE";
private final OnboardingMsConnector onboardingMsConnector;
private final PartyConnector partyConnector;
Expand All @@ -79,7 +81,6 @@ class InstitutionServiceImpl implements InstitutionService {
private final OnboardingValidationStrategy onboardingValidationStrategy;
private final PartyRegistryProxyConnector partyRegistryProxyConnector;
private final InstitutionInfoMapper institutionMapper;

@Autowired
InstitutionServiceImpl(OnboardingMsConnector onboardingMsConnector, PartyConnector partyConnector,
ProductsConnector productsConnector,
Expand Down Expand Up @@ -479,12 +480,49 @@ private void validateOnboarding(String externalInstitutionId, String productId)
public InstitutionInfoIC getInstitutionsByUser(String fiscalCode) {
log.trace("getInstitutionsByUserId start");
log.debug(LogUtils.CONFIDENTIAL_MARKER, "getInstitutionsByUserId user = {}", fiscalCode);

InstitutionInfoIC result = partyRegistryProxyConnector.getInstitutionsByUserFiscalCode(fiscalCode);
log.debug("found {} institutions for the user", result.getBusinesses().size());

List<BusinessInfoIC> institutionsNotOnboardedByUser = result.getBusinesses().stream()
.filter(businessInfoIC -> !isOnboardedByUser(businessInfoIC, fiscalCode))
.toList();

result.setBusinesses(institutionsNotOnboardedByUser);
log.debug(LogUtils.CONFIDENTIAL_MARKER, "getInstitutionsByUserId result = {}", result);
log.trace("getInstitutionsByUserId end");
return result;
}

private boolean isOnboardedByUser(BusinessInfoIC businessInfoIC, String fiscalCode) {
log.debug(LogUtils.CONFIDENTIAL_MARKER, "Checking if business with tax code {} is onboarded by user with fiscal code {}",
businessInfoIC.getBusinessTaxId(), fiscalCode);
try {
onboardingMsConnector.verifyOnboarding(PROD_PNPG, businessInfoIC.getBusinessTaxId(), null, null, null);
log.debug("Business with tax code {} is already onboarded, checking if user with fiscal code {} is manager",
businessInfoIC.getBusinessTaxId(), fiscalCode);

boolean isManager = onboardingMsConnector.checkManager(getOnboardingData(fiscalCode, businessInfoIC));
log.debug(LogUtils.CONFIDENTIAL_MARKER, "User with fiscal code {} is manager of business with tax code {}",
fiscalCode, businessInfoIC.getBusinessTaxId());
return isManager;
} catch (ResourceNotFoundException e) {
log.debug("Business with tax code {} is not onboarded", businessInfoIC.getBusinessTaxId());
return false;
}
}

private OnboardingData getOnboardingData(String fiscalCode, BusinessInfoIC businessInfoIC) {
OnboardingData onboardingData = new OnboardingData();
User user = new User();
user.setTaxCode(fiscalCode);
user.setRole(PartyRole.MANAGER);
onboardingData.setUsers(List.of(user));
onboardingData.setTaxCode(businessInfoIC.getBusinessTaxId());
onboardingData.setProductId(PROD_PNPG);
return onboardingData;
}

@Override
public List<Institution> getByFilters(String productId, String taxCode, String origin, String originId, String subunitCode) {
log.trace("getByFilters start");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ void verifyOnboardingInfo_allowed() {
}

@Test
void getInstitutionsByUser_default() {
void getInstitutionsByUser_default_notOnboarded() {
//given
String taxCode = "setTaxCode";
SaveUserDto saveUserDto = mockInstance(new SaveUserDto(), "setFiscalCode");
Expand All @@ -1289,6 +1289,38 @@ void getInstitutionsByUser_default() {
institutionInfoICmock.setBusinesses(businessInfoICSmock);
when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(anyString()))
.thenReturn(institutionInfoICmock);
doThrow(new ResourceNotFoundException()).when(onboardingMsConnector)
.verifyOnboarding(any(), any(), any(), any(), any());
//when
InstitutionInfoIC result = institutionService.getInstitutionsByUser(user.getTaxCode());
//then
assertNotNull(result);
assertEquals(institutionInfoICmock.getBusinesses().get(0).getBusinessName(), result.getBusinesses().get(0).getBusinessName());
assertEquals(institutionInfoICmock.getBusinesses().get(0).getBusinessTaxId(), result.getBusinesses().get(0).getBusinessTaxId());
assertEquals(institutionInfoICmock.getLegalTaxId(), result.getLegalTaxId());
assertEquals(institutionInfoICmock.getRequestDateTime(), result.getRequestDateTime());
verify(partyRegistryProxyConnectorMock, times(1))
.getInstitutionsByUserFiscalCode(taxCode);
verifyNoMoreInteractions(partyRegistryProxyConnectorMock);
verifyNoMoreInteractions(userConnectorMock);
}

@Test
void getInstitutionsByUser_default_onboarded() {
//given
String taxCode = "setTaxCode";
SaveUserDto saveUserDto = mockInstance(new SaveUserDto(), "setFiscalCode");
saveUserDto.setFiscalCode(taxCode);
UserId userId = mockInstance(new UserId());
User user = mockInstance(new User(), "setId");
user.setId(userId.toString());
List<BusinessInfoIC> businessInfoICSmock = List.of(mockInstance(new BusinessInfoIC()));
InstitutionInfoIC institutionInfoICmock = mockInstance(new InstitutionInfoIC(), "setBusinesses");
institutionInfoICmock.setBusinesses(businessInfoICSmock);
when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(anyString()))
.thenReturn(institutionInfoICmock);
doNothing().when(onboardingMsConnector).verifyOnboarding(any(), any(), any(), any(), any());
when(onboardingMsConnector.checkManager(any())).thenReturn(false);
//when
InstitutionInfoIC result = institutionService.getInstitutionsByUser(user.getTaxCode());
//then
Expand Down

0 comments on commit 7af1bc5

Please sign in to comment.