diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java index 6569cf35..cd8d3712 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java @@ -10,6 +10,7 @@ import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_DEPLOYABLE_STATUS; import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_DEPLOYED_STATUS; import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_EXISTING_REFERENCED_ENTITY; +import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_VALID_REFERENCED_ENTITY; /** * Enumeration for application error codes and messages @@ -24,6 +25,8 @@ public enum AppErrorCodeEnum { BPMN_FILE_CANNOT_BE_DEPLOYED("ATMLM_4000004", "The referenced BPMN file can not be deployed", NOT_DEPLOYABLE_STATUS), BPMN_FUNCTION_TYPE_DIFFERENT_FROM_REQUESTED("ATMLM_4000005", "The referenced BPMN file has a function type different from the requested", INVALID_FUNCTION_TYPE), BPMN_CANNOT_BE_DELETED_FOR_STATUS("ATMLM_4000006", "The referenced BPMN file can not be deleted in the actual state", NOT_DELETABLE), + MULTIPLE_BPMN_FILE_FOR_SINGLE_CONFIGURATION("ATMLM_4000007", "Multiple BPMN file found for a single configuration", INTERNAL), + NO_BPMN_FOUND_FOR_CONFIGURATION("ATMLM_4000008", "No runnable BPMN found for configuration", NOT_VALID_REFERENCED_ENTITY), OBJECT_STORE_SAVE_FILE_ERROR("ATMLM_4000100", "Error on persisting file on Object Store ", INTERNAL); private final String errorCode; diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/repository/BpmnBankConfigRepository.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/repository/BpmnBankConfigRepository.java index 97387241..f424f804 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/repository/BpmnBankConfigRepository.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/repository/BpmnBankConfigRepository.java @@ -28,4 +28,15 @@ public Uni> findByAcquirerIdAndFunctionType(String acquirer params.put("functionType", functionType); return list("select b from BpmnBankConfig b where b.bpmnBankConfigPK.acquirerId = :acquirerId and b.functionType= :functionType", params); } + + public Uni> findByTriadAndFunctionType(String acquirerId, String branchId, String terminalId, FunctionTypeEnum functionType) { + Map params = new HashMap<>(); + params.put("acquirerId", acquirerId); + params.put("branchId", branchId); + params.put("terminalId", terminalId); + params.put("functionType", functionType); + return find("select b from BpmnBankConfig b " + + "where b.bpmnBankConfigPK.branchId= :branchId and b.bpmnBankConfigPK.acquirerId = :acquirerId and b.functionType= :functionType " + + "and b.bpmnBankConfigPK.terminalId= :terminalId", params).list(); + } } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/BpmnResource.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/BpmnResource.java index aa147d8c..a4a4db4d 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/BpmnResource.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/BpmnResource.java @@ -10,11 +10,13 @@ import it.gov.pagopa.atmlayer.service.model.entity.BpmnVersion; import it.gov.pagopa.atmlayer.service.model.entity.BpmnVersionPK; import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum; +import it.gov.pagopa.atmlayer.service.model.enumeration.BankConfigUtilityValues; import it.gov.pagopa.atmlayer.service.model.enumeration.FunctionTypeEnum; import it.gov.pagopa.atmlayer.service.model.enumeration.StatusEnum; import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException; import it.gov.pagopa.atmlayer.service.model.service.BpmnFileStorageService; import it.gov.pagopa.atmlayer.service.model.service.BpmnVersionService; +import it.gov.pagopa.atmlayer.service.model.service.impl.BpmnBankConfigService; import it.gov.pagopa.atmlayer.service.model.utils.BpmnDtoMapper; import it.gov.pagopa.atmlayer.service.model.utils.BpmnUtils; import it.gov.pagopa.atmlayer.service.model.validators.BpmnEntityValidator; @@ -39,6 +41,7 @@ import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -54,6 +57,10 @@ public class BpmnResource { @Inject BpmnVersionService bpmnVersionService; + + @Inject + BpmnBankConfigService bpmnBankConfigService; + @Inject BpmnEntityValidator bpmnEntityValidator; @Inject @@ -144,4 +151,33 @@ public Uni deployBPMN(@PathParam("uuid") UUID uuid, StatusEnum.DEPLOYED)); }); } + + @GET + @Path("/function/{functionType}/bank{acquirerId}/branch/{branchId}/terminal/{terminalId}") + @Produces(MediaType.APPLICATION_JSON) + public Uni> findBPMNByTriad(@PathParam("functionType") FunctionTypeEnum functionTypeEnum, + @PathParam("acquirerId") String acquirerId, + @PathParam("branchId") String branchId, + @PathParam("terminalId") String terminalId) { + return bpmnBankConfigService.findByConfigurationsAndFunction(acquirerId, branchId, terminalId, functionTypeEnum) + .onItem() + .transformToUni(x1 -> { + if (x1.isPresent()) { + return bpmnVersionService.findByPk(new BpmnVersionPK(x1.get().getBpmnBankConfigPK().getBpmnId(), x1.get().getBpmnBankConfigPK().getBpmnModelVersion())); + } + return bpmnBankConfigService.findByConfigurationsAndFunction(acquirerId, branchId, BankConfigUtilityValues.NULL_VALUE.getValue(), functionTypeEnum) + .onItem().transformToUni(x2 -> { + if (x2.isPresent()) { + return bpmnVersionService.findByPk(new BpmnVersionPK(x2.get().getBpmnBankConfigPK().getBpmnId(), x2.get().getBpmnBankConfigPK().getBpmnModelVersion())); + } + return bpmnBankConfigService.findByConfigurationsAndFunction(acquirerId, BankConfigUtilityValues.NULL_VALUE.getValue(), BankConfigUtilityValues.NULL_VALUE.getValue(), functionTypeEnum) + .onItem().transformToUni(Unchecked.function(x3 -> { + if (x3.isPresent()) { + return bpmnVersionService.findByPk(new BpmnVersionPK(x3.get().getBpmnBankConfigPK().getBpmnId(), x3.get().getBpmnBankConfigPK().getBpmnModelVersion())); + } + throw new AtmLayerException("No runnable BPMN found for selection", Response.Status.BAD_REQUEST, AppErrorCodeEnum.NO_BPMN_FOUND_FOR_CONFIGURATION); + })); + }); + }); + } } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnBankConfigService.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnBankConfigService.java index 4c2a110c..549b1c1d 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnBankConfigService.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/BpmnBankConfigService.java @@ -2,13 +2,18 @@ import io.quarkus.hibernate.reactive.panache.common.WithTransaction; import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.unchecked.Unchecked; import it.gov.pagopa.atmlayer.service.model.entity.BpmnBankConfig; +import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum; import it.gov.pagopa.atmlayer.service.model.enumeration.FunctionTypeEnum; +import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException; import it.gov.pagopa.atmlayer.service.model.repository.BpmnBankConfigRepository; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import jakarta.ws.rs.core.Response; import java.util.List; +import java.util.Optional; @ApplicationScoped public class BpmnBankConfigService { @@ -28,4 +33,15 @@ public Uni> findByAcquirerIdAndFunctionType(String acquirer public Uni deleteByAcquirerIdAndFunctionType(String acquirerId, FunctionTypeEnum functionTypeEnum) { return this.bankConfigRepository.deleteByAcquirerIdAndFunctionType(acquirerId, functionTypeEnum); } + + public Uni> findByConfigurationsAndFunction(String acquirerId, String branchId, String terminalId, FunctionTypeEnum functionTypeEnum) { + return this.bankConfigRepository.findByTriadAndFunctionType(acquirerId, branchId, terminalId, functionTypeEnum) + .onItem().transformToUni(Unchecked.function(x -> { + if (!x.isEmpty() && x.size() > 1) { + throw new AtmLayerException("Multiple BPMN found for a single configuration.", Response.Status.INTERNAL_SERVER_ERROR, AppErrorCodeEnum.ATMLM_500); + } + + return Uni.createFrom().item(x.isEmpty() ? Optional.empty() : Optional.ofNullable(x.get(0))); + })); + } }