diff --git a/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/DelegationEntity.java b/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/DelegationEntity.java index 38971fa89..4b14e6b21 100644 --- a/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/DelegationEntity.java +++ b/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/DelegationEntity.java @@ -25,6 +25,8 @@ public class DelegationEntity { private String to; private String toTaxCode; private String fromTaxCode; + private String toType; + private String fromType; private String productId; private DelegationType type; private DelegationState status; diff --git a/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/mapper/DelegationEntityMapper.java b/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/mapper/DelegationEntityMapper.java index 458041617..b762a794b 100644 --- a/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/mapper/DelegationEntityMapper.java +++ b/connector/dao/src/main/java/it/pagopa/selfcare/mscore/connector/dao/model/mapper/DelegationEntityMapper.java @@ -12,8 +12,12 @@ public interface DelegationEntityMapper { @Mapping(target = "id", defaultExpression = "java(UUID.randomUUID().toString())") + @Mapping(target = "toType", source = "brokerType") + @Mapping(target = "fromType", source = "institutionType") DelegationEntity convertToDelegationEntity(Delegation delegation); + @Mapping(target = "brokerType", source = "toType") + @Mapping(target = "institutionType", source = "fromType") Delegation convertToDelegation(DelegationEntity entity); } diff --git a/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java b/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java index 734e96eac..3b5687a5e 100644 --- a/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java +++ b/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java @@ -100,6 +100,7 @@ private void setPartnerByInstitutionTaxCode(Delegation delegation) { INSTITUTION_TAX_CODE_NOT_FOUND.getCode()) )); delegation.setToTaxCode(partner.getTaxCode()); + delegation.setBrokerType(partner.getInstitutionType()); delegation.setTo(partner.getId()); /* @@ -107,6 +108,7 @@ private void setPartnerByInstitutionTaxCode(Delegation delegation) { */ Institution institutionFrom = institutionService.retrieveInstitutionById(delegation.getFrom()); delegation.setFromTaxCode(institutionFrom.getTaxCode()); + delegation.setInstitutionType(institutionFrom.getInstitutionType()); } private void setTaxCodesByInstitutionIds(Delegation delegation){ @@ -117,7 +119,10 @@ private void setTaxCodesByInstitutionIds(Delegation delegation){ Institution institutionFrom = institutionService.retrieveInstitutionById(delegation.getFrom()); delegation.setToTaxCode(institutionTo.getTaxCode()); + delegation.setBrokerType(institutionTo.getInstitutionType()); + delegation.setFromTaxCode(institutionFrom.getTaxCode()); + delegation.setInstitutionType(institutionFrom.getInstitutionType()); } @Override @@ -129,13 +134,13 @@ public Delegation createDelegationFromInstitutionsTaxCode(Delegation delegation) If we call getInstitutions by empty subunitCode parameter, we have to filter retrieved list for institution with blank subunitCode field, otherwise we take first element returned by api. */ - String partnerIdentifier = institutionsTo.stream() + Institution partner = institutionsTo.stream() .filter(institution -> StringUtils.hasText(delegation.getToSubunitCode()) || !StringUtils.hasText(institution.getSubunitCode())) .findFirst() - .map(Institution::getId) .orElseThrow(() -> new ResourceNotFoundException(String.format(INSTITUTION_TAX_CODE_NOT_FOUND.getMessage(), delegation.getTo()), INSTITUTION_TAX_CODE_NOT_FOUND.getCode())); - delegation.setTo(partnerIdentifier); + delegation.setTo(partner.getId()); + delegation.setBrokerType(partner.getInstitutionType()); // TODO: remove filter when getInstitutions API will be fixed. /* @@ -143,13 +148,13 @@ public Delegation createDelegationFromInstitutionsTaxCode(Delegation delegation) with blank subunitCode field, otherwise we take first element returned by api. */ List institutionsFrom = institutionService.getInstitutions(delegation.getFromTaxCode(), delegation.getFromSubunitCode()); - String from = institutionsFrom.stream() + Institution from = institutionsFrom.stream() .filter(institution -> StringUtils.hasText(delegation.getFromSubunitCode()) || !StringUtils.hasText(institution.getSubunitCode())) .findFirst() - .map(Institution::getId) .orElseThrow(() -> new ResourceNotFoundException(String.format(INSTITUTION_TAX_CODE_NOT_FOUND.getMessage(), delegation.getTo()), INSTITUTION_TAX_CODE_NOT_FOUND.getCode())); - delegation.setFrom(from); + delegation.setFrom(from.getId()); + delegation.setInstitutionType(from.getInstitutionType()); return checkIfExistsAndSaveDelegation(delegation); } diff --git a/core/src/test/java/it/pagopa/selfcare/mscore/core/DelegationServiceImplTest.java b/core/src/test/java/it/pagopa/selfcare/mscore/core/DelegationServiceImplTest.java index beffb5d04..91191ed16 100644 --- a/core/src/test/java/it/pagopa/selfcare/mscore/core/DelegationServiceImplTest.java +++ b/core/src/test/java/it/pagopa/selfcare/mscore/core/DelegationServiceImplTest.java @@ -74,9 +74,11 @@ void testCreateDelegationWithProductProdIo() { Institution institutionTo = new Institution(); institutionTo.setId("idTo"); institutionTo.setTaxCode("taxCodeTo"); + institutionTo.setInstitutionType(InstitutionType.PA); Institution institutionFrom = new Institution(); institutionTo.setId("idFrom"); institutionTo.setTaxCode("taxCodeFrom"); + institutionTo.setInstitutionType(InstitutionType.PT); when(delegationConnector.save(dummyDelegationProdIo)).thenAnswer(arg ->arg.getArguments()[0]); when(institutionService.retrieveInstitutionById(dummyDelegationProdIo.getFrom())).thenReturn(institutionFrom); when(institutionService.retrieveInstitutionById(dummyDelegationProdIo.getTo())).thenReturn(institutionTo); @@ -89,6 +91,8 @@ void testCreateDelegationWithProductProdIo() { assertEquals(dummyDelegationProdIo.getId(), response.getId()); assertEquals(institutionTo.getTaxCode(), response.getToTaxCode()); assertEquals(institutionFrom.getTaxCode(), response.getFromTaxCode()); + assertEquals(institutionTo.getInstitutionType(), response.getBrokerType()); + assertEquals(institutionFrom.getInstitutionType(), response.getInstitutionType()); } /** @@ -128,7 +132,7 @@ void testCreateDelegationForEcWithProductPagopa() { Institution institutionFrom = new Institution(); institutionFrom.setId("from"); institutionFrom.setTaxCode("taxCodeFrom"); - when(delegationConnector.findAndActivate(institutionFrom.getId(), institutionTo.getId(), dummyDelegationProdPagopa.getProductId())).thenReturn(dummyDelegationProdPagopa);; + when(delegationConnector.findAndActivate(institutionFrom.getId(), institutionTo.getId(), dummyDelegationProdPagopa.getProductId())).thenReturn(dummyDelegationProdPagopa); doNothing().when(mailNotificationService).sendMailForDelegation(any(), any(), any()); when(institutionService.getInstitutions(dummyDelegationProdPagopa.getTo(), null)).thenReturn(List.of(institutionTo)); doNothing().when(institutionService).updateInstitutionDelegation(any(),anyBoolean()); @@ -312,6 +316,8 @@ void testCreateDelegationFromTaxCode() { assertEquals(dummyDelegationTaxCode.getId(), response.getId()); assertEquals(institutionTo.getTaxCode(), response.getToTaxCode()); assertEquals(institutionFrom.getTaxCode(), response.getFromTaxCode()); + assertEquals(institutionTo.getInstitutionType(), response.getInstitutionType()); + assertEquals(institutionFrom.getInstitutionType(), response.getBrokerType()); } @Test diff --git a/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationController.java b/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationController.java index bd64387f3..6afa7b4ad 100644 --- a/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationController.java +++ b/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationController.java @@ -111,7 +111,7 @@ public ResponseEntity> getDelegations(@ApiParam("${swag throw new InvalidRequestException("institutionId or brokerId must not be null!!", GenericError.GENERIC_ERROR.getCode()); return ResponseEntity.status(HttpStatus.OK).body(delegationService.getDelegations(institutionId, brokerId, productId, search, taxCode, order, page, size).stream() - .map(delegation -> delegationMapper.toDelegationResponseGet(delegation, brokerId)) + .map(delegationMapper::toDelegationResponseGet) .toList()); } diff --git a/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationV2Controller.java b/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationV2Controller.java index a85b41a8d..49804d5a1 100644 --- a/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationV2Controller.java +++ b/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/DelegationV2Controller.java @@ -86,7 +86,7 @@ public ResponseEntity getDelegations(@ApiParam DelegationWithPaginationResponse response = new DelegationWithPaginationResponse( delegationWithPagination.getDelegations().stream().map( - delegation -> delegationMapper.toDelegationResponseGet(delegation, brokerId)) + delegationMapper::toDelegationResponseGet) .toList(), delegationWithPagination.getPageInfo()); return ResponseEntity.status(HttpStatus.OK).body(response); diff --git a/web/src/main/java/it/pagopa/selfcare/mscore/web/model/mapper/DelegationMapper.java b/web/src/main/java/it/pagopa/selfcare/mscore/web/model/mapper/DelegationMapper.java index 72a12fc15..2754ba250 100644 --- a/web/src/main/java/it/pagopa/selfcare/mscore/web/model/mapper/DelegationMapper.java +++ b/web/src/main/java/it/pagopa/selfcare/mscore/web/model/mapper/DelegationMapper.java @@ -7,8 +7,6 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; -import java.util.Objects; - @Mapper(componentModel = "spring") public interface DelegationMapper { @@ -23,16 +21,13 @@ public interface DelegationMapper { @Mapping(source = "institutionFromRootName", target = "institutionRootName") DelegationResponse toDelegationResponse(Delegation delegation); - @Mapping(source = "delegation.from", target = "institutionId") - @Mapping(source = "delegation.to", target = "brokerId") - @Mapping(expression = "java(getTaxCodeValue(delegation, to))", target = "taxCode") - @Mapping(source = "delegation.institutionFromName", target = "institutionName") - @Mapping(source = "delegation.institutionToName", target = "brokerName") - @Mapping(source = "delegation.institutionFromRootName", target = "institutionRootName") - DelegationResponse toDelegationResponseGet(Delegation delegation, String to); - - default String getTaxCodeValue(Delegation delegation, String to) { - return Objects.nonNull(to) ? delegation.getFromTaxCode() : delegation.getToTaxCode(); - } + @Mapping(source = "from", target = "institutionId") + @Mapping(source = "to", target = "brokerId") + @Mapping(source = "toTaxCode", target = "brokerTaxCode") + @Mapping(source = "fromTaxCode", target = "taxCode") + @Mapping(source = "institutionFromName", target = "institutionName") + @Mapping(source = "institutionToName", target = "brokerName") + @Mapping(source = "institutionFromRootName", target = "institutionRootName") + DelegationResponse toDelegationResponseGet(Delegation delegation); } \ No newline at end of file