Skip to content

Commit

Permalink
FINERACT-2152: fix overlapping for update interest pause
Browse files Browse the repository at this point in the history
  • Loading branch information
kulminsky authored and adamsaghy committed Jan 21, 2025
1 parent 5729af5 commit 9a8db7e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,13 @@ private CommandProcessingResult processUpdateInterestPause(Loan loan, Long varia
LocalDate endDate = parseDate(endDateString, dateFormat, locale);

validateActiveLoan(loan);
validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale);

LoanTermVariations variation = loanTermVariationsRepository
.findByIdAndLoanIdAndTermType(variationId, loan.getId(), INTEREST_PAUSE.getValue())
.orElseThrow(() -> new GeneralPlatformDomainRuleException("error.msg.variation.not.found",
"Variation not found for the given loan ID"));

validateVariations(loan, variation);
validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale, variation.getId());

variation.setTermApplicableFrom(startDate);
variation.setDateValue(endDate);
Expand All @@ -135,7 +134,7 @@ private CommandProcessingResult processUpdateInterestPause(Loan loan, Long varia
private CommandProcessingResult processInterestPause(Loan loan, LocalDate startDate, LocalDate endDate, String dateFormat,
String locale) {
validateActiveLoan(loan);
validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale);
validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale, null);

LoanTermVariations variation = new LoanTermVariations(INTEREST_PAUSE.getValue(), startDate, null, endDate, false, loan);

Expand All @@ -144,7 +143,8 @@ private CommandProcessingResult processInterestPause(Loan loan, LocalDate startD
return new CommandProcessingResultBuilder().withEntityId(savedVariation.getId()).build();
}

private void validateInterestPauseDates(Loan loan, LocalDate startDate, LocalDate endDate, String dateFormat, String locale) {
private void validateInterestPauseDates(Loan loan, LocalDate startDate, LocalDate endDate, String dateFormat, String locale,
Long currentVariationId) {

validateOrThrow(baseDataValidator -> {
baseDataValidator.reset().parameter("startDate").value(startDate).notBlank();
Expand Down Expand Up @@ -182,11 +182,15 @@ private void validateInterestPauseDates(Loan loan, LocalDate startDate, LocalDat
"Interest pause is only supported for loans with recalculate interest enabled.");
}

for (LoanTermVariations existingVariation : loan.getLoanTermVariations()) {
if (Objects.equals(existingVariation.getTermType().getValue(), INTEREST_PAUSE.getValue())) {
if (!(endDate.isBefore(existingVariation.getTermApplicableFrom()) || startDate.isAfter(existingVariation.getDateValue()))) {
throw new GeneralPlatformDomainRuleException("interest.pause.overlapping",
"Overlapping interest pauses are not allowed.");
List<LoanTermVariations> existingVariations = loan.getLoanTermVariations();
for (LoanTermVariations existingVariation : existingVariations) {
if (currentVariationId == null || !existingVariation.getId().equals(currentVariationId)) {
if (Objects.equals(existingVariation.getTermType().getValue(), INTEREST_PAUSE.getValue())) {
if (!(endDate.isBefore(existingVariation.getTermApplicableFrom())
|| startDate.isAfter(existingVariation.getDateValue()))) {
throw new GeneralPlatformDomainRuleException("interest.pause.overlapping",
"Overlapping interest pauses are not allowed.");
}
}
}
}
Expand All @@ -199,25 +203,6 @@ private void validateActiveLoan(Loan loan) {
}
}

private void validateVariations(Loan loan, LoanTermVariations variation) {
if (variation == null) {
throw new GeneralPlatformDomainRuleException("interest.pause.not.found",
"The specified interest pause does not exist for the given loan.");
}

List<LoanTermVariations> existingVariations = loan.getLoanTermVariations();
for (LoanTermVariations existingVariation : existingVariations) {
if (!existingVariation.equals(variation)
&& Objects.equals(existingVariation.getTermType().getValue(), INTEREST_PAUSE.getValue())) {
if (!(variation.getDateValue().isBefore(existingVariation.getTermApplicableFrom())
|| variation.getTermApplicableFrom().isAfter(existingVariation.getDateValue()))) {
throw new GeneralPlatformDomainRuleException("interest.pause.overlapping",
"Overlapping interest pauses are not allowed.");
}
}
}
}

private LocalDate parseDate(String date, String dateFormat, String locale) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat, Locale.forLanguageTag(locale));
Expand Down
Loading

0 comments on commit 9a8db7e

Please sign in to comment.