Skip to content

Commit c871cd9

Browse files
committed
fix: [EBE-549] refactoring and fixing counters handling for invoice update
1 parent 9266059 commit c871cd9

File tree

9 files changed

+105
-160
lines changed

9 files changed

+105
-160
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ public BatchCountersDTO incrementInitialAmountCents(Long amountCents) {
3131
return this;
3232
}
3333

34-
3534
public BatchCountersDTO incrementNumberOfTransactions(Long number) {
3635
this.numberOfTransactions = this.numberOfTransactions + number;
3736
return this;
3837
}
3938

39+
public BatchCountersDTO decrementNumberOfTransactions() {
40+
this.numberOfTransactions = this.numberOfTransactions - 1;
41+
return this;
42+
}
43+
4044
public BatchCountersDTO incrementApprovedAmountCents(Long amountCents) {
4145
this.approvedAmountCents = this.approvedAmountCents + amountCents;
4246
return this;
@@ -57,6 +61,11 @@ public BatchCountersDTO decrementSuspendedAmountCents(Long amountCents) {
5761
return this;
5862
}
5963

64+
public BatchCountersDTO decrementTrxElaborated() {
65+
this.trxElaborated = this.trxElaborated - 1L;
66+
return this;
67+
}
68+
6069
public BatchCountersDTO incrementTrxElaborated() {
6170
this.trxElaborated = this.trxElaborated + 1L;
6271
return this;
@@ -67,6 +76,11 @@ public BatchCountersDTO incrementTrxElaborated(Long number) {
6776
return this;
6877
}
6978

79+
public BatchCountersDTO decrementTrxElaborated(Long number) {
80+
this.trxElaborated = this.trxElaborated - number;
81+
return this;
82+
}
83+
7084
public BatchCountersDTO incrementTrxSuspended() {
7185
this.trxSuspended = this.trxSuspended + 1L;
7286
return this;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
public interface RewardBatchSpecificRepository {
1313
Mono<RewardBatch> incrementTotalAmountCents(String batchId, long accruedAmountCents);
1414
Mono<RewardBatch> decrementTotalAmountCents(String batchId, long accruedAmountCents);
15-
Mono<RewardBatch> moveSuspendToNewBatch(String oldBatchId, String newBatchId, long accruedAmountCents);
1615
Mono<RewardBatch> moveTrxToNewBatch(String oldBatchId, String newBatchId, long accruedAmountCents, boolean isSuspended);
1716
Flux<RewardBatch> findRewardBatchesCombined(String merchantId, String status, String assigneeLevel, String month, boolean isOperator, Pageable pageable);
1817
Mono<Long> getCountCombined(String merchantId, String status, String assigneeLevel, String month, boolean isOperator);

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public Mono<Long> getCountCombined(String merchantId, String status, String assi
5353
return mongoTemplate.count(Query.query(criteria), RewardBatch.class);
5454
}
5555

56+
@Deprecated
5657
@Override
5758
public Mono<RewardBatch> incrementTotalAmountCents(String batchId, long accruedAmountCents) {
5859
return mongoTemplate.findAndModify(
@@ -66,6 +67,7 @@ public Mono<RewardBatch> incrementTotalAmountCents(String batchId, long accruedA
6667
);
6768
}
6869

70+
@Deprecated
6971
@Override
7072
public Mono<RewardBatch> decrementTotalAmountCents(String batchId, long accruedAmountCents) {
7173
return mongoTemplate.findAndModify(
@@ -117,42 +119,6 @@ public Mono<RewardBatch> moveTrxToNewBatch(String oldBatchId, String newBatchId,
117119
.switchIfEmpty(Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, REWARD_BATCH_NOT_FOUND)));
118120
}
119121

120-
@Override
121-
public Mono<RewardBatch> moveSuspendToNewBatch(String oldBatchId, String newBatchId, long accruedAmountCents) {
122-
123-
Update decOld = new Update()
124-
//.inc(INITIAL_AMOUNT_CENTS, -accruedAmountCents)
125-
.inc(NUMBER_OF_TRANSACTIONS, -1)
126-
.inc(SUSPENDED_AMOUNT_CENTS, -accruedAmountCents)
127-
.inc(NUMBER_OF_TRANSACTIONS_SUSPENDED, -1)
128-
.inc(NUMBER_OF_TRANSACTIONS_ELABORATED, -1)
129-
.set(RewardBatch.Fields.updateDate, LocalDateTime.now());
130-
131-
Update incNew = new Update()
132-
.inc(INITIAL_AMOUNT_CENTS, accruedAmountCents)
133-
.inc(NUMBER_OF_TRANSACTIONS, 1)
134-
.inc(SUSPENDED_AMOUNT_CENTS, accruedAmountCents)
135-
.inc(NUMBER_OF_TRANSACTIONS_SUSPENDED, 1)
136-
.inc(NUMBER_OF_TRANSACTIONS_ELABORATED, 1)
137-
.set(RewardBatch.Fields.updateDate, LocalDateTime.now());
138-
139-
return mongoTemplate.findAndModify(
140-
Query.query(Criteria.where("_id").is(oldBatchId)),
141-
decOld,
142-
FindAndModifyOptions.options().returnNew(true),
143-
RewardBatch.class
144-
)
145-
.switchIfEmpty(Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, REWARD_BATCH_NOT_FOUND)))
146-
.then(mongoTemplate.findAndModify(
147-
Query.query(Criteria.where("_id").is(newBatchId)),
148-
incNew,
149-
FindAndModifyOptions.options().returnNew(true),
150-
RewardBatch.class
151-
))
152-
.switchIfEmpty(Mono.error(new ClientExceptionNoBody(HttpStatus.BAD_REQUEST, REWARD_BATCH_NOT_FOUND)));
153-
}
154-
155-
156122
private Criteria buildCombinedCriteria(String merchantId, String status, String assigneeLevel, String month, boolean isOperator) {
157123
List<Criteria> subCriteria = new ArrayList<>();
158124

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

Lines changed: 70 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import it.gov.pagopa.idpay.transactions.connector.rest.UserRestClient;
55
import it.gov.pagopa.idpay.transactions.connector.rest.dto.FiscalCodeInfoPDV;
66
import it.gov.pagopa.idpay.transactions.dto.*;
7+
import it.gov.pagopa.idpay.transactions.dto.batch.BatchCountersDTO;
78
import it.gov.pagopa.idpay.transactions.dto.mapper.RewardTransactionKafkaMapper;
89
import it.gov.pagopa.idpay.transactions.enums.PosType;
910
import it.gov.pagopa.idpay.transactions.enums.RewardBatchTrxStatus;
@@ -191,8 +192,7 @@ private Mono<Void> validateBatchAndUpdateInvoiceFlow(RewardTransaction trx,
191192
validateTrxBatchStatusNotApproved(trx);
192193

193194
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))
196196
.then();
197197
});
198198
}
@@ -250,114 +250,84 @@ private Mono<RewardTransaction> updateInvoiceFileAndFields(RewardTransaction trx
250250
}));
251251
}
252252

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) {
287255

288-
PosType posType = suspendedTrx.getPointOfSaleType();
289-
String businessName = suspendedTrx.getBusinessName();
256+
PosType posType = oldTransaction.getPointOfSaleType();
257+
String businessName = oldTransaction.getBusinessName();
290258

291-
YearMonth now = YearMonth.now();
259+
YearMonth currentMonth = YearMonth.now();
292260
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;
332262

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

266+
private Mono<RewardTransaction> suspendAndMoveTransaction(
267+
RewardTransaction oldTransaction, RewardBatch oldBatch) {
349268

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

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));
359329
}
360-
330+
}
361331

362332
public Mono<Void> reversalTransaction(
363333
String transactionId,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public interface RewardBatchService {
1717
Mono<Page<RewardBatch>> getRewardBatches(String merchantId, String organizationRole, String status, String assigneeLevel, String month, Pageable pageable);
1818
Mono<RewardBatch> incrementTotalAmountCents(String batchId, long accruedAmountCents);
1919
Mono<RewardBatch> decrementTotalAmountCents(String batchId, long accruedAmountCents);
20-
Mono<RewardBatch> moveSuspendToNewBatch(String oldBatchId, String newBatchId, long accruedAmountCents);
2120
Mono<RewardBatch> rewardBatchConfirmation(String initiativeId, String rewardBatchId);
2221

2322
Mono<Void> rewardBatchConfirmationBatch(String initiativeId, List<String> rewardBatchIds);

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,18 @@ private Mono<RewardBatch> createBatch(String merchantId, PosType posType, String
175175
return rewardBatchRepository.save(batch);
176176
}
177177

178+
@Deprecated
178179
@Override
179180
public Mono<RewardBatch> incrementTotalAmountCents(String batchId, long accruedAmountCents) {
180181
return rewardBatchRepository.incrementTotalAmountCents(batchId, accruedAmountCents);
181182
}
182183

184+
@Deprecated
183185
@Override
184186
public Mono<RewardBatch> decrementTotalAmountCents(String batchId, long accruedAmountCents) {
185187
return rewardBatchRepository.decrementTotalAmountCents(batchId, accruedAmountCents);
186188
}
187189

188-
@Override
189-
public Mono<RewardBatch> moveSuspendToNewBatch(String oldBatchId, String newBatchId, long accruedAmountCents) {
190-
return rewardBatchRepository.moveSuspendToNewBatch(oldBatchId, newBatchId, accruedAmountCents);
191-
}
192-
193190
@Override
194191
public Mono<Void> sendRewardBatch(String merchantId, String batchId) {
195192
return rewardBatchRepository.findById(batchId)

src/test/java/it/gov/pagopa/idpay/transactions/repository/RewardBatchSpecificRepositoryImplTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ void moveSuspendToNewBatch_ok_shouldMoveCountersAndAmounts() {
921921
LocalDateTime newUpdateBefore = newBatch.getUpdateDate();
922922

923923
Mono<RewardBatch> result = rewardBatchSpecificRepository
924-
.moveSuspendToNewBatch("batch1", "batch2", accrued);
924+
.moveSuspendFromEvaluatingBatch("batch1", "batch2", accrued);
925925

926926
StepVerifier.create(result)
927927
.assertNext(updatedNew -> {
@@ -958,9 +958,9 @@ void moveSuspendToNewBatch_ok_shouldMoveCountersAndAmounts() {
958958
}
959959

960960
@Test
961-
void moveSuspendToNewBatch_oldBatchNotFound_returnsBadRequest() {
961+
void moveSuspendFromEvaluatingBatch_oldBatchNotFound_returnsBadRequest() {
962962
Mono<RewardBatch> result = rewardBatchSpecificRepository
963-
.moveSuspendToNewBatch("NOT_EXISTING", "batch2", 150L);
963+
.moveSuspendFromEvaluatingBatch("NOT_EXISTING", "batch2", 150L);
964964

965965
StepVerifier.create(result)
966966
.expectErrorSatisfies(ex -> {
@@ -973,7 +973,7 @@ void moveSuspendToNewBatch_oldBatchNotFound_returnsBadRequest() {
973973

974974

975975
@Test
976-
void moveSuspendToNewBatch_newBatchNotFound_returnsBadRequest_andOldWasDecremented() {
976+
void moveSuspendFromEvaluatingBatchNotFound_returnsBadRequest_andOldWasDecremented() {
977977
long accrued = 150L;
978978

979979
RewardBatch oldBatch = rewardBatchRepository.findById("batch1").block();
@@ -987,7 +987,7 @@ void moveSuspendToNewBatch_newBatchNotFound_returnsBadRequest_andOldWasDecrement
987987
rewardBatchRepository.save(oldBatch).block();
988988

989989
Mono<RewardBatch> result = rewardBatchSpecificRepository
990-
.moveSuspendToNewBatch("batch1", "NOT_EXISTING", accrued);
990+
.moveSuspendFromEvaluatingBatch("batch1", "NOT_EXISTING", accrued);
991991

992992
StepVerifier.create(result)
993993
.expectErrorSatisfies(ex -> {

0 commit comments

Comments
 (0)