diff --git a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/BaseRegistryManager.java b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/BaseRegistryManager.java index 3aa98e4ad..656f17eec 100644 --- a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/BaseRegistryManager.java +++ b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/BaseRegistryManager.java @@ -1,7 +1,9 @@ package it.pagopa.selfcare.onboarding.entity.registry; +import it.pagopa.selfcare.onboarding.common.PartyRole; import it.pagopa.selfcare.onboarding.entity.Onboarding; +import it.pagopa.selfcare.onboarding.entity.User; public abstract class BaseRegistryManager implements RegistryManager { @@ -11,6 +13,7 @@ public abstract class BaseRegistryManager implements RegistryManager { protected static final String ONBOARDING_NOT_ALLOWED_ERROR_MESSAGE_NOT_DELEGABLE = "Institution with external id '%s' is not allowed to onboard '%s' product because it is not delegable"; protected static final String PARENT_TAX_CODE_IS_INVALID = "The tax code of the parent entity of the request does not match the tax code of the parent entity retrieved by IPA"; protected static final String TAX_CODE_INVOICING_IS_INVALID = "The tax code invoicing of the request does not match any tax code of institutions' hierarchy"; + protected static final String PNPG_INSTITUTION_REGISTRY_NOT_FOUND = "Institution with taxCode %s is not into registry"; protected static final int DURATION_TIMEOUT = 5; protected static final int MAX_NUMBER_ATTEMPTS = 2; @@ -33,4 +36,12 @@ public RegistryManager setResource(T registryResource) { this.registryResource = registryResource; return this; } + + protected String getManagerIdFromOnboarding() { + return onboarding.getUsers().stream() + .filter(user -> user.getRole().equals(PartyRole.MANAGER)) + .map(User::getId) + .findFirst() + .orElse(null); + } } diff --git a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerADE.java b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerADE.java index 21c9337c2..577dc5523 100644 --- a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerADE.java +++ b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerADE.java @@ -3,13 +3,17 @@ import io.smallrye.mutiny.Uni; import it.pagopa.selfcare.onboarding.entity.Onboarding; import it.pagopa.selfcare.onboarding.entity.registry.client.ClientRegistryADE; +import it.pagopa.selfcare.onboarding.exception.InvalidRequestException; import it.pagopa.selfcare.product.entity.Product; import org.openapi.quarkus.party_registry_proxy_json.api.NationalRegistriesApi; import org.openapi.quarkus.user_registry_json.api.UserApi; +import java.util.Objects; + public class RegistryManagerADE extends ClientRegistryADE { - public RegistryManagerADE(Onboarding onboarding, NationalRegistriesApi nationalRegistriesApi, UserApi userApi) { + public RegistryManagerADE( + Onboarding onboarding, NationalRegistriesApi nationalRegistriesApi, UserApi userApi) { super(onboarding, nationalRegistriesApi, userApi); } @@ -20,7 +24,15 @@ public Uni customValidation(Product product) { @Override public Uni isValid() { + Boolean result = super.registryResource; + if (Objects.isNull(result) || !result) { + return Uni.createFrom() + .failure( + new InvalidRequestException( + String.format( + PNPG_INSTITUTION_REGISTRY_NOT_FOUND, + onboarding.getInstitution().getTaxCode()))); + } return Uni.createFrom().item(true); } - } diff --git a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerInfocamere.java b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerInfocamere.java index 1d9d4b8e4..20843908a 100644 --- a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerInfocamere.java +++ b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryManagerInfocamere.java @@ -3,13 +3,20 @@ import io.smallrye.mutiny.Uni; import it.pagopa.selfcare.onboarding.entity.Onboarding; import it.pagopa.selfcare.onboarding.entity.registry.client.ClientRegistryInfocamere; +import it.pagopa.selfcare.onboarding.exception.InvalidRequestException; import it.pagopa.selfcare.product.entity.Product; import org.openapi.quarkus.party_registry_proxy_json.api.InfocamereApi; +import org.openapi.quarkus.party_registry_proxy_json.model.BusinessResource; +import org.openapi.quarkus.user_registry_json.api.UserApi; + +import java.util.List; +import java.util.Objects; public class RegistryManagerInfocamere extends ClientRegistryInfocamere { - public RegistryManagerInfocamere(Onboarding onboarding, InfocamereApi infocamereApi) { - super(onboarding, infocamereApi); + public RegistryManagerInfocamere( + Onboarding onboarding, InfocamereApi infocamereApi, UserApi userApi) { + super(onboarding, infocamereApi, userApi); } @Override @@ -19,7 +26,26 @@ public Uni customValidation(Product product) { @Override public Uni isValid() { + List institutions = super.registryResource.getBusinesses(); + if (institutions.isEmpty() || Objects.isNull(findByTaxCode(institutions))) { + return Uni.createFrom() + .failure( + new InvalidRequestException( + String.format( + PNPG_INSTITUTION_REGISTRY_NOT_FOUND, + onboarding.getInstitution().getTaxCode()))); + } return Uni.createFrom().item(true); } - + + private BusinessResource findByTaxCode(List businessResources) { + return businessResources.stream() + .filter( + businessResource -> + businessResource + .getBusinessTaxId() + .equals(onboarding.getInstitution().getTaxCode())) + .findFirst() + .orElse(null); + } } diff --git a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryResourceFactory.java b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryResourceFactory.java index feb1cc6fe..8f91d7fc1 100644 --- a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryResourceFactory.java +++ b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/RegistryResourceFactory.java @@ -43,7 +43,7 @@ public RegistryManager create(Onboarding onboarding) { case PDND_INFOCAMERE -> new RegistryManagerPDNDInfocamere(onboarding, infocamerePdndApi); case ANAC -> new RegistryManagerANAC(onboarding, stationsApi); case IVASS -> new RegistryManagerIVASS(onboarding, insuranceCompaniesApi); - case INFOCAMERE -> new RegistryManagerInfocamere(onboarding, infocamereApi); + case INFOCAMERE -> new RegistryManagerInfocamere(onboarding, infocamereApi, userApi); case ADE -> new RegistryManagerADE(onboarding, nationalRegistriesApi, userApi); case IPA -> getResourceFromIPA(onboarding); default -> getRegistryManagerSELC(onboarding); diff --git a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryADE.java b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryADE.java index d7885c27e..1ef53de3a 100644 --- a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryADE.java +++ b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryADE.java @@ -3,9 +3,7 @@ import static it.pagopa.selfcare.onboarding.service.OnboardingServiceDefault.USERS_FIELD_LIST; import io.smallrye.mutiny.Uni; -import it.pagopa.selfcare.onboarding.common.PartyRole; import it.pagopa.selfcare.onboarding.entity.Onboarding; -import it.pagopa.selfcare.onboarding.entity.User; import it.pagopa.selfcare.onboarding.entity.registry.BaseRegistryManager; import it.pagopa.selfcare.onboarding.exception.ResourceNotFoundException; import jakarta.ws.rs.WebApplicationException; @@ -48,14 +46,8 @@ public Boolean retrieveInstitution() { } private String getManagerTaxCode() { - final String managerId = - onboarding.getUsers().stream() - .filter(user -> user.getRole().equals(PartyRole.MANAGER)) - .map(User::getId) - .findFirst() - .orElse(null); return userApi - .findByIdUsingGET(USERS_FIELD_LIST, managerId) + .findByIdUsingGET(USERS_FIELD_LIST, getManagerIdFromOnboarding()) .await() .atMost(Duration.of(DURATION_TIMEOUT, ChronoUnit.SECONDS)) .getFiscalCode(); diff --git a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryInfocamere.java b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryInfocamere.java index 3c1906cca..0ee80a5e1 100644 --- a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryInfocamere.java +++ b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/entity/registry/client/ClientRegistryInfocamere.java @@ -1,5 +1,6 @@ package it.pagopa.selfcare.onboarding.entity.registry.client; +import static it.pagopa.selfcare.onboarding.service.OnboardingServiceDefault.USERS_FIELD_LIST; import io.smallrye.mutiny.Uni; import it.pagopa.selfcare.onboarding.entity.Onboarding; @@ -12,14 +13,17 @@ 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.user_registry_json.api.UserApi; public abstract class ClientRegistryInfocamere extends BaseRegistryManager { private final InfocamereApi client; + private final UserApi userApi; - protected ClientRegistryInfocamere(Onboarding onboarding, InfocamereApi client) { + protected ClientRegistryInfocamere(Onboarding onboarding, InfocamereApi client, UserApi userApi) { super(onboarding); this.client = client; + this.userApi = userApi; } public BusinessesResource retrieveInstitution() { @@ -28,7 +32,7 @@ public BusinessesResource retrieveInstitution() { GetInstitutionsByLegalDto.builder() .filter( GetInstitutionsByLegalFilterDto.builder() - .legalTaxId(onboarding.getInstitution().getTaxCode()) + .legalTaxId(getManagerTaxCode()) .build()) .build()) .onFailure() @@ -42,10 +46,18 @@ public BusinessesResource retrieveInstitution() { .failure( new ResourceNotFoundException( String.format( - "Institution with taxCode %s not found", + "Institutions' list for user with taxCode %s not found", onboarding.getInstitution().getTaxCode()))) : Uni.createFrom().failure(ex)) .await() .atMost(Duration.of(DURATION_TIMEOUT, ChronoUnit.SECONDS)); } + + private String getManagerTaxCode() { + return userApi + .findByIdUsingGET(USERS_FIELD_LIST, getManagerIdFromOnboarding()) + .await() + .atMost(Duration.of(DURATION_TIMEOUT, ChronoUnit.SECONDS)) + .getFiscalCode(); + } }