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

[SELC-4908] feat: add toTaxCode and fromTaxCode in delegation #498

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class Delegation {
private String productId;
private InstitutionType institutionType;
private String taxCode;
private String toTaxCode;
private String fromTaxCode;
private InstitutionType brokerType;
private String brokerTaxCode;
private String fromSubunitCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class DelegationEntity {
private String institutionToName;
private String institutionFromRootName;
private String to;
private String toTaxCode;
private String fromTaxCode;
private String productId;
private DelegationType type;
private DelegationState status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public Delegation createDelegation(Delegation delegation) {
*/
if(PROD_PAGOPA.equals(delegation.getProductId())) {
setPartnerByInstitutionTaxCode(delegation);
} else {
setTaxCodesByInstitutionIds(delegation);
}

Delegation savedDelegation = checkIfExistsAndSaveDelegation(delegation);
Expand Down Expand Up @@ -88,22 +90,40 @@ private void setPartnerByInstitutionTaxCode(Delegation delegation) {
In case the api returns more institutions we always try to take the PT,
otherwise it is okay to take the first one
*/
List<Institution> institutions = institutionService.getInstitutions(delegation.getTo(), null);
Institution partner = institutions.stream()
List<Institution> institutionsTo = institutionService.getInstitutions(delegation.getTo(), null);
Institution partner = institutionsTo.stream()
.filter(institution -> institution.getInstitutionType() == InstitutionType.PT)
.findFirst()
.orElse(institutions.stream().findFirst()
.orElse(institutionsTo.stream().findFirst()
.orElseThrow(() -> new ResourceNotFoundException(
String.format(INSTITUTION_TAX_CODE_NOT_FOUND.getMessage(), delegation.getTo()),
INSTITUTION_TAX_CODE_NOT_FOUND.getCode())
));
delegation.setToTaxCode(partner.getTaxCode());
delegation.setTo(partner.getId());

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

private void setTaxCodesByInstitutionIds(Delegation delegation){
/*
Retrieve both delegator's and partner's institutions to set taxCodeFrom and taxCodeTo
*/
Institution institutionTo = institutionService.retrieveInstitutionById(delegation.getTo());
Institution institutionFrom = institutionService.retrieveInstitutionById(delegation.getFrom());

delegation.setToTaxCode(institutionTo.getTaxCode());
delegation.setFromTaxCode(institutionFrom.getTaxCode());
}

@Override
public Delegation createDelegationFromInstitutionsTaxCode(Delegation delegation) {

List<Institution> institutionsTo = institutionService.getInstitutions(delegation.getTo(), delegation.getToSubunitCode());
List<Institution> institutionsTo = institutionService.getInstitutions(delegation.getToTaxCode(), delegation.getToSubunitCode());
// 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
Expand All @@ -122,7 +142,7 @@ 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.
*/
List<Institution> institutionsFrom = institutionService.getInstitutions(delegation.getFrom(), delegation.getFromSubunitCode());
List<Institution> institutionsFrom = institutionService.getInstitutions(delegation.getFromTaxCode(), delegation.getFromSubunitCode());
String from = institutionsFrom.stream()
.filter(institution -> StringUtils.hasText(delegation.getFromSubunitCode()) || !StringUtils.hasText(institution.getSubunitCode()))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static it.pagopa.selfcare.mscore.constant.GenericError.CREATE_INSTITUTION_ERROR;

Expand Down Expand Up @@ -111,11 +110,6 @@ public List<Institution> getInstitutions(String taxCode, String subunitCode, Str

}

@Override
public List<Institution> getInstitutions(String taxCode, String subunitCode) {
return institutionConnector.findByTaxCodeAndSubunitCode(taxCode, subunitCode);
}

@Override
public Institution createInstitutionFromIpa(String taxCode, InstitutionPaSubunitType subunitType, String subunitCode, List<InstitutionGeographicTaxonomies> geographicTaxonomies, InstitutionType institutionType) {
CreateInstitutionStrategy institutionStrategy = createInstitutionStrategyFactory.createInstitutionStrategyIpa();
Expand All @@ -128,6 +122,11 @@ public Institution createInstitutionFromIpa(String taxCode, InstitutionPaSubunit
.build());
}

@Override
public List<Institution> getInstitutions(String taxCode, String subunitCode) {
return institutionConnector.findByTaxCodeAndSubunitCode(taxCode, subunitCode);
}

@Override
public Institution createInstitutionFromPda(Institution institution, String injectionInstitutionType) {
CreateInstitutionStrategy institutionStrategy = createInstitutionStrategyFactory.createInstitutionStrategyPda(injectionInstitutionType);
Expand Down Expand Up @@ -263,7 +262,7 @@ public List<Onboarding> retrieveInstitutionProducts(Institution institution, Lis
if (states != null && !states.isEmpty()) {
onboardingList = institution.getOnboarding().stream()
.filter(onboarding -> states.contains(onboarding.getStatus()))
.collect(Collectors.toList());
.toList();
} else {
onboardingList = institution.getOnboarding();
}
Expand All @@ -286,7 +285,7 @@ public List<GeographicTaxonomies> retrieveInstitutionGeoTaxonomies(Institution i
List<GeographicTaxonomies> geographicTaxonomies = institution.getGeographicTaxonomies().stream()
.map(institutionGeoTax -> retrieveGeoTaxonomies(institutionGeoTax.getCode())
.orElseThrow(() -> new MsCoreException(String.format(CustomError.GEO_TAXONOMY_CODE_NOT_FOUND.getMessage(), institutionGeoTax.getCode()), CustomError.GEO_TAXONOMY_CODE_NOT_FOUND.getCode())))
.collect(Collectors.toList());
.toList();
if (!geographicTaxonomies.isEmpty()) {
return geographicTaxonomies;
}
Expand Down Expand Up @@ -323,7 +322,7 @@ private List<InstitutionGeographicTaxonomies> retrieveGeographicTaxonomies(Insti
.map(geoTaxonomy -> retrieveGeoTaxonomies(geoTaxonomy.getCode())
.orElseThrow(() -> new MsCoreException(String.format(CustomError.GEO_TAXONOMY_CODE_NOT_FOUND.getMessage(), geoTaxonomy.getCode()), geoTaxonomy.getCode())))
.map(geo -> new InstitutionGeographicTaxonomies(geo.getGeotaxId(), geo.getDescription()))
.collect(Collectors.toList());
.toList();
}
return Collections.emptyList();
}
Expand Down
Loading
Loading