Skip to content

Commit

Permalink
[PPD-96] fix create debt pos: Added publish and fix org fiscal code
Browse files Browse the repository at this point in the history
  • Loading branch information
aacitelli authored and aacitelli committed Mar 21, 2022
1 parent fc0fc0b commit 397ac0e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -51,23 +56,35 @@ public void run(
}

}

/**
* calls GPD for the status and updates the elem status in the table
*
* @param filename used as partition key
* @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())
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ public class DebtPositionRowMessage {
private String id;
private String debtorName;
private String debtorEmail;
private String debtorIdFiscalCode;
private Long amount;

// generated
private String iuv;
private String iupd;

// EC config
private String fiscalCode;
private String paIdFiscalCode;
private String companyName;
private String iban;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ private List<DebtPositionRowMessage> getDebtPositionQueueMsg (List<DebtPositionE
row.setAmount(Long.parseLong(e.getAmount()));
row.setIuv(e.getPaymentNoticeNumber());
row.setIupd(e.getIupd());
row.setFiscalCode(e.getDebtorIdFiscalCode());
row.setPaIdFiscalCode(e.getPaIdFiscalCode());
row.setDebtorIdFiscalCode(e.getDebtorIdFiscalCode());
row.setCompanyName(e.getCompanyName());
row.setIban(e.getIban());
debtPositionMsgs.add(row);
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/it/gov/pagopa/canoneunico/service/GpdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class GpdClient {

private static final String POST_DEBT_POSITIONS = "/organizations/%s/debtpositions";
private static final String PUBLISH_DEBT_POSITIONS = "/organizations/%s/debtpositions/%s/publish";
private static GpdClient instance = null;
private final String gpdHost = System.getenv("GPD_HOST");

Expand All @@ -30,18 +31,37 @@ public static GpdClient getInstance() {

public boolean createDebtPosition(Logger logger, String idPa, PaymentPositionModel body) {
try {
logger.log(Level.INFO, () -> "[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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 397ac0e

Please sign in to comment.