Skip to content

Commit b81b602

Browse files
author
dalilaacqua
committed
Fix updateTotals and add logs
1 parent 676aae0 commit b81b602

File tree

3 files changed

+136
-75
lines changed

3 files changed

+136
-75
lines changed

src/main/java/it/gov/pagopa/idpay/transactions/dto/batch/BatchCountersDTO.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@ public class BatchCountersDTO {
1212
private Long trxElaborated;
1313
private Long trxRejected;
1414

15-
private BatchCountersDTO() {
15+
@Override
16+
public String toString() {
17+
return "BatchCountersDTO{" +
18+
"initialAmountCents=" + initialAmountCents +
19+
", numberOfTransactions=" + numberOfTransactions +
20+
", suspendedAmountCents=" + suspendedAmountCents +
21+
", trxSuspended=" + trxSuspended +
22+
", approvedAmountCents=" + approvedAmountCents +
23+
", trxElaborated=" + trxElaborated +
24+
", trxRejected=" + trxRejected +
25+
'}';
26+
}
27+
28+
private BatchCountersDTO() {
1629
this.initialAmountCents = 0L;
1730
this.numberOfTransactions = 0L;
1831
this.approvedAmountCents = 0L;

src/main/java/it/gov/pagopa/idpay/transactions/repository/RewardBatchSpecificRepositoryImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ public Mono<RewardBatch> updateTotals(String rewardBatchId, BatchCountersDTO acc
222222
if (acc.getSuspendedAmountCents() != 0) {
223223
update.inc(RewardBatch.Fields.suspendedAmountCents, acc.getSuspendedAmountCents());
224224
}
225+
if (acc.getInitialAmountCents() != 0) {
226+
update.inc(RewardBatch.Fields.initialAmountCents, acc.getInitialAmountCents());
227+
}
228+
if (acc.getNumberOfTransactions() != 0) {
229+
update.inc(RewardBatch.Fields.numberOfTransactions, acc.getNumberOfTransactions());
230+
}
225231

226232
update.currentDate(RewardBatch.Fields.updateDate);
227233

src/main/java/it/gov/pagopa/idpay/transactions/service/PointOfSaleTransactionServiceImpl.java

Lines changed: 116 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,17 @@ private Mono<Page<RewardTransaction>> getTransactions(TrxFiltersDTO filters,
168168
public Mono<Void> updateInvoiceTransaction(String transactionId, String merchantId,
169169
String pointOfSaleId, FilePart file, String docNumber) {
170170

171+
log.info("[UPDATE_INVOICE_FILE_SERVICE] - [updateInvoiceTransaction] - start | trxId={} merchantId={} posId={} docNumber={} filename={}",
172+
transactionId, merchantId, pointOfSaleId, docNumber, file != null ? file.filename() : null);
173+
171174
Utilities.checkFileExtensionOrThrow(file);
172175

173176
return rewardTransactionRepository
174177
.findTransactionForUpdateInvoice(merchantId, pointOfSaleId, transactionId)
175-
.switchIfEmpty(Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, TRANSACTION_MISSING_INVOICE)))
176-
.flatMap(trx -> validateBatchAndUpdateInvoiceFlow(trx, merchantId, pointOfSaleId, transactionId, file, docNumber));
178+
.switchIfEmpty(Mono.defer(() -> Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, TRANSACTION_MISSING_INVOICE))))
179+
.flatMap(trx -> validateBatchAndUpdateInvoiceFlow(trx, merchantId, pointOfSaleId, transactionId, file, docNumber))
180+
.doOnSuccess(v -> log.info("[UPDATE_INVOICE_FILE_SERVICE] - [findTransactionForUpdateInvoice] - success | trxId={} merchantId={} posId={}",
181+
transactionId, merchantId, pointOfSaleId));
177182
}
178183

179184
private Mono<Void> validateBatchAndUpdateInvoiceFlow(RewardTransaction trx,
@@ -183,29 +188,39 @@ private Mono<Void> validateBatchAndUpdateInvoiceFlow(RewardTransaction trx,
183188
FilePart file,
184189
String docNumber) {
185190

191+
log.info("[UPDATE_INVOICE_FILE_SERVICE] - [validateBatchAndUpdateInvoiceFlow] - start | trxId={} merchantId={} posId={} docNumber={} filename={}",
192+
transactionId, merchantId, pointOfSaleId, docNumber, file != null ? file.filename() : null);
193+
186194
String oldBatchId = requireRewardBatchId(trx);
187195

188196
return rewardBatchRepository.findRewardBatchById(oldBatchId)
189-
.switchIfEmpty(Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, REWARD_BATCH_NOT_FOUND)))
197+
.switchIfEmpty(Mono.defer(() -> Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, REWARD_BATCH_NOT_FOUND))))
190198
.flatMap(oldBatch -> {
191199
validateOldBatchStatusAllowed(oldBatch);
192200
validateTrxBatchStatusNotApproved(trx);
193201

194202
return updateInvoiceFileAndFields(trx, merchantId, pointOfSaleId, transactionId, file, docNumber)
195203
.flatMap(savedTrx -> suspendAndMoveTransaction(savedTrx, oldBatch))
196204
.then();
197-
});
205+
})
206+
.doOnSuccess(v -> log.info("[UPDATE_INVOICE_FILE_SERVICE] - [validateBatchAndUpdateInvoiceFlow] - end success | trxId={} oldBatchId={}",
207+
transactionId, oldBatchId))
208+
.doOnError(e -> log.error("[UPDATE_INVOICE_FILE_SERVICE] - [validateBatchAndUpdateInvoiceFlow] - end fail | trxId={} oldBatchId={} errorType={} message={}",
209+
transactionId, oldBatchId,
210+
e != null ? e.getClass().getSimpleName() : null,
211+
e != null ? e.getMessage() : null,
212+
e));
198213
}
199214

200215
private String requireRewardBatchId(RewardTransaction trx) {
201216
String oldBatchId = trx.getRewardBatchId();
202217
if (oldBatchId == null) {
203218
throw new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, REWARD_BATCH_NOT_FOUND);
204219
}
220+
205221
return oldBatchId;
206222
}
207223

208-
/** (1) batch deve essere EVALUATING o CREATED */
209224
private void validateOldBatchStatusAllowed(RewardBatch oldBatch) {
210225
if (!EVALUATING.equals(oldBatch.getStatus()) && !CREATED.equals(oldBatch.getStatus())) {
211226
throw new ClientExceptionWithBody(
@@ -214,12 +229,10 @@ private void validateOldBatchStatusAllowed(RewardBatch oldBatch) {
214229
ERROR_MESSAGE_REWARD_BATCH_STATUS_NOT_ALLOWED
215230
);
216231
}
217-
}
232+
}
218233

219-
/** (2) trx batch status deve essere != APPROVED */
220234
private void validateTrxBatchStatusNotApproved(RewardTransaction trx) {
221-
RewardBatchTrxStatus batchTrxStatus = trx.getRewardBatchTrxStatus();
222-
if (batchTrxStatus == RewardBatchTrxStatus.APPROVED) {
235+
if (trx.getRewardBatchTrxStatus() == RewardBatchTrxStatus.APPROVED) {
223236
throw new ClientExceptionWithBody(
224237
HttpStatus.BAD_REQUEST,
225238
TRANSACTION_STATUS_NOT_ALLOWED,
@@ -228,14 +241,16 @@ private void validateTrxBatchStatusNotApproved(RewardTransaction trx) {
228241
}
229242
}
230243

231-
/** (3) replace file + update campi fattura + save trx */
232244
private Mono<RewardTransaction> updateInvoiceFileAndFields(RewardTransaction trx,
233245
String merchantId,
234246
String pointOfSaleId,
235247
String transactionId,
236248
FilePart file,
237249
String docNumber) {
238250

251+
log.info("[UPDATE_INVOICE_FILE_SERVICE] - [updateInvoiceFileAndFields] - start | trxId={} merchantId={} posId={} docNumber={} filename={}",
252+
transactionId, merchantId, pointOfSaleId, docNumber, file != null ? file.filename() : null);
253+
239254
InvoiceData oldDocumentData = validateTransactionData(trx, merchantId, pointOfSaleId);
240255

241256
return replaceInvoiceFile(file, oldDocumentData, merchantId, pointOfSaleId, transactionId)
@@ -247,87 +262,114 @@ private Mono<RewardTransaction> updateInvoiceFileAndFields(RewardTransaction trx
247262
trx.setInvoiceUploadDate(LocalDateTime.now());
248263
trx.setUpdateDate(LocalDateTime.now());
249264
return rewardTransactionRepository.save(trx);
250-
}));
265+
}))
266+
.doOnNext(saved -> log.info("[UPDATE_INVOICE_FILE_SERVICE] - [updateInvoiceFileAndFields] - end success | trxId={} invoiceFilename={} docNumber={}",
267+
transactionId,
268+
saved != null && saved.getInvoiceData() != null ? saved.getInvoiceData().getFilename() : null,
269+
saved != null && saved.getInvoiceData() != null ? saved.getInvoiceData().getDocNumber() : null))
270+
.doOnError(e -> log.error("[UPDATE_INVOICE_FILE_SERVICE] - [updateInvoiceFileAndFields] - end fail | trxId={} errorType={} message={}",
271+
transactionId,
272+
e != null ? e.getClass().getSimpleName() : null,
273+
e != null ? e.getMessage() : null,
274+
e));
251275
}
252276

253277
private Mono<RewardBatch> findOrCreateTargetBatch(RewardTransaction oldTransaction,
254278
RewardBatch oldBatch) {
255279

280+
log.info("[UPDATE_INVOICE_FILE_SERVICE] - [findOrCreateTargetBatch] - start | oldBatchId={} trxId={} posType={} businessName={}",
281+
oldBatch.getId(), oldTransaction.getId(), oldTransaction.getPointOfSaleType(), oldTransaction.getBusinessName());
282+
256283
PosType posType = oldTransaction.getPointOfSaleType();
257284
String businessName = oldTransaction.getBusinessName();
258285

259286
YearMonth currentMonth = YearMonth.now();
260287
YearMonth oldMonth = YearMonth.parse(oldBatch.getMonth());
261288
YearMonth targetMonth = oldMonth.isAfter(currentMonth) ? oldMonth : currentMonth;
262289

263-
return rewardBatchService.findOrCreateBatch(oldBatch.getMerchantId(), posType, targetMonth.toString(), businessName);
290+
return rewardBatchService.findOrCreateBatch(oldBatch.getMerchantId(), posType, targetMonth.toString(), businessName)
291+
.doOnNext(b -> log.info("[UPDATE_INVOICE_FILE_SERVICE] - [findOrCreateTargetBatch] - end success | targetBatchId={} month={} status={}",
292+
b != null ? b.getId() : null,
293+
b != null ? b.getMonth() : null,
294+
b != null ? b.getStatus() : null))
295+
.doOnError(e -> log.error("[UPDATE_INVOICE_FILE_SERVICE] - [findOrCreateTargetBatch] - end fail | errorType={} message={}",
296+
e != null ? e.getClass().getSimpleName() : null,
297+
e != null ? e.getMessage() : null,
298+
e));
264299
}
265300

266-
private Mono<RewardTransaction> suspendAndMoveTransaction(
267-
RewardTransaction oldTransaction, RewardBatch oldBatch) {
301+
private Mono<RewardTransaction> suspendAndMoveTransaction(
302+
RewardTransaction oldTransaction, RewardBatch oldBatch) {
268303

269-
// Se CREATED non sospendere
270-
if (CREATED.equals(oldBatch.getStatus())) {
271-
return Mono.just(oldTransaction);
272-
}
304+
log.info("[UPDATE_INVOICE_FILE_SERVICE] - [suspendAndMoveTransaction] - start | trxId={} oldBatchId={} oldBatchStatus={} trxBatchStatus={}",
305+
oldTransaction.getId(), oldBatch.getId(), oldBatch.getStatus(), oldTransaction.getRewardBatchTrxStatus());
306+
307+
if (CREATED.equals(oldBatch.getStatus())) {
308+
log.info("[UPDATE_INVOICE_FILE_SERVICE] - [suspendAndMoveTransaction] - end success | no-move (old batch CREATED) | trxId={} oldBatchId={}",
309+
oldTransaction.getId(), oldBatch.getId());
310+
return Mono.just(oldTransaction);
311+
}
273312

274-
long accruedRewardCents =
275-
oldTransaction
276-
.getRewards()
277-
.get(oldTransaction.getInitiatives().getFirst())
278-
.getAccruedRewardCents();
279-
280-
if (oldTransaction.getRewardBatchTrxStatus() == RewardBatchTrxStatus.SUSPENDED) {
281-
282-
BatchCountersDTO oldBatchCounter = BatchCountersDTO.newBatch()
283-
.decrementNumberOfTransactions()
284-
.decrementTrxElaborated();
285-
286-
BatchCountersDTO newBatchCounter =
287-
BatchCountersDTO.newBatch()
288-
.incrementInitialAmountCents(accruedRewardCents)
289-
.incrementNumberOfTransactions(1L)
290-
.incrementTrxSuspended(1L)
291-
.incrementSuspendedAmountCents(accruedRewardCents)
292-
.incrementTrxElaborated(1L);
293-
294-
return rewardBatchRepository
295-
.updateTotals(oldBatch.getId(), oldBatchCounter)
296-
.flatMap(savedBatch -> findOrCreateTargetBatch(oldTransaction, oldBatch))
297-
.flatMap(
298-
newBatch -> rewardBatchRepository.updateTotals(newBatch.getId(), newBatchCounter))
299-
.thenReturn(oldTransaction);
300-
} else {
301-
boolean isRejected = oldTransaction.getRewardBatchTrxStatus() == RewardBatchTrxStatus.REJECTED;
302-
oldTransaction.setRewardBatchTrxStatus(RewardBatchTrxStatus.SUSPENDED);
303-
oldTransaction.setUpdateDate(LocalDateTime.now());
304-
305-
BatchCountersDTO oldBatchCounter =
306-
BatchCountersDTO.newBatch()
307-
.decrementNumberOfTransactions()
308-
.decrementTrxElaborated(isRejected ? 1L : 0L);
309-
310-
BatchCountersDTO newBatchCounter =
311-
BatchCountersDTO.newBatch()
312-
.incrementInitialAmountCents(accruedRewardCents)
313-
.incrementNumberOfTransactions(1L)
314-
.incrementTrxSuspended(1L)
315-
.incrementSuspendedAmountCents(accruedRewardCents)
316-
.incrementTrxElaborated(1L);
317-
318-
return rewardTransactionRepository
319-
.save(oldTransaction)
320-
.flatMap(
321-
savedTrx ->
322-
rewardBatchRepository
323-
.updateTotals(oldBatch.getId(), oldBatchCounter)
324-
.flatMap(savedBatch -> findOrCreateTargetBatch(oldTransaction, oldBatch))
325-
.flatMap(
326-
newBatch ->
327-
rewardBatchRepository.updateTotals(newBatch.getId(), newBatchCounter))
328-
.thenReturn(oldTransaction));
313+
long accruedRewardCents =
314+
oldTransaction
315+
.getRewards()
316+
.get(oldTransaction.getInitiatives().getFirst())
317+
.getAccruedRewardCents();
318+
319+
boolean wasSuspended = oldTransaction.getRewardBatchTrxStatus() == RewardBatchTrxStatus.SUSPENDED;
320+
boolean wasRejected = oldTransaction.getRewardBatchTrxStatus() == RewardBatchTrxStatus.REJECTED;
321+
322+
BatchCountersDTO oldBatchCounter;
323+
BatchCountersDTO newBatchCounter;
324+
325+
if (wasSuspended) {
326+
oldBatchCounter = BatchCountersDTO.newBatch()
327+
.decrementNumberOfTransactions()
328+
.decrementTrxElaborated();
329+
330+
newBatchCounter = BatchCountersDTO.newBatch()
331+
.incrementInitialAmountCents(accruedRewardCents)
332+
.incrementNumberOfTransactions(1L)
333+
.incrementTrxSuspended(1L)
334+
.incrementSuspendedAmountCents(accruedRewardCents)
335+
.incrementTrxElaborated(1L);
336+
} else {
337+
oldBatchCounter = BatchCountersDTO.newBatch()
338+
.decrementNumberOfTransactions()
339+
.decrementTrxElaborated(wasRejected ? 1L : 0L);
340+
341+
newBatchCounter = BatchCountersDTO.newBatch()
342+
.incrementInitialAmountCents(accruedRewardCents)
343+
.incrementNumberOfTransactions(1L)
344+
.incrementTrxSuspended(1L)
345+
.incrementSuspendedAmountCents(accruedRewardCents)
346+
.incrementTrxElaborated(1L);
347+
}
348+
349+
return findOrCreateTargetBatch(oldTransaction, oldBatch)
350+
.flatMap(newBatch -> {
351+
log.info("[UPDATE_INVOICE_FILE_SERVICE] - [suspendAndMoveTransaction] - moving trx | trxId={} fromBatchId={} toBatchId={} oldStatus={} newStatus=SUSPENDED oldCounters={} newCounters={}",
352+
oldTransaction.getId(), oldBatch.getId(), newBatch.getId(),
353+
oldTransaction.getRewardBatchTrxStatus(),
354+
oldBatchCounter, newBatchCounter);
355+
356+
oldTransaction.setRewardBatchTrxStatus(RewardBatchTrxStatus.SUSPENDED);
357+
oldTransaction.setRewardBatchId(newBatch.getId());
358+
oldTransaction.setUpdateDate(LocalDateTime.now());
359+
360+
return rewardTransactionRepository.save(oldTransaction)
361+
.then(rewardBatchRepository.updateTotals(oldBatch.getId(), oldBatchCounter))
362+
.then(rewardBatchRepository.updateTotals(newBatch.getId(), newBatchCounter))
363+
.thenReturn(oldTransaction);
364+
})
365+
.doOnNext(saved -> log.info("[UPDATE_INVOICE_FILE_SERVICE] - [suspendAndMoveTransaction] - end success | trxId={} rewardBatchId={} trxBatchStatus={}",
366+
saved.getId(), saved.getRewardBatchId(), saved.getRewardBatchTrxStatus()))
367+
.doOnError(e -> log.error("[UPDATE_INVOICE_FILE_SERVICE] - [suspendAndMoveTransaction] - end fail | trxId={} oldBatchId={} errorType={} message={}",
368+
oldTransaction.getId(), oldBatch.getId(),
369+
e != null ? e.getClass().getSimpleName() : null,
370+
e != null ? e.getMessage() : null,
371+
e));
329372
}
330-
}
331373

332374
public Mono<Void> reversalTransaction(
333375
String transactionId,

0 commit comments

Comments
 (0)