Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[SELC-4983] feat: add toType and fromType in delegation entity (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
giulia-tremolada authored May 28, 2024
1 parent f174047 commit 115831e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ private void setPartnerByInstitutionTaxCode(Delegation delegation) {
INSTITUTION_TAX_CODE_NOT_FOUND.getCode())
));
delegation.setToTaxCode(partner.getTaxCode());
delegation.setBrokerType(partner.getInstitutionType());
delegation.setTo(partner.getId());

/*
Retrieve delegator's institution to set taxCodeFrom
*/
Institution institutionFrom = institutionService.retrieveInstitutionById(delegation.getFrom());
delegation.setFromTaxCode(institutionFrom.getTaxCode());
delegation.setInstitutionType(institutionFrom.getInstitutionType());
}

private void setTaxCodesByInstitutionIds(Delegation delegation){
Expand All @@ -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
Expand All @@ -129,27 +134,27 @@ 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.
/*
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.
*/
List<Institution> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public ResponseEntity<List<DelegationResponse>> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ResponseEntity<DelegationWithPaginationResponse> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.Objects;

@Mapper(componentModel = "spring")
public interface DelegationMapper {

Expand All @@ -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);

}

0 comments on commit 115831e

Please sign in to comment.