Skip to content

Commit

Permalink
[SELC-6172] feat: removed DELETE_MANAGERS_BY_IC_AND_ADE activity
Browse files Browse the repository at this point in the history
  • Loading branch information
empassaro committed Dec 17, 2024
1 parent 9c6f42a commit 11d601d
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 370 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,21 +648,6 @@ public void createAggregatesCsv(
readOnboardingWorkflowValue(objectMapper, onboardingWorkflowString));
}

@FunctionName(DELETE_MANAGERS_BY_IC_AND_ADE)
public void deleteOldPgManagers(
@DurableActivityTrigger(name = "onboardingString") String onboardingString,
final ExecutionContext context) {
context
.getLogger()
.info(
() ->
String.format(
FORMAT_LOGGER_ONBOARDING_STRING,
DELETE_MANAGERS_BY_IC_AND_ADE,
onboardingString));
completionService.deleteOldPgManagers(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(RETRIEVE_AGGREGATES_ACTIVITY)
public String retrieveAggregates(
@DurableActivityTrigger(name = "onboardingString") String onboardingString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class ActivityName {
public static final String RESEND_NOTIFICATIONS_ACTIVITY = "ResendNotificationsActivity";
public static final String CREATE_AGGREGATES_CSV_ACTIVITY = "CreateAggregatesCsv";
public static final String EXISTS_DELEGATION_ACTIVITY = "ExistsDelegationActivity";
public static final String DELETE_MANAGERS_BY_IC_AND_ADE = "DeleteManagersByIcAndAde";
public static final String RETRIEVE_AGGREGATES_ACTIVITY = "RetrieveAggregates";
public static final String BUILD_ATTACHMENTS_SAVE_TOKENS_ACTIVITY = "BuildAttachmentAndSaveTokens";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public interface CompletionService {

String existsDelegation(OnboardingAggregateOrchestratorInput onboardingAggregateOrchestratorInput);

void deleteOldPgManagers(Onboarding onboarding);

List<DelegationResponse> retrieveAggregates(Onboarding onboarding);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@
import org.openapi.quarkus.party_registry_proxy_json.api.InfocamereApi;
import org.openapi.quarkus.party_registry_proxy_json.api.NationalRegistriesApi;
import org.openapi.quarkus.party_registry_proxy_json.api.UoApi;
import org.openapi.quarkus.party_registry_proxy_json.model.BusinessesResource;
import org.openapi.quarkus.party_registry_proxy_json.model.GetInstitutionsByLegalDto;
import org.openapi.quarkus.party_registry_proxy_json.model.GetInstitutionsByLegalFilterDto;
import org.openapi.quarkus.party_registry_proxy_json.model.LegalVerificationResult;
import org.openapi.quarkus.user_json.model.AddUserRoleDto;
import org.openapi.quarkus.user_json.model.OnboardedProductState;
import org.openapi.quarkus.user_json.model.UserInstitutionResponse;
import org.openapi.quarkus.user_registry_json.api.UserApi;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -401,112 +395,11 @@ private List<String> getDestinationMails(Onboarding onboarding) {
.collect(Collectors.toList());
}

@Override
public void deleteOldPgManagers(Onboarding onboarding) {
String institutionId = onboarding.getInstitution().getId();
String productId = onboarding.getProductId();
List<String> oldPgManagersUid = retrieveActiveManagersOnInstitution(institutionId, productId);
Origin origin = onboarding.getInstitution().getOrigin();

oldPgManagersUid.stream()
.map(uid -> new PgManagerInfo(uid, retrieveTaxCode(uid)))
.filter(pgManagerInfo -> !isActiveManagerOnRegistries(pgManagerInfo.getTaxCode(), onboarding.getInstitution().getTaxCode(), origin))
.forEach(pgManagerInfo -> deleteManagerFromProduct(pgManagerInfo.getUid(), institutionId, productId));
}

@Override
public List<DelegationResponse> retrieveAggregates(Onboarding onboarding) {
String institutionId = onboarding.getInstitution().getId();
String productId = onboarding.getProductId();
DelegationWithPaginationResponse delegations = delegationApi.getDelegationsUsingGET1(null, institutionId, productId, null, null, null, null, null);
return delegations.getDelegations();
}

private List<String> retrieveActiveManagersOnInstitution(String institutionId, String productId) {
List<UserInstitutionResponse> activeManagers = userInstitutionApi.retrieveUserInstitutions(
institutionId,
null,
List.of(productId),
List.of(MANAGER.name()),
List.of(OnboardedProductState.ACTIVE.name()),
null
);

if(Objects.isNull(activeManagers) || CollectionUtils.isEmpty(activeManagers)) {
return Collections.emptyList();
}

return activeManagers.stream()
.map(UserInstitutionResponse::getUserId)
.toList();
}

private String retrieveTaxCode(String uid) {
return userRegistryApi.findByIdUsingGET(USERS_FIELD_LIST, uid).getFiscalCode();
}

private boolean isActiveManagerOnRegistries(String userTaxCode, String institutionTaxCode, Origin origin) {
return switch (origin) {
case INFOCAMERE -> isActiveManagerOnInfocamereRegistry(userTaxCode, institutionTaxCode);
case ADE -> isActiveManagerOnAdeRegistry(userTaxCode, institutionTaxCode);
default -> throw new GenericOnboardingException("Origin not supported");
};
}

private boolean isActiveManagerOnInfocamereRegistry(String userTaxCode, String institutionTaxCode) {
BusinessesResource businessesResource = infocamereApi.institutionsByLegalTaxIdUsingPOST(toGetInstitutionByLegalDto(userTaxCode));
if(Objects.isNull(businessesResource) || CollectionUtils.isEmpty(businessesResource.getBusinesses())) {
return false;
}

return businessesResource.getBusinesses().stream()
.anyMatch(business -> institutionTaxCode.equals(business.getBusinessTaxId()));
}

private GetInstitutionsByLegalDto toGetInstitutionByLegalDto(String userTaxCode) {
GetInstitutionsByLegalDto getInstitutionsByLegalDto = new GetInstitutionsByLegalDto();
GetInstitutionsByLegalFilterDto getInstitutionsByLegalFilterDto = new GetInstitutionsByLegalFilterDto();
getInstitutionsByLegalFilterDto.setLegalTaxId(userTaxCode);
getInstitutionsByLegalDto.setFilter(getInstitutionsByLegalFilterDto);
return getInstitutionsByLegalDto;
}

private boolean isActiveManagerOnAdeRegistry(String userTaxCode, String institutionTaxCode) {
try {
LegalVerificationResult legalVerificationResult = nationalRegistriesApi.verifyLegalUsingGET(userTaxCode, institutionTaxCode);
return legalVerificationResult.getVerificationResult();
} catch (WebApplicationException e) {
// 400 status code means that the user is not a manager of the institution
if (e.getResponse().getStatus() == 400) {
return false;
}
throw new GenericOnboardingException(String.format("Error during verify legal %s", e.getMessage()));
}
}

private void deleteManagerFromProduct(String uid, String institutionId, String productId) {
try (Response response = userApi.deleteProducts(institutionId, productId, uid)) {
if (!SUCCESSFUL.equals(response.getStatusInfo().getFamily())) {
throw new GenericOnboardingException(String.format("Failed to delete user %s from product %s in institution %s", uid, productId, institutionId));
}
}
}

private static class PgManagerInfo {
private final String uid;
private final String taxCode;

public PgManagerInfo(String uid, String taxCode) {
this.uid = uid;
this.taxCode = taxCode;
}

public String getUid() {
return uid;
}

public String getTaxCode() {
return taxCode;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public Optional<OnboardingStatus> executeToBeValidatedState(TaskOrchestrationCon
@Override
public Optional<OnboardingStatus> executePendingState(TaskOrchestrationContext ctx, OnboardingWorkflow onboardingWorkflow) {
final String onboardingString = getOnboardingString(objectMapper(), onboardingWorkflow.getOnboarding());
ctx.callActivity(DELETE_MANAGERS_BY_IC_AND_ADE, onboardingString, optionsRetry(), String.class).await();
ctx.callActivity(CREATE_USERS_ACTIVITY, onboardingString, optionsRetry(), String.class).await();
ctx.callActivity(STORE_ONBOARDING_ACTIVATEDAT, onboardingString, optionsRetry(), String.class).await();
return Optional.of(COMPLETED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,10 @@ void usersPgOrchestrator_whenStatusPending() {
function.onboardingsOrchestrator(orchestrationContext, executionContext);

ArgumentCaptor<String> captorActivity = ArgumentCaptor.forClass(String.class);
verify(orchestrationContext, times(3))
verify(orchestrationContext, times(2))
.callActivity(captorActivity.capture(), any(), any(), any());
assertEquals(DELETE_MANAGERS_BY_IC_AND_ADE, captorActivity.getAllValues().get(0));
assertEquals(CREATE_USERS_ACTIVITY, captorActivity.getAllValues().get(1));
assertEquals(STORE_ONBOARDING_ACTIVATEDAT, captorActivity.getAllValues().get(2));
assertEquals(CREATE_USERS_ACTIVITY, captorActivity.getAllValues().get(0));
assertEquals(STORE_ONBOARDING_ACTIVATEDAT, captorActivity.getAllValues().get(1));
}

@Test
Expand Down Expand Up @@ -1077,18 +1076,6 @@ void sendTestEmail() {
verify(completionService, times(1)).sendTestEmail(executionContext);
}

@Test
void deleteOldPgManagers() {
final String onboardingString = "{\"onboardingId\":\"onboardingId\"}";

when(executionContext.getLogger()).thenReturn(Logger.getGlobal());
doNothing().when(completionService).deleteOldPgManagers(any());

function.deleteOldPgManagers(onboardingString, executionContext);

verify(completionService, times(1)).deleteOldPgManagers(any());
}

@Test
void retrieveAggregates() {
List<DelegationResponse> delegationResponseList = new ArrayList<>();
Expand Down
Loading

0 comments on commit 11d601d

Please sign in to comment.