Skip to content

Commit

Permalink
FINERACT-2081: Validation error in mandatory loan account parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Alberto Hernandez committed Sep 5, 2024
1 parent a6e3517 commit d6d3a39
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1056,4 +1056,12 @@ public void throwValidationErrors() throws PlatformApiDataValidationException {
throw new PlatformApiDataValidationException(dataValidationErrors);
}
}

public static ApiParameterError buildValidationParameterApiError(final String resource, final String parameterName,
final String errorCode, final String errorMessage, final Object... defaultUserMessageArgs) {
final String validationErrorCode = "validation.msg." + resource + "." + parameterName + errorCode;
String defaultEnglishMessage = "The parameter `" + parameterName + "` " + errorMessage;
return ApiParameterError.parameterError(validationErrorCode, defaultEnglishMessage, parameterName, defaultUserMessageArgs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ private void validateForCreate(final JsonElement element) {
boolean isMeetingMandatoryForJLGLoans = configurationDomainService.isMeetingMandatoryForJLGLoans();

final Long productId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.productIdParameterName, element);
if (productId == null) {
throwMandatoryParameterError(LoanApiConstants.productIdParameterName);
}
final LoanProduct loanProduct = this.loanProductRepository.findById(productId)
.orElseThrow(() -> new LoanProductNotFoundException(productId));

Expand Down Expand Up @@ -466,6 +469,9 @@ private void validateForCreate(final JsonElement element) {
.extractLocalDateNamed(LoanApiConstants.expectedDisbursementDateParameterName, element);
baseDataValidator.reset().parameter(LoanApiConstants.expectedDisbursementDateParameterName).value(expectedDisbursementDate)
.notNull();
if (expectedDisbursementDate == null) {
return;
}

// grace validation
final Integer graceOnPrincipalPayment = this.fromApiJsonHelper
Expand Down Expand Up @@ -511,8 +517,10 @@ private void validateForCreate(final JsonElement element) {

final LocalDate submittedOnDate = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.submittedOnDateParameterName,
element);

baseDataValidator.reset().parameter(LoanApiConstants.submittedOnDateParameterName).value(submittedOnDate).notNull();
if (submittedOnDate == null) {
return;
}

if (this.fromApiJsonHelper.parameterExists(LoanApiConstants.submittedOnNoteParameterName, element)) {
final String submittedOnNote = this.fromApiJsonHelper.extractStringNamed(LoanApiConstants.submittedOnNoteParameterName,
Expand All @@ -523,7 +531,11 @@ private void validateForCreate(final JsonElement element) {

final String transactionProcessingStrategy = this.fromApiJsonHelper
.extractStringNamed(LoanApiConstants.transactionProcessingStrategyCodeParameterName, element);

baseDataValidator.reset().parameter(LoanApiConstants.transactionProcessingStrategyCodeParameterName)
.value(transactionProcessingStrategy).notNull();
if (transactionProcessingStrategy == null) {
return;
}
validateTransactionProcessingStrategy(transactionProcessingStrategy, loanProduct, baseDataValidator);

validateLinkedSavingsAccount(element, baseDataValidator);
Expand Down Expand Up @@ -749,7 +761,10 @@ private void validateForCreate(final JsonElement element) {
validateCollateral(element);
// validate if disbursement date is a holiday or a non-working day
validateDisbursementDateIsOnNonWorkingDay(expectedDisbursementDate);
Long officeId = client != null ? client.getOffice().getId() : group.getOffice().getId();
Long officeId = setOfficeId(client, group);
if (officeId == null) {
return;
}
validateDisbursementDateIsOnHoliday(expectedDisbursementDate, officeId);
final Integer recurringMoratoriumOnPrincipalPeriods = this.fromApiJsonHelper
.extractIntegerWithLocaleNamed("recurringMoratoriumOnPrincipalPeriods", element);
Expand Down Expand Up @@ -927,6 +942,9 @@ public void validateForModify(final JsonCommand command, final Loan loan) {
.extractStringNamed(LoanApiConstants.transactionProcessingStrategyCodeParameterName, element);
baseDataValidator.reset().parameter(LoanApiConstants.transactionProcessingStrategyCodeParameterName)
.value(transactionProcessingStrategy).notNull();
if (transactionProcessingStrategy == null) {
return;
}
// Validating whether the processor is existing
validateTransactionProcessingStrategy(transactionProcessingStrategy, loanProduct, baseDataValidator);
}
Expand Down Expand Up @@ -1114,6 +1132,9 @@ public void validateForModify(final JsonCommand command, final Loan loan) {
.extractLocalDateNamed(LoanApiConstants.expectedDisbursementDateParameterName, element);
baseDataValidator.reset().parameter(LoanApiConstants.expectedDisbursementDateParameterName).value(expectedDisbursementDate)
.notNull();
if (expectedDisbursementDate == null) {
return;
}
}

Integer graceOnPrincipalPayment = loan.getLoanProductRelatedDetail().getGraceOnPrincipalPayment();
Expand Down Expand Up @@ -1410,7 +1431,10 @@ public void validateForModify(final JsonCommand command, final Loan loan) {

// validate if disbursement date is a holiday or a non-working day
validateDisbursementDateIsOnNonWorkingDay(expectedDisbursementDate);
Long officeId = client != null ? client.getOffice().getId() : group.getOffice().getId();
final Long officeId = setOfficeId(client, group);
if (officeId == null) {
return;
}
validateDisbursementDateIsOnHoliday(expectedDisbursementDate, officeId);

Integer recurringMoratoriumOnPrincipalPeriods = loan.getLoanProductRelatedDetail().getRecurringMoratoriumOnPrincipalPeriods();
Expand Down Expand Up @@ -1750,9 +1774,6 @@ private void officeSpecificLoanProductValidation(final Long productId, final Lon
private void validateTransactionProcessingStrategy(final String transactionProcessingStrategy, final LoanProduct loanProduct,
final DataValidatorBuilder baseDataValidator) {

baseDataValidator.reset().parameter(LoanApiConstants.transactionProcessingStrategyCodeParameterName)
.value(transactionProcessingStrategy).notNull();

// TODO: Review exceptions
if (!AdvancedPaymentScheduleTransactionProcessor.ADVANCED_PAYMENT_ALLOCATION_STRATEGY
.equals(loanProduct.getTransactionProcessingStrategyCode())
Expand Down Expand Up @@ -2127,4 +2148,22 @@ public static void validateOrThrow(String resource, Consumer<DataValidatorBuilde
dataValidationErrors);
}
}

private Long setOfficeId(Client client, Group group) {
if (client != null) {
return client.getOffice().getId();
}
if (group != null) {
return group.getOffice().getId();
}
return null;
}

private void throwMandatoryParameterError(final String parameterName) {
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
dataValidationErrors
.add(DataValidatorBuilder.buildValidationParameterApiError("loans", parameterName, ".cannot.be.blank", "is mandatory.", 0));
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.",
dataValidationErrors);
}
}

0 comments on commit d6d3a39

Please sign in to comment.