Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5352,11 +5352,39 @@ private static List<PaymentAllocationOrder> getPaymentAllocationOrder(
private PostLoanProductsResponse createLoanProductIdempotent(PostLoanProductsRequest loanProductRequest) {
String productName = loanProductRequest.getName();
log.debug("Attempting to create loan product: {}", productName);

// First, check if product already exists
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change: it has nothing to do with linked story and PR description.

Please remove and raise a new PR with this.

PostLoanProductsResponse existingResponse = findExistingProduct(productName);
if (existingResponse != null) {
return existingResponse;
}

// Try to create
log.debug("Creating new loan product: {}", productName);
try {
PostLoanProductsResponse response = ok(() -> fineractClient.loanProducts().createLoanProduct(loanProductRequest, Map.of()));
log.debug("Successfully created loan product '{}' with ID: {}", productName, response.getResourceId());
return response;
} catch (Exception e) {
// On failure (e.g., race condition with parallel thread or shortName collision from previous run),
// re-check if the product now exists before propagating the error
log.warn("Failed to create loan product '{}', re-checking if it was created concurrently", productName, e);
PostLoanProductsResponse retryResponse = findExistingProduct(productName);
if (retryResponse != null) {
log.info("Loan product '{}' found on retry with ID: {}", productName, retryResponse.getResourceId());
return retryResponse;
}
// If still not found, propagate the original error
log.error("FAILED to create loan product '{}' and it doesn't exist in database", productName);
throw e;
}
}

private PostLoanProductsResponse findExistingProduct(String productName) {
try {
List<GetLoanProductsResponse> existingProducts = fineractClient.loanProducts().retrieveAllLoanProducts(Map.of());
GetLoanProductsResponse existingProduct = existingProducts.stream().filter(p -> productName.equals(p.getName())).findFirst()
.orElse(null);
Comment on lines 5385 to 5387

if (existingProduct != null) {
log.debug("Loan product '{}' already exists with ID: {}", productName, existingProduct.getId());
PostLoanProductsResponse response = new PostLoanProductsResponse();
Expand All @@ -5366,15 +5394,6 @@ private PostLoanProductsResponse createLoanProductIdempotent(PostLoanProductsReq
} catch (Exception e) {
log.warn("Error checking if loan product '{}' exists", productName, e);
}

log.debug("Creating new loan product: {}", productName);
try {
PostLoanProductsResponse response = ok(() -> fineractClient.loanProducts().createLoanProduct(loanProductRequest, Map.of()));
log.debug("Successfully created loan product '{}' with ID: {}", productName, response.getResourceId());
return response;
} catch (Exception e) {
log.error("FAILED to create loan product '{}'", productName, e);
throw e;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,14 @@ public SavingsDTO populateSavingsDtoFromMap(final Map<String, Object> accounting
}
}

if (!isAccountTransfer) {
isAccountTransfer = this.accountTransfersReadPlatformService.isAccountTransfer(Long.parseLong(transactionId),
boolean localIsAccountTransfer = isAccountTransfer;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaningless change...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it help to copy into a new local variable?

if (!localIsAccountTransfer) {
localIsAccountTransfer = this.accountTransfersReadPlatformService.isAccountTransfer(Long.parseLong(transactionId),
PortfolioAccountType.SAVINGS);
}
Comment thread
a7med3del1973 marked this conversation as resolved.
final SavingsTransactionDTO transaction = new SavingsTransactionDTO(transactionOfficeId, paymentTypeId, transactionId,
transactionDate, transactionType, amount, reversed, feePayments, penaltyPayments, overdraftAmount, isAccountTransfer,
taxPayments);
transactionDate, transactionType, amount, reversed, feePayments, penaltyPayments, overdraftAmount,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning less change...

localIsAccountTransfer, taxPayments);
Comment on lines +264 to +271

newSavingsTransactions.add(transaction);

Expand Down
Loading