Skip to content

Commit

Permalink
FINERACT-2081: loan validation code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kjozsa committed Aug 6, 2024
1 parent e967426 commit ac38274
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 352 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static LocalDate getOffSetDateIfNonWorkingDay(final LocalDate date, final
}

public static boolean isWorkingDay(final WorkingDays workingDays, final LocalDate date) {
return CalendarUtils.isValidRedurringDate(workingDays.getRecurrence(), date, date);
return CalendarUtils.isValidRecurringDate(workingDays.getRecurrence(), date, date);
}

public static boolean isNonWorkingDay(final WorkingDays workingDays, final LocalDate date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public boolean isBetweenStartAndEndDate(final LocalDate compareDate) {

public boolean isValidRecurringDate(final LocalDate compareDate, final Boolean isSkipMeetingOnFirstDay, final Integer numberOfDays) {
if (isBetweenStartAndEndDate(compareDate)) {
return CalendarUtils.isValidRedurringDate(this.getRecurrence(), this.getStartDate(), compareDate, isSkipMeetingOnFirstDay,
return CalendarUtils.isValidRecurringDate(this.getRecurrence(), this.getStartDate(), compareDate, isSkipMeetingOnFirstDay,
numberOfDays);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,14 @@ private static String constructRecurrence(final CalendarFrequencyType frequencyT
public boolean isValidRecurringDate(final LocalDate compareDate, Boolean isSkipRepaymentOnFirstMonth, Integer numberOfDays) {

if (isBetweenStartAndEndDate(compareDate)) {
return CalendarUtils.isValidRedurringDate(getRecurrence(), getStartDateLocalDate(), compareDate, isSkipRepaymentOnFirstMonth,
return CalendarUtils.isValidRecurringDate(getRecurrence(), getStartDateLocalDate(), compareDate, isSkipRepaymentOnFirstMonth,
numberOfDays);
}

// validate with history details.
for (CalendarHistory history : history()) {
if (history.isBetweenStartAndEndDate(compareDate)) {
return CalendarUtils.isValidRedurringDate(history.getRecurrence(), history.getStartDate(), compareDate,
return CalendarUtils.isValidRecurringDate(history.getRecurrence(), history.getStartDate(), compareDate,
isSkipRepaymentOnFirstMonth, numberOfDays);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public static String getRRuleReadable(final LocalDate startDate, final String re
return humanReadable;
}

public static boolean isValidRedurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date) {
public static boolean isValidRecurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date) {
final Recur recur = CalendarUtils.getICalRecur(recurringRule);
if (recur == null) {
return false;
Expand All @@ -365,7 +365,7 @@ public static boolean isValidRedurringDate(final String recurringRule, final Loc
return isValidRecurringDate(recur, seedDate, date, isSkipRepaymentonFirstDayOfMonth, numberOfDays);
}

public static boolean isValidRedurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date,
public static boolean isValidRecurringDate(final String recurringRule, final LocalDate seedDate, final LocalDate date,
boolean isSkipRepaymentonFirstDayOfMonth, final Integer numberOfDays) {

final Recur recur = CalendarUtils.getICalRecur(recurringRule);
Expand Down Expand Up @@ -529,7 +529,7 @@ public static LocalDate getFirstRepaymentMeetingDate(final Calendar calendar, fi
}
LocalDate startDate = disbursementDate;
final LocalDate seedDate = calendar.getStartDateLocalDate();
if (isValidRedurringDate(calendar.getRecurrence(), seedDate, startDate, isSkipRepaymentOnFirstDayOfMonth, numberOfDays)
if (isValidRecurringDate(calendar.getRecurrence(), seedDate, startDate, isSkipRepaymentOnFirstDayOfMonth, numberOfDays)
&& !frequency.equals(Recur.Frequency.DAILY.name())) {
startDate = startDate.plusDays(1);
}
Expand Down Expand Up @@ -728,10 +728,6 @@ public static LocalDate getNextScheduleDate(final Calendar calendar, final Local
return null;
}
final LocalDate seedDate = calendar.getStartDateLocalDate();
/**
* if (isValidRedurringDate(calendar.getRecurrence(), seedDate, date)) { date = date.plusDays(1); }
**/

return getNextRecurringDate(recur, seedDate, startDate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ public BigDecimal calculatePeriodsBetweenDates(final LocalDate startDate, final
this.repaymentPeriodFrequencyType);
} else {
LocalDate expectedStartDate = startDate;
if (!CalendarUtils.isValidRedurringDate(loanCalendar.getRecurrence(),
if (!CalendarUtils.isValidRecurringDate(loanCalendar.getRecurrence(),
loanCalendar.getStartDateLocalDate().minusMonths(getRepaymentEvery()), startDate)) {
expectedStartDate = CalendarUtils.getNewRepaymentMeetingDate(loanCalendar.getRecurrence(),
startDate.minusMonths(getRepaymentEvery()), startDate.minusMonths(getRepaymentEvery()), getRepaymentEvery(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ private List<DisbursementData> fetchDisbursementData(final JsonObject command) {

private void validateRepaymentsStartDateWithMeetingDates(final LocalDate repaymentsStartingFromDate, final Calendar calendar,
boolean isSkipRepaymentOnFirstDayOfMonth, final Integer numberOfDays) {
if (repaymentsStartingFromDate != null && !CalendarUtils.isValidRedurringDate(calendar.getRecurrence(),
if (repaymentsStartingFromDate != null && !CalendarUtils.isValidRecurringDate(calendar.getRecurrence(),
calendar.getStartDateLocalDate(), repaymentsStartingFromDate, isSkipRepaymentOnFirstDayOfMonth, numberOfDays)) {
final String errorMessage = "First repayment date '" + repaymentsStartingFromDate + "' do not fall on a meeting date";
throw new LoanApplicationDateException("first.repayment.date.do.not.match.meeting.date", errorMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,6 @@ private void validateTransactionProcessingStrategy(final String transactionProce
}

public void checkForProductMixRestrictions(final JsonElement element) {

final List<Long> activeLoansLoanProductIds;
final Long productId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.productIdParameterName, element);
final Long groupId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.groupIdParameterName, element);
Expand Down Expand Up @@ -1863,17 +1862,17 @@ private void validateSubmittedOnDate(final JsonElement element, LocalDate origin
Group group = groupRepository.findOneWithNotFoundDetection(groupId);

if (group != null && group.isActivatedAfter(submittedOnDate)) {
final String errorMessage = "The date on which a loan is submitted cannot be earlier than groups's activation date.";
final String errorMessage = "The date on which a loan is submitted cannot be earlier than group's activation date.";
throw new InvalidLoanStateTransitionException("submittal", "cannot.be.before.group.activation.date", errorMessage,
submittedOnDate, group.getActivationDate());
}
}

if (DateUtils.isAfter(submittedOnDate, expectedDisbursementDate)) {
final String errorMessage = "The date on which a loan is submitted cannot be after its expected disbursement date: "
+ expectedDisbursementDate;
throw new InvalidLoanStateTransitionException("submittal", "cannot.be.after.expected.disbursement.date", errorMessage,
submittedOnDate, expectedDisbursementDate);
}
if (DateUtils.isAfter(submittedOnDate, expectedDisbursementDate)) {
final String errorMessage = "The date on which a loan is submitted cannot be after its expected disbursement date: "
+ expectedDisbursementDate;
throw new InvalidLoanStateTransitionException("submittal", "cannot.be.after.expected.disbursement.date", errorMessage,
submittedOnDate, expectedDisbursementDate);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public void validateRepaymentDateWithMeetingDate(final LocalDate repaymentDate,
final Calendar calendar = calendarInstance.getCalendar();
if (calendar != null && repaymentDate != null) {
// Disbursement date should fall on a meeting date
if (!CalendarUtils.isValidRedurringDate(calendar.getRecurrence(), calendar.getStartDateLocalDate(), repaymentDate)) {
if (!CalendarUtils.isValidRecurringDate(calendar.getRecurrence(), calendar.getStartDateLocalDate(), repaymentDate)) {
final String errorMessage = "Transaction date '" + repaymentDate.toString() + "' does not fall on a meeting date.";
throw new NotValidRecurringDateException("loan.transaction.date", errorMessage, repaymentDate.toString(),
calendar.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1942,11 +1942,8 @@ private void validateChargeToIncomeAccountMappings(final DataValidatorBuilder ba

public void validateMinMaxConstraints(final JsonElement element, final DataValidatorBuilder baseDataValidator,
final LoanProduct loanProduct) {

validatePrincipalMinMaxConstraint(element, loanProduct, baseDataValidator);

validateNumberOfRepaymentsMinMaxConstraint(element, loanProduct, baseDataValidator);

validateNominalInterestRatePerPeriodMinMaxConstraint(element, loanProduct, baseDataValidator);
}

Expand Down Expand Up @@ -2029,7 +2026,6 @@ public void validateMinMaxConstraints(final JsonElement element, final DataValid

private void validatePrincipalMinMaxConstraint(final JsonElement element, final LoanProduct loanProduct,
final DataValidatorBuilder baseDataValidator) {

boolean principalUpdated = false;
boolean minPrincipalUpdated = false;
boolean maxPrincipalUpdated = false;
Expand Down Expand Up @@ -2555,11 +2551,7 @@ public void validateRepaymentPeriodWithGraceSettings(final Integer numberOfRepay
}
}

private Integer defaultToZeroIfNull(final Integer value) {
Integer result = value;
if (value == null) {
result = 0;
}
return result;
private Integer defaultToZeroIfNull(Integer value) {
return value != null ? value : 0;
}
}

0 comments on commit ac38274

Please sign in to comment.