From 397ac0e4dff7d9a51e1635b9105ab807f99d8b3f Mon Sep 17 00:00:00 2001 From: aacitelli Date: Mon, 21 Mar 2022 17:55:22 +0100 Subject: [PATCH] [PPD-96] fix create debt pos: Added publish and fix org fiscal code --- .../functions/CuCreateDebtPosition.java | 38 +++++++++++++------ .../model/DebtPositionRowMessage.java | 3 +- .../canoneunico/service/CuCsvService.java | 3 +- .../pagopa/canoneunico/service/GpdClient.java | 26 +++++++++++-- .../functions/CuCreateDebtPositionTest.java | 3 +- .../canoneunico/service/CuCsvServiceTest.java | 3 +- .../service/DebtPositionTableServiceTest.java | 3 +- 7 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/main/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPosition.java b/src/main/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPosition.java index 6b9f0e3..f596c32 100644 --- a/src/main/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPosition.java +++ b/src/main/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPosition.java @@ -39,10 +39,15 @@ public void run( // map message in a model var debtPositions = new ObjectMapper().readValue(message, DebtPositionMessage.class); + // in parallel, for each element in the message calls GPD for the status and updates the elem status in the table + //debtPositions.getRows() + // .parallelStream() + // .forEach(row -> createDebtPosition(debtPositions.getCsvFilename(), logger, row)); + // in parallel, for each element in the message calls GPD for the status and updates the elem status in the table debtPositions.getRows() .parallelStream() - .forEach(row -> createDebtPosition(debtPositions.getCsvFilename(), logger, row)); + .forEach(row -> createAndPublishDebtPosition(debtPositions.getCsvFilename(), logger, row)); logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction END] processed a message " + message); } catch (Exception e) { @@ -51,7 +56,7 @@ public void run( } } - + /** * calls GPD for the status and updates the elem status in the table * @@ -59,15 +64,27 @@ public void run( * @param logger for logging * @param row element to process */ - private void createDebtPosition(String filename, Logger logger, DebtPositionRowMessage row) { + + private void createAndPublishDebtPosition (String filename, Logger logger, DebtPositionRowMessage row) { + + var status = this.createDebtPosition(logger, row) && this.publishDebtPosition(logger, row); + + // update entity + logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction] Updating table: [paIdFiscalCode= "+row.getPaIdFiscalCode()+"; debtorIdFiscalCode=" + row.getDebtorIdFiscalCode() + "]"); + var tabelService = getDebtPositionService(logger); + tabelService.updateEntity(filename, row, status); + } + + + private boolean createDebtPosition(Logger logger, DebtPositionRowMessage row) { // get status from GPD GpdClient gpdClient = this.getGpdClientInstance(); - var status = gpdClient.createDebtPosition(logger, row.getFiscalCode(), PaymentPositionModel.builder() + return gpdClient.createDebtPosition(logger, row.getPaIdFiscalCode(), PaymentPositionModel.builder() .iupd(row.getIupd()) .type("G") - .fiscalCode(row.getFiscalCode()) + .fiscalCode(row.getDebtorIdFiscalCode()) .fullName(row.getDebtorName()) .email(row.getDebtorEmail()) .companyName(row.getCompanyName()) @@ -87,12 +104,11 @@ private void createDebtPosition(String filename, Logger logger, DebtPositionRowM .build())) .build())) .build()); - - // update entity - logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction] Updating table: " + row.getFiscalCode()); - var tabelService = getDebtPositionService(logger); - tabelService.updateEntity(filename, row, status); - + } + + private boolean publishDebtPosition(Logger logger, DebtPositionRowMessage row) { + GpdClient gpdClient = this.getGpdClientInstance(); + return gpdClient.publishDebtPosition(logger, row.getPaIdFiscalCode(), row.getIupd()); } protected GpdClient getGpdClientInstance() { diff --git a/src/main/java/it/gov/pagopa/canoneunico/model/DebtPositionRowMessage.java b/src/main/java/it/gov/pagopa/canoneunico/model/DebtPositionRowMessage.java index ba9f8c0..00fa26c 100644 --- a/src/main/java/it/gov/pagopa/canoneunico/model/DebtPositionRowMessage.java +++ b/src/main/java/it/gov/pagopa/canoneunico/model/DebtPositionRowMessage.java @@ -15,6 +15,7 @@ public class DebtPositionRowMessage { private String id; private String debtorName; private String debtorEmail; + private String debtorIdFiscalCode; private Long amount; // generated @@ -22,7 +23,7 @@ public class DebtPositionRowMessage { private String iupd; // EC config - private String fiscalCode; + private String paIdFiscalCode; private String companyName; private String iban; diff --git a/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java b/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java index 0a2386c..e5fe962 100644 --- a/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java +++ b/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java @@ -442,7 +442,8 @@ private List getDebtPositionQueueMsg (List "[CuCreateDebtPositionFunction GPD] Calling GPD service: " + idPa); + logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction GPD - createDebtPosition] Calling GPD service: " + idPa); Response response = ClientBuilder.newClient() .register(JacksonJaxbJsonProvider.class) .target(gpdHost + String.format(POST_DEBT_POSITIONS, idPa)) .request() .accept(MediaType.APPLICATION_JSON) .post(Entity.json(body)); - logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction GPD] HTTP status: " + response.getStatus() + logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction GPD - createDebtPosition] HTTP status: " + response.getStatus() + ", Body: " + response.getEntity()); return response.getStatus() == HttpStatus.CREATED.value(); } catch (Exception e) { - logger.log(Level.SEVERE, () -> "[CuCreateDebtPositionFunction ERROR] error during the GPD call " + e.getMessage() + " " + logger.log(Level.SEVERE, () -> "[CuCreateDebtPositionFunction ERROR - createDebtPosition] error during the GPD call " + e.getMessage() + " " + + e.getCause()); + return false; + } + } + + public boolean publishDebtPosition(Logger logger, String idPa, String iupd) { + try { + logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction GPD - publishDebtPosition] Calling GPD service: " + idPa); + Response response = ClientBuilder.newClient() + .register(JacksonJaxbJsonProvider.class) + .target(gpdHost + String.format(PUBLISH_DEBT_POSITIONS, idPa, iupd)) + .request() + .accept(MediaType.APPLICATION_JSON) + .post(Entity.json(null)); + logger.log(Level.INFO, () -> "[CuCreateDebtPositionFunction GPD - publishDebtPosition] HTTP status: " + response.getStatus() + + ", Body: " + response.getEntity()); + return response.getStatus() == HttpStatus.OK.value(); + } catch (Exception e) { + logger.log(Level.SEVERE, () -> "[CuCreateDebtPositionFunction ERROR - publishDebtPosition] error during the GPD call " + e.getMessage() + " " + e.getCause()); return false; } diff --git a/src/test/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPositionTest.java b/src/test/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPositionTest.java index d7fb570..c9429bb 100644 --- a/src/test/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPositionTest.java +++ b/src/test/java/it/gov/pagopa/canoneunico/functions/CuCreateDebtPositionTest.java @@ -54,7 +54,8 @@ void run() throws JsonProcessingException { .csvFilename("csv") .rows(List.of(DebtPositionRowMessage.builder() .amount(100L) - .fiscalCode("A") + .paIdFiscalCode("PAFISCALCODE") + .debtorIdFiscalCode("DEBTORFISCALCODE") .build())) .build()); function.run(message, context); diff --git a/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java b/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java index d6c564b..88d2bf1 100644 --- a/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java +++ b/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java @@ -367,7 +367,8 @@ void addDebtPositionMsg() throws InvalidKeyException, URISyntaxException, Storag r.setAmount(0L); r.setIuv("iuv"); r.setIupd("iupd"); - r.setFiscalCode("fiscalcode"); + r.setPaIdFiscalCode("paIdFiscalCode"); + r.setDebtorIdFiscalCode("debtorIdFiscalCode"); r.setCompanyName("companyname"); r.setIban("iban"); rows.add(r); diff --git a/src/test/java/it/gov/pagopa/canoneunico/service/DebtPositionTableServiceTest.java b/src/test/java/it/gov/pagopa/canoneunico/service/DebtPositionTableServiceTest.java index 1852418..67dce90 100644 --- a/src/test/java/it/gov/pagopa/canoneunico/service/DebtPositionTableServiceTest.java +++ b/src/test/java/it/gov/pagopa/canoneunico/service/DebtPositionTableServiceTest.java @@ -42,7 +42,8 @@ void updateEntity() { tableService.batchInsert(List.of(entity)); tableService.updateEntity("csv", DebtPositionRowMessage.builder() .id("1") - .fiscalCode("A") + .paIdFiscalCode("PAFISCALCODE") + .debtorIdFiscalCode("DEBTORFISCALCODE") .build(), true); var res = tableService.getEntity("csv", "1");