Skip to content

Commit

Permalink
feat: P4ADEV-1774 save debt position (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoGiuliani authored Jan 14, 2025
1 parent 62542ff commit 314c933
Show file tree
Hide file tree
Showing 20 changed files with 1,057 additions and 293 deletions.
581 changes: 293 additions & 288 deletions openapi/generated.openapi.json

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions openapi/p4pa-debt-position.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ components:
format: int64
iupdOrg:
type: string
iupdPagopa:
type: string
description:
type: string
status:
Expand Down Expand Up @@ -95,6 +93,9 @@ components:
paymentOptionId:
type: integer
format: int64
debtPositionId:
type: integer
format: int64
totalAmountCents:
type: integer
format: int64
Expand Down Expand Up @@ -133,7 +134,7 @@ components:
format: int64
status:
type: string
iupd_pagopa:
iupdPagopa:
type: string
iud:
type: string
Expand Down Expand Up @@ -188,6 +189,9 @@ components:
transferId:
type: integer
format: int64
installmentId:
type: integer
format: int64
orgFiscalCode:
type: string
orgName:
Expand All @@ -211,7 +215,7 @@ components:
type: string
transferIndex:
type: integer
format: int32
format: int64
ReceiptDTO:
type: object
properties:
Expand Down
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);
}
}
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;
}

}
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);
}
}
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;
}

}
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;
}

}
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);
}
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);
});
});
});
}
}

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");
}

}
Loading

0 comments on commit 314c933

Please sign in to comment.