generated from pagopa/template-payments-java-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: P4ADEV-1774 save debt position (#11)
- Loading branch information
1 parent
62542ff
commit 314c933
Showing
20 changed files
with
1,057 additions
and
293 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/it/gov/pagopa/pu/debtpositions/mapper/DebtPositionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package it.gov.pagopa.pu.debtpositions.mapper; | ||
|
||
import it.gov.pagopa.pu.debtpositions.dto.Installment; | ||
import it.gov.pagopa.pu.debtpositions.dto.generated.DebtPositionDTO; | ||
import it.gov.pagopa.pu.debtpositions.model.DebtPosition; | ||
import it.gov.pagopa.pu.debtpositions.model.InstallmentNoPII; | ||
import it.gov.pagopa.pu.debtpositions.model.PaymentOption; | ||
import org.springframework.data.util.Pair; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class DebtPositionMapper { | ||
|
||
private final PaymentOptionMapper paymentOptionMapper; | ||
|
||
private static final Collector<PaymentOption, ?, SortedSet<PaymentOption>> toPaymentOptionTreeSet = Collectors.toCollection(TreeSet::new); | ||
|
||
public DebtPositionMapper(PaymentOptionMapper paymentOptionMapper) { | ||
this.paymentOptionMapper = paymentOptionMapper; | ||
} | ||
|
||
public Pair<DebtPosition, Map<InstallmentNoPII, Installment>> mapToModel(DebtPositionDTO dto) { | ||
DebtPosition debtPosition = new DebtPosition(); | ||
debtPosition.setDebtPositionId(dto.getDebtPositionId()); | ||
debtPosition.setIupdOrg(dto.getIupdOrg()); | ||
debtPosition.setDescription(dto.getDescription()); | ||
debtPosition.setStatus(dto.getStatus()); | ||
debtPosition.setIngestionFlowFileId(dto.getIngestionFlowFileId()); | ||
debtPosition.setIngestionFlowFileLineNumber(dto.getIngestionFlowFileLineNumber()); | ||
debtPosition.setOrganizationId(dto.getOrganizationId()); | ||
debtPosition.setDebtPositionTypeOrgId(dto.getDebtPositionTypeOrgId()); | ||
debtPosition.setNotificationDate(dto.getNotificationDate()); | ||
debtPosition.setValidityDate(dto.getValidityDate()); | ||
debtPosition.setFlagIuvVolatile(dto.getFlagIuvVolatile()); | ||
debtPosition.setCreationDate(dto.getCreationDate().toLocalDateTime()); | ||
debtPosition.setUpdateDate(dto.getUpdateDate().toLocalDateTime()); | ||
|
||
Map<InstallmentNoPII, Installment> installmentMapping = new HashMap<>(); | ||
|
||
SortedSet<PaymentOption> paymentOptions = dto.getPaymentOptions().stream() | ||
.map(paymentOptionDTO -> { | ||
Pair<PaymentOption, Map<InstallmentNoPII, Installment>> paymentOptionWithInstallments = paymentOptionMapper.mapToModel(paymentOptionDTO); | ||
installmentMapping.putAll(paymentOptionWithInstallments.getSecond()); | ||
return paymentOptionWithInstallments.getFirst(); | ||
}) | ||
.collect(toPaymentOptionTreeSet); | ||
|
||
debtPosition.setPaymentOptions(paymentOptions); | ||
|
||
return Pair.of(debtPosition, installmentMapping); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/it/gov/pagopa/pu/debtpositions/mapper/InstallmentMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package it.gov.pagopa.pu.debtpositions.mapper; | ||
|
||
import it.gov.pagopa.pu.debtpositions.dto.Installment; | ||
import it.gov.pagopa.pu.debtpositions.dto.generated.InstallmentDTO; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class InstallmentMapper { | ||
|
||
private final PersonMapper personMapper; | ||
|
||
private final TransferMapper transferMapper; | ||
|
||
public InstallmentMapper(PersonMapper personMapper, TransferMapper transferMapper) { | ||
this.personMapper = personMapper; | ||
this.transferMapper = transferMapper; | ||
} | ||
|
||
public Installment mapToModel(InstallmentDTO dto) { | ||
Installment installment = new Installment(); | ||
installment.setInstallmentId(dto.getInstallmentId()); | ||
installment.setPaymentOptionId(dto.getPaymentOptionId()); | ||
installment.setStatus(dto.getStatus()); | ||
installment.setIupdPagopa(dto.getIupdPagopa()); | ||
installment.setIud(dto.getIud()); | ||
installment.setIuv(dto.getIuv()); | ||
installment.setIur(dto.getIur()); | ||
installment.setIuf(dto.getIuf()); | ||
installment.setNav(dto.getNav()); | ||
installment.setDueDate(dto.getDueDate()); | ||
installment.setPaymentTypeCode(dto.getPaymentTypeCode()); | ||
installment.setAmountCents(dto.getAmountCents()); | ||
installment.setNotificationFeeCents(dto.getNotificationFeeCents()); | ||
installment.setRemittanceInformation(dto.getRemittanceInformation()); | ||
installment.setHumanFriendlyRemittanceInformation(dto.getHumanFriendlyRemittanceInformation()); | ||
installment.setBalance(dto.getBalance()); | ||
installment.setLegacyPaymentMetadata(dto.getLegacyPaymentMetadata()); | ||
installment.setDebtor(personMapper.mapToModel(dto.getDebtor())); | ||
installment.setTransfers(dto.getTransfers().stream() | ||
.map(transferMapper::mapToModel) | ||
.toList()); | ||
installment.setCreationDate(dto.getCreationDate().toLocalDateTime()); | ||
installment.setUpdateDate(dto.getUpdateDate().toLocalDateTime()); | ||
return installment; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/it/gov/pagopa/pu/debtpositions/mapper/PaymentOptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package it.gov.pagopa.pu.debtpositions.mapper; | ||
|
||
import it.gov.pagopa.pu.debtpositions.dto.Installment; | ||
import it.gov.pagopa.pu.debtpositions.dto.InstallmentPIIDTO; | ||
import it.gov.pagopa.pu.debtpositions.dto.generated.PaymentOptionDTO; | ||
import it.gov.pagopa.pu.debtpositions.enums.PaymentOptionType; | ||
import it.gov.pagopa.pu.debtpositions.model.InstallmentNoPII; | ||
import it.gov.pagopa.pu.debtpositions.model.PaymentOption; | ||
import org.springframework.data.util.Pair; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class PaymentOptionMapper { | ||
|
||
private final InstallmentMapper installmentMapper; | ||
private final InstallmentPIIMapper installmentPIIMapper; | ||
|
||
private static final Collector<InstallmentNoPII, ?, SortedSet<InstallmentNoPII>> toInstallmentTreeSet = Collectors.toCollection(TreeSet::new); | ||
|
||
public PaymentOptionMapper(InstallmentMapper installmentMapper, InstallmentPIIMapper installmentPIIMapper) { | ||
this.installmentMapper = installmentMapper; | ||
this.installmentPIIMapper = installmentPIIMapper; | ||
} | ||
|
||
public Pair<PaymentOption, Map<InstallmentNoPII, Installment>> mapToModel(PaymentOptionDTO dto) { | ||
Map<InstallmentNoPII, Installment> installmentMapping = new HashMap<>(); | ||
|
||
List<Installment> installments = dto.getInstallments().stream() | ||
.map(installmentMapper::mapToModel) | ||
.toList(); | ||
|
||
SortedSet<InstallmentNoPII> installmentNoPIIs = installments.stream() | ||
.map(installment -> { | ||
Pair<InstallmentNoPII, InstallmentPIIDTO> result = installmentPIIMapper.map(installment); | ||
InstallmentNoPII installmentNoPII = result.getFirst(); | ||
installmentMapping.put(installmentNoPII, installment); | ||
return installmentNoPII; | ||
}) | ||
.collect(toInstallmentTreeSet); | ||
|
||
PaymentOption paymentOption = new PaymentOption(); | ||
paymentOption.setPaymentOptionId(dto.getPaymentOptionId()); | ||
paymentOption.setDebtPositionId(dto.getDebtPositionId()); | ||
paymentOption.setTotalAmountCents(dto.getTotalAmountCents()); | ||
paymentOption.setStatus(dto.getStatus()); | ||
paymentOption.setMultiDebtor(dto.getMultiDebtor()); | ||
paymentOption.setDueDate(dto.getDueDate()); | ||
paymentOption.setDescription(dto.getDescription()); | ||
paymentOption.setPaymentOptionType(PaymentOptionType.valueOf(dto.getPaymentOptionType().name())); | ||
paymentOption.setInstallments(installmentNoPIIs); | ||
|
||
return Pair.of(paymentOption, installmentMapping); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/it/gov/pagopa/pu/debtpositions/mapper/PersonMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package it.gov.pagopa.pu.debtpositions.mapper; | ||
|
||
import it.gov.pagopa.pu.debtpositions.dto.Person; | ||
import it.gov.pagopa.pu.debtpositions.dto.generated.PersonDTO; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class PersonMapper { | ||
|
||
public Person mapToModel(PersonDTO dto) { | ||
Person person = new Person(); | ||
person.setEntityType(dto.getEntityType()); | ||
person.setFiscalCode(dto.getFiscalCode()); | ||
person.setFullName(dto.getFullName()); | ||
person.setAddress(dto.getAddress()); | ||
person.setCivic(dto.getCivic()); | ||
person.setPostalCode(dto.getPostalCode()); | ||
person.setLocation(dto.getLocation()); | ||
person.setProvince(dto.getProvince()); | ||
person.setNation(dto.getNation()); | ||
person.setEmail(dto.getEmail()); | ||
return person; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/it/gov/pagopa/pu/debtpositions/mapper/TransferMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package it.gov.pagopa.pu.debtpositions.mapper; | ||
|
||
import it.gov.pagopa.pu.debtpositions.dto.generated.TransferDTO; | ||
import it.gov.pagopa.pu.debtpositions.model.Stamp; | ||
import it.gov.pagopa.pu.debtpositions.model.Transfer; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class TransferMapper { | ||
|
||
public Transfer mapToModel(TransferDTO dto) { | ||
Transfer transfer = new Transfer(); | ||
transfer.setTransferId(dto.getTransferId()); | ||
transfer.setInstallmentId(dto.getInstallmentId()); | ||
transfer.setOrgFiscalCode(dto.getOrgFiscalCode()); | ||
transfer.setOrgName(dto.getOrgName()); | ||
transfer.setAmountCents(dto.getAmountCents()); | ||
transfer.setRemittanceInformation(dto.getRemittanceInformation()); | ||
transfer.setStamp(new Stamp(dto.getStampType(), dto.getStampHashDocument(), dto.getStampProvincialResidence())); | ||
transfer.setIban(dto.getIban()); | ||
transfer.setPostalIban(dto.getPostalIban()); | ||
transfer.setCategory(dto.getCategory()); | ||
transfer.setTransferIndex(dto.getTransferIndex()); | ||
return transfer; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/it/gov/pagopa/pu/debtpositions/service/DebtPositionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package it.gov.pagopa.pu.debtpositions.service; | ||
|
||
import it.gov.pagopa.pu.debtpositions.dto.generated.DebtPositionDTO; | ||
|
||
public interface DebtPositionService { | ||
|
||
void saveDebtPosition(DebtPositionDTO debtPositionDTO); | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/it/gov/pagopa/pu/debtpositions/service/DebtPositionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package it.gov.pagopa.pu.debtpositions.service; | ||
|
||
import it.gov.pagopa.pu.debtpositions.dto.Installment; | ||
import it.gov.pagopa.pu.debtpositions.dto.generated.DebtPositionDTO; | ||
import it.gov.pagopa.pu.debtpositions.mapper.*; | ||
import it.gov.pagopa.pu.debtpositions.model.DebtPosition; | ||
import it.gov.pagopa.pu.debtpositions.model.InstallmentNoPII; | ||
import it.gov.pagopa.pu.debtpositions.model.PaymentOption; | ||
import it.gov.pagopa.pu.debtpositions.repository.*; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.data.util.Pair; | ||
|
||
import java.util.Map; | ||
|
||
public class DebtPositionServiceImpl implements DebtPositionService { | ||
|
||
private final DebtPositionRepository debtPositionRepository; | ||
private final PaymentOptionRepository paymentOptionRepository; | ||
private final InstallmentPIIRepository installmentRepository; | ||
private final TransferRepository transferRepository; | ||
private final DebtPositionMapper debtPositionMapper; | ||
|
||
public DebtPositionServiceImpl(DebtPositionRepository debtPositionRepository, PaymentOptionRepository paymentOptionRepository, | ||
InstallmentPIIRepository installmentRepository, TransferRepository transferRepository, | ||
DebtPositionMapper debtPositionMapper) { | ||
this.debtPositionRepository = debtPositionRepository; | ||
this.paymentOptionRepository = paymentOptionRepository; | ||
this.installmentRepository = installmentRepository; | ||
this.transferRepository = transferRepository; | ||
this.debtPositionMapper = debtPositionMapper; | ||
} | ||
|
||
@Transactional | ||
@Override | ||
public void saveDebtPosition(DebtPositionDTO debtPositionDTO) { | ||
Pair<DebtPosition, Map<InstallmentNoPII, Installment>> mappedDebtPosition = debtPositionMapper.mapToModel(debtPositionDTO); | ||
|
||
DebtPosition savedDebtPosition = debtPositionRepository.save(mappedDebtPosition.getFirst()); | ||
|
||
savedDebtPosition.getPaymentOptions().forEach(paymentOption -> { | ||
paymentOption.setDebtPositionId(savedDebtPosition.getDebtPositionId()); | ||
PaymentOption savedPaymentOption = paymentOptionRepository.save(paymentOption); | ||
|
||
savedPaymentOption.getInstallments().forEach(installmentNoPII -> { | ||
Installment mappedInstallment = mappedDebtPosition.getSecond().get(installmentNoPII); | ||
mappedInstallment.setPaymentOptionId(savedPaymentOption.getPaymentOptionId()); | ||
long idInstallment = installmentRepository.save(mappedInstallment); | ||
|
||
mappedInstallment.getTransfers().forEach(transfer -> { | ||
transfer.setInstallmentId(idInstallment); | ||
transferRepository.save(transfer); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
src/test/java/it/gov/pagopa/pu/debtpositions/mapper/DebtPositionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package it.gov.pagopa.pu.debtpositions.mapper; | ||
|
||
import it.gov.pagopa.pu.debtpositions.citizen.service.DataCipherService; | ||
import it.gov.pagopa.pu.debtpositions.dto.Installment; | ||
import it.gov.pagopa.pu.debtpositions.dto.generated.DebtPositionDTO; | ||
import it.gov.pagopa.pu.debtpositions.model.DebtPosition; | ||
import it.gov.pagopa.pu.debtpositions.model.InstallmentNoPII; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.springframework.data.util.Pair; | ||
|
||
import java.util.Map; | ||
|
||
import static it.gov.pagopa.pu.debtpositions.util.TestUtils.checkNotNullFields; | ||
import static it.gov.pagopa.pu.debtpositions.util.faker.DebtPositionFaker.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DebtPositionMapperTest { | ||
|
||
@Mock | ||
private DataCipherService dataCipherServiceMock; | ||
|
||
private DebtPositionMapper debtPositionMapper; | ||
|
||
@BeforeEach | ||
void init(){ | ||
PersonMapper personMapper = new PersonMapper(); | ||
TransferMapper transferMapper = new TransferMapper(); | ||
InstallmentMapper installmentMapper = new InstallmentMapper(personMapper, transferMapper); | ||
InstallmentPIIMapper installmentPIIMapper = new InstallmentPIIMapper(dataCipherServiceMock); | ||
PaymentOptionMapper paymentOptionMapper = new PaymentOptionMapper(installmentMapper, installmentPIIMapper); | ||
debtPositionMapper = new DebtPositionMapper(paymentOptionMapper); | ||
} | ||
|
||
@Test | ||
void givenValidDebtPositionDTO_whenMapToModel_thenReturnDebtPositionAndInstallmentMap() { | ||
DebtPosition debtPositionExpected = buildDebtPosition(); | ||
DebtPositionDTO debtPositionDTO = buildDebtPositionDTO(); | ||
|
||
Pair<DebtPosition, Map<InstallmentNoPII, Installment>> result = debtPositionMapper.mapToModel(debtPositionDTO); | ||
|
||
assertEquals(debtPositionExpected, result.getFirst()); | ||
checkNotNullFields(result.getFirst(), "updateOperatorExternalId"); | ||
} | ||
|
||
} |
Oops, something went wrong.