|
4 | 4 | import it.gov.pagopa.idpay.transactions.connector.rest.UserRestClient; |
5 | 5 | import it.gov.pagopa.idpay.transactions.connector.rest.dto.FiscalCodeInfoPDV; |
6 | 6 | import it.gov.pagopa.idpay.transactions.dto.*; |
| 7 | +import it.gov.pagopa.idpay.transactions.dto.batch.BatchCountersDTO; |
7 | 8 | import it.gov.pagopa.idpay.transactions.dto.mapper.RewardTransactionKafkaMapper; |
8 | 9 | import it.gov.pagopa.idpay.transactions.enums.PosType; |
9 | 10 | import it.gov.pagopa.idpay.transactions.enums.RewardBatchTrxStatus; |
@@ -191,8 +192,7 @@ private Mono<Void> validateBatchAndUpdateInvoiceFlow(RewardTransaction trx, |
191 | 192 | validateTrxBatchStatusNotApproved(trx); |
192 | 193 |
|
193 | 194 | return updateInvoiceFileAndFields(trx, merchantId, pointOfSaleId, transactionId, file, docNumber) |
194 | | - .flatMap(savedTrx -> suspendIfNeeded(savedTrx, oldBatch, oldBatchId, merchantId, pointOfSaleId, transactionId)) |
195 | | - .flatMap(suspendedTrx -> moveToCurrentMonthBatchAndUpdateCounters(suspendedTrx, oldBatch, oldBatchId, merchantId)) |
| 195 | + .flatMap(savedTrx -> suspendAndMoveTransaction(savedTrx, oldBatch)) |
196 | 196 | .then(); |
197 | 197 | }); |
198 | 198 | } |
@@ -250,114 +250,84 @@ private Mono<RewardTransaction> updateInvoiceFileAndFields(RewardTransaction trx |
250 | 250 | })); |
251 | 251 | } |
252 | 252 |
|
253 | | - /** (4) DOPO update fattura -> sospende (se non già sospesa). Se batch CREATED non sospendere. */ |
254 | | - private Mono<RewardTransaction> suspendIfNeeded(RewardTransaction savedTrx, |
255 | | - RewardBatch oldBatch, |
256 | | - String oldBatchId, |
257 | | - String merchantId, |
258 | | - String pointOfSaleId, |
259 | | - String transactionId) { |
260 | | - |
261 | | - // Se CREATED non sospendere |
262 | | - if (CREATED.equals(oldBatch.getStatus())) { |
263 | | - return Mono.just(savedTrx); |
264 | | - } |
265 | | - |
266 | | - // Se già suspended non fare nulla |
267 | | - if (savedTrx.getRewardBatchTrxStatus() == RewardBatchTrxStatus.SUSPENDED) { |
268 | | - return Mono.just(savedTrx); |
269 | | - } |
270 | | - |
271 | | - TransactionsRequest req = TransactionsRequest.builder() |
272 | | - .transactionIds(List.of(savedTrx.getId())) |
273 | | - .reason(savedTrx.getRejectionReasons() != null ? savedTrx.getRejectionReasons().toString() : null) |
274 | | - .build(); |
275 | | - |
276 | | - return rewardBatchService |
277 | | - .suspendTransactions(oldBatchId, savedTrx.getInitiativeId(), req) |
278 | | - .then(rewardTransactionRepository.findTransactionForUpdateInvoice(merchantId, pointOfSaleId, transactionId)) |
279 | | - .switchIfEmpty(Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, TRANSACTION_MISSING_INVOICE))); |
280 | | - } |
281 | | - |
282 | | - /** (5) sposta trx al batch del mese corrente + contatori suspended */ |
283 | | - private Mono<Void> moveToCurrentMonthBatchAndUpdateCounters(RewardTransaction suspendedTrx, |
284 | | - RewardBatch oldBatch, |
285 | | - String oldBatchId, |
286 | | - String merchantId) { |
| 253 | + private Mono<RewardBatch> findOrCreateTargetBatch(RewardTransaction oldTransaction, |
| 254 | + RewardBatch oldBatch) { |
287 | 255 |
|
288 | | - PosType posType = suspendedTrx.getPointOfSaleType(); |
289 | | - String businessName = suspendedTrx.getBusinessName(); |
| 256 | + PosType posType = oldTransaction.getPointOfSaleType(); |
| 257 | + String businessName = oldTransaction.getBusinessName(); |
290 | 258 |
|
291 | | - YearMonth now = YearMonth.now(); |
| 259 | + YearMonth currentMonth = YearMonth.now(); |
292 | 260 | YearMonth oldMonth = YearMonth.parse(oldBatch.getMonth()); |
293 | | - YearMonth targetMonth = oldMonth.isAfter(now) ? oldMonth : now; |
294 | | - |
295 | | - String initiativeId = suspendedTrx.getInitiatives().getFirst(); |
296 | | - long accruedRewardCents = suspendedTrx.getRewards().get(initiativeId).getAccruedRewardCents(); |
297 | | - |
298 | | - return rewardBatchService.findOrCreateBatch(merchantId, posType, targetMonth.toString(), businessName) |
299 | | - .flatMap(newBatch -> { |
300 | | - if (newBatch.getId().equals(oldBatchId)) { |
301 | | - // batch già del mese odierno |
302 | | - return rewardTransactionRepository.save(suspendedTrx).then(); |
303 | | - } |
304 | | - |
305 | | - Mono<Void> moveCounters = computeMoveCounters(oldBatch, oldBatchId, newBatch.getId(), accruedRewardCents); |
306 | | - |
307 | | - return moveCounters |
308 | | - .then(Mono.fromRunnable(() -> applyBatchMoveToTransaction( |
309 | | - suspendedTrx, |
310 | | - oldBatch, |
311 | | - newBatch |
312 | | - ))) |
313 | | - .then(rewardTransactionRepository.save(suspendedTrx)) |
314 | | - .then(); |
315 | | - }); |
316 | | - } |
317 | | - |
318 | | - private Mono<Void> computeMoveCounters(RewardBatch oldBatch, |
319 | | - String oldBatchId, |
320 | | - String newBatchId, |
321 | | - long accruedRewardCents) { |
322 | | - |
323 | | - if (CREATED.equals(oldBatch.getStatus())) { |
324 | | - return rewardBatchService.decrementTotalAmountCents(oldBatchId, accruedRewardCents) |
325 | | - .then(rewardBatchService.incrementTotalAmountCents(newBatchId, accruedRewardCents)) |
326 | | - .then(); |
327 | | - } |
328 | | - |
329 | | - return rewardBatchService.moveSuspendToNewBatch(oldBatchId, newBatchId, accruedRewardCents) |
330 | | - .then(); |
331 | | - } |
| 261 | + YearMonth targetMonth = oldMonth.isAfter(currentMonth) ? oldMonth : currentMonth; |
332 | 262 |
|
333 | | - private void applyBatchMoveToTransaction(RewardTransaction suspendedTrx, |
334 | | - RewardBatch oldBatch, |
335 | | - RewardBatch newBatch) { |
336 | | - |
337 | | - suspendedTrx.setRewardBatchId(newBatch.getId()); |
338 | | - suspendedTrx.setUpdateDate(LocalDateTime.now()); |
339 | | - |
340 | | - if (!CREATED.equals(oldBatch.getStatus())) { |
341 | | - updateLastMonthElaboratedOnBatchMove( |
342 | | - suspendedTrx, |
343 | | - oldBatch.getMonth(), |
344 | | - newBatch.getMonth() |
345 | | - ); |
346 | | - } |
| 263 | + return rewardBatchService.findOrCreateBatch(oldBatch.getMerchantId(), posType, targetMonth.toString(), businessName); |
347 | 264 | } |
348 | 265 |
|
| 266 | + private Mono<RewardTransaction> suspendAndMoveTransaction( |
| 267 | + RewardTransaction oldTransaction, RewardBatch oldBatch) { |
349 | 268 |
|
350 | | - private YearMonth getYearMonth (String yearMonthString){ |
351 | | - return YearMonth.parse(yearMonthString.toLowerCase(), BATCH_MONTH_FORMAT); |
| 269 | + // Se CREATED non sospendere |
| 270 | + if (CREATED.equals(oldBatch.getStatus())) { |
| 271 | + return Mono.just(oldTransaction); |
352 | 272 | } |
353 | 273 |
|
354 | | - private void updateLastMonthElaboratedOnBatchMove(RewardTransaction trx, String oldBatchMonth, String newBatchMonth) { |
355 | | - if (trx.getRewardBatchLastMonthElaborated() == null |
356 | | - || getYearMonth(trx.getRewardBatchLastMonthElaborated()).isBefore(getYearMonth(newBatchMonth))) { |
357 | | - trx.setRewardBatchLastMonthElaborated(oldBatchMonth); |
358 | | - } |
| 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)); |
359 | 329 | } |
360 | | - |
| 330 | + } |
361 | 331 |
|
362 | 332 | public Mono<Void> reversalTransaction( |
363 | 333 | String transactionId, |
|
0 commit comments