Skip to content

Commit

Permalink
[SELC-6247] feat: turned API verify-manager into GET and updated serv…
Browse files Browse the repository at this point in the history
…ice code to use common class to perform verification
  • Loading branch information
empassaro committed Dec 19, 2024
1 parent b8f8abc commit 5e8e92c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 204 deletions.
55 changes: 20 additions & 35 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1279,21 +1279,30 @@
} ]
}
},
"/v2/institutions/company/verify-manager" : {
"post" : {
"/v2/institutions/company/{companyTaxCode}/verify-manager" : {
"get" : {
"tags" : [ "institutions" ],
"summary" : "verifyManager",
"description" : "The service allows to verify the legal representative on external registries and retrieve the company name",
"operationId" : "verifyManagerUsingPOST",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/VerifyManagerRequest"
}
}
"operationId" : "verifyManagerUsingGET",
"parameters" : [ {
"name" : "companyTaxCode",
"in" : "path",
"description" : "Institution's taxCode",
"required" : true,
"style" : "simple",
"schema" : {
"type" : "string"
}
},
}, {
"name" : "name",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
Expand Down Expand Up @@ -1335,16 +1344,6 @@
}
}
},
"409" : {
"description" : "Conflict",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"500" : {
"description" : "Internal Server Error",
"content" : {
Expand Down Expand Up @@ -4744,20 +4743,6 @@
}
}
},
"VerifyManagerRequest" : {
"title" : "VerifyManagerRequest",
"type" : "object",
"properties" : {
"companyTaxCode" : {
"type" : "string",
"description" : "Institution's taxCode"
},
"userTaxCode" : {
"type" : "string",
"description" : "User's fiscal code"
}
}
},
"VerifyManagerResponse" : {
"title" : "VerifyManagerResponse",
"type" : "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import it.pagopa.selfcare.onboarding.core.exception.UpdateNotAllowedException;
import it.pagopa.selfcare.onboarding.core.mapper.InstitutionInfoMapper;
import it.pagopa.selfcare.onboarding.core.strategy.OnboardingValidationStrategy;
import it.pagopa.selfcare.onboarding.core.utils.PgManagerVerifier;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductRoleInfo;
import it.pagopa.selfcare.product.entity.ProductStatus;
Expand Down Expand Up @@ -82,14 +83,16 @@ class InstitutionServiceImpl implements InstitutionService {
private final OnboardingValidationStrategy onboardingValidationStrategy;
private final PartyRegistryProxyConnector partyRegistryProxyConnector;
private final InstitutionInfoMapper institutionMapper;
private final PgManagerVerifier pgManagerVerifier;
@Autowired
InstitutionServiceImpl(OnboardingMsConnector onboardingMsConnector, PartyConnector partyConnector,
ProductsConnector productsConnector,
UserRegistryConnector userConnector,
OnboardingFunctionsConnector onboardingFunctionsConnector,
PartyRegistryProxyConnector partyRegistryProxyConnector,
OnboardingValidationStrategy onboardingValidationStrategy,
InstitutionInfoMapper institutionMapper
InstitutionInfoMapper institutionMapper,
PgManagerVerifier pgManagerVerifier
) {
this.onboardingMsConnector = onboardingMsConnector;
this.partyConnector = partyConnector;
Expand All @@ -99,6 +102,7 @@ class InstitutionServiceImpl implements InstitutionService {
this.userConnector = userConnector;
this.onboardingValidationStrategy = onboardingValidationStrategy;
this.institutionMapper = institutionMapper;
this.pgManagerVerifier = pgManagerVerifier;
}


Expand Down Expand Up @@ -628,29 +632,12 @@ public void onboardingUsersPgFromIcAndAde(OnboardingData onboardingData) {
public ManagerVerification verifyManager(String userTaxCode, String institutionTaxCode) {
log.trace("verifyManager start");

log.debug(LogUtils.CONFIDENTIAL_MARKER, "Checking if user with taxCode {} is manager of institution with taxCode {} on INFOCAMERE", Encode.forJava(userTaxCode), Encode.forJava(institutionTaxCode));
InstitutionInfoIC institutionInfoIC = partyRegistryProxyConnector.getInstitutionsByUserFiscalCode(userTaxCode);
if (Objects.nonNull(institutionInfoIC) && Objects.nonNull(institutionInfoIC.getBusinesses())){
for (BusinessInfoIC business : institutionInfoIC.getBusinesses()) {
if (institutionTaxCode.equals(business.getBusinessTaxId())) {
log.debug("User found as manager in INFOCAMERE for business with name = {}", business.getBusinessName());
return new ManagerVerification(Origin.INFOCAMERE.getValue(), business.getBusinessName());
}
}
ManagerVerification result = pgManagerVerifier.doVerify(userTaxCode, institutionTaxCode);
if(!result.isVerified()) {
throw new ResourceNotFoundException(String.format("User with userTaxCode %s is not the legal representative of the institution", userTaxCode));
}

try {
log.debug(LogUtils.CONFIDENTIAL_MARKER, "Checking if user with taxCode {} is manager of institution with taxCode {} on ADE", Encode.forJava(userTaxCode), Encode.forJava(institutionTaxCode));
MatchInfoResult matchInfoResult = partyRegistryProxyConnector.matchInstitutionAndUser(institutionTaxCode, userTaxCode);
if (Objects.nonNull(matchInfoResult) && matchInfoResult.isVerificationResult()) {
log.debug("User found as manager in ADE, response = {}", matchInfoResult);
return new ManagerVerification(Origin.ADE.getValue(), null);
}
} catch (InvalidRequestException e) {
throw new ResourceNotFoundException(String.format("User with taxCode %s is not the legal representative of the institution", userTaxCode));
}

throw new ResourceNotFoundException(String.format("User with userTaxCode %s is not the legal representative of the institution", userTaxCode));
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import it.pagopa.selfcare.onboarding.core.mapper.InstitutionInfoMapper;
import it.pagopa.selfcare.onboarding.core.mapper.InstitutionInfoMapperImpl;
import it.pagopa.selfcare.onboarding.core.strategy.OnboardingValidationStrategy;
import it.pagopa.selfcare.onboarding.core.utils.PgManagerVerifier;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductRole;
import it.pagopa.selfcare.product.entity.ProductRoleInfo;
Expand Down Expand Up @@ -84,6 +85,9 @@ class InstitutionServiceImplTest {
@Mock
private OnboardingValidationStrategy onboardingValidationStrategyMock;

@Mock
private PgManagerVerifier pgManagerVerifierMock;

@Captor
private ArgumentCaptor<OnboardingData> onboardingDataCaptor;

Expand Down Expand Up @@ -1004,124 +1008,39 @@ void shouldOnboardingProductInstitutionIpaUO() {
}

@Test
void verifyManager_userIsManagerOnInfocamere() {
void verifyManager_shouldReturnManagerVerificationWhenVerified() {
// given
String taxCode = "validTaxCode";
String companyTaxCode = "validCompanyTaxCode";
InstitutionInfoIC institutionInfoIC = new InstitutionInfoIC();
BusinessInfoIC businessInfoIC = new BusinessInfoIC();
businessInfoIC.setBusinessTaxId("otherCompanyTaxCode");
businessInfoIC.setBusinessName("CompanyName 2");
BusinessInfoIC businessInfoIC2 = new BusinessInfoIC();
businessInfoIC2.setBusinessTaxId(companyTaxCode);
businessInfoIC2.setBusinessName("CompanyName 1");
institutionInfoIC.setBusinesses(List.of(businessInfoIC, businessInfoIC2));
when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(taxCode)).thenReturn(institutionInfoIC);
String userTaxCode = "userTaxCode";
String institutionTaxCode = "institutionTaxCode";
ManagerVerification expectedVerification = new ManagerVerification();
expectedVerification.setVerified(true);
when(pgManagerVerifierMock.doVerify(userTaxCode, institutionTaxCode)).thenReturn(expectedVerification);

// when
ManagerVerification result = institutionService.verifyManager(taxCode, companyTaxCode);
ManagerVerification result = institutionService.verifyManager(userTaxCode, institutionTaxCode);

// then
assertNotNull(result);
assertEquals(Origin.INFOCAMERE.getValue(), result.getOrigin());
assertEquals("CompanyName 1", result.getCompanyName());
assertTrue(result.isVerified());
verify(pgManagerVerifierMock, times(1)).doVerify(userTaxCode, institutionTaxCode);
}

@Test
void verifyManager_userIsManagerOnAde() {
void verifyManager_shouldThrowResourceNotFoundExceptionWhenNotVerified() {
// given
String taxCode = "validTaxCode";
String companyTaxCode = "validCompanyTaxCode";
InstitutionInfoIC institutionInfoIC = new InstitutionInfoIC();
institutionInfoIC.setBusinesses(Collections.emptyList());
when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(taxCode)).thenReturn(institutionInfoIC);
MatchInfoResult matchInfoResult = new MatchInfoResult();
matchInfoResult.setVerificationResult(true);
when(partyRegistryProxyConnectorMock.matchInstitutionAndUser(companyTaxCode, taxCode)).thenReturn(matchInfoResult);
String userTaxCode = "userTaxCode";
String institutionTaxCode = "institutionTaxCode";
ManagerVerification expectedVerification = new ManagerVerification();
expectedVerification.setVerified(false);
when(pgManagerVerifierMock.doVerify(userTaxCode, institutionTaxCode)).thenReturn(expectedVerification);

// when
ManagerVerification result = institutionService.verifyManager(taxCode, companyTaxCode);

// then
assertNotNull(result);
assertEquals(Origin.ADE.getValue(), result.getOrigin());
}

@Test
void verifyManager_userAdeIsNull() {
// given
String taxCode = "validTaxCode";
String companyTaxCode = "validCompanyTaxCode";

//when
InstitutionInfoIC institutionInfoIC = new InstitutionInfoIC();
institutionInfoIC.setBusinesses(Collections.emptyList());
when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(taxCode)).thenReturn(institutionInfoIC);
when(partyRegistryProxyConnectorMock.matchInstitutionAndUser(companyTaxCode, taxCode)).thenReturn(null);

// then
assertThrows(ResourceNotFoundException.class, () -> institutionService.verifyManager(taxCode, companyTaxCode));
}

@Test
void verifyManager_userAdeIsFalse() {
// given
String taxCode = "validTaxCode";
String companyTaxCode = "validCompanyTaxCode";

//when
InstitutionInfoIC institutionInfoIC = new InstitutionInfoIC();
institutionInfoIC.setBusinesses(Collections.emptyList());
when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(taxCode)).thenReturn(institutionInfoIC);
MatchInfoResult matchInfoResult = new MatchInfoResult();
matchInfoResult.setVerificationResult(false);
when(partyRegistryProxyConnectorMock.matchInstitutionAndUser(companyTaxCode, taxCode)).thenReturn(matchInfoResult);
Executable executable = () -> institutionService.verifyManager(userTaxCode, institutionTaxCode);

// then
assertThrows(ResourceNotFoundException.class, () -> institutionService.verifyManager(taxCode, companyTaxCode));
}

@Test
void verifyManager_businessNull() {
// given
String taxCode = "validTaxCode";
String companyTaxCode = "validCompanyTaxCode";

when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(anyString())).thenReturn(null);
when(partyRegistryProxyConnectorMock.matchInstitutionAndUser(anyString(), anyString())).thenReturn(null);

// when & then
assertThrows(ResourceNotFoundException.class, () -> institutionService.verifyManager(taxCode, companyTaxCode));
}

@Test
void verifyManager_noBusinessFound() {
// given
String taxCode = "validTaxCode";
String companyTaxCode = "validCompanyTaxCode";
InstitutionInfoIC institutionInfoIC = new InstitutionInfoIC();
institutionInfoIC.setBusinesses(Collections.emptyList());

when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(anyString())).thenReturn(institutionInfoIC);
when(partyRegistryProxyConnectorMock.matchInstitutionAndUser(anyString(), anyString())).thenReturn(null);

// when & then
assertThrows(ResourceNotFoundException.class, () -> institutionService.verifyManager(taxCode, companyTaxCode));
}

@Test
void verifyManager_invalidRequestException() {
// given
String taxCode = "validTaxCode";
String companyTaxCode = "validCompanyTaxCode";
InstitutionInfoIC institutionInfoIC = new InstitutionInfoIC();
institutionInfoIC.setBusinesses(Collections.emptyList());

when(partyRegistryProxyConnectorMock.getInstitutionsByUserFiscalCode(anyString())).thenReturn(institutionInfoIC);
when(partyRegistryProxyConnectorMock.matchInstitutionAndUser(anyString(), anyString())).thenThrow(new InvalidRequestException("Invalid request"));

// when & then
assertThrows(ResourceNotFoundException.class, () -> institutionService.verifyManager(taxCode, companyTaxCode));
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, executable);
assertEquals("User with userTaxCode userTaxCode is not the legal representative of the institution", exception.getMessage());
verify(pgManagerVerifierMock, times(1)).doVerify(userTaxCode, institutionTaxCode);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,19 @@ public VerifyAggregatesResponse verifyAggregatesCsv(@RequestParam("aggregates")
return response;
}

@PostMapping(value = "/company/verify-manager")
@GetMapping(value = "/company/{companyTaxCode}/verify-manager")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "", notes = "${swagger.onboarding.institutions.api.onboarding.verifyManager}")
public VerifyManagerResponse verifyManager(@RequestBody @Valid VerifyManagerRequest request) {
public VerifyManagerResponse verifyManager(
@ApiParam("${swagger.onboarding.institutions.model.taxCode}")
@PathVariable("companyTaxCode") String companyTaxCode,
Principal principal
) {
log.trace("verifyManager start");
VerifyManagerResponse response = onboardingResourceMapper.toManagerVerification(institutionService.verifyManager(request.getUserTaxCode(), request.getCompanyTaxCode()));
JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) principal;
SelfCareUser selfCareUser = (SelfCareUser) jwtAuthenticationToken.getPrincipal();

VerifyManagerResponse response = onboardingResourceMapper.toManagerVerification(institutionService.verifyManager(selfCareUser.getFiscalCode(), companyTaxCode));
log.trace("verifyManager end");
return response;
}
Expand Down

This file was deleted.

Loading

0 comments on commit 5e8e92c

Please sign in to comment.