FINERACT-1971: update and delete interest pause by external id

This commit is contained in:
Andrii Kulminskyi
2025-01-20 09:53:02 +01:00
committed by Adam Saghy
parent 001797b97a
commit 9258be777c
9 changed files with 443 additions and 37 deletions
@@ -20,6 +20,9 @@ package org.apache.fineract.portfolio.interestpauses.api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@@ -64,7 +67,8 @@ public class LoanInterestPauseApiResource {
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Create a new interest pause period for a loan", description = "Allows users to define a period during which no interest will be accrued for a specific loan.")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK") })
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Command successfully processed", content = @Content(schema = @Schema(implementation = CommandProcessingResult.class))) })
public CommandProcessingResult createInterestPause(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId,
@RequestBody(required = true) final InterestPauseRequestDto request) {
@@ -80,7 +84,8 @@ public class LoanInterestPauseApiResource {
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Create a new interest pause for a loan using external ID", description = "Allows users to define a period during which no interest will be accrued for a specific loan using the external loan ID.")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK") })
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Command successfully processed", content = @Content(schema = @Schema(implementation = CommandProcessingResult.class))) })
public CommandProcessingResult createInterestPauseByExternalId(
@PathParam("loanExternalId") @Parameter(description = "loanExternalId") final String loanExternalId,
@RequestBody(required = true) final InterestPauseRequestDto request) {
@@ -97,7 +102,8 @@ public class LoanInterestPauseApiResource {
@Path("/{loanId}/interest-pauses")
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve all interest pause periods for a loan", description = "Fetches a list of all active interest pause periods for a specific loan.")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK") })
@ApiResponses({
@ApiResponse(responseCode = "200", description = "List of interest pause periods", content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterestPauseResponseDto.class)))) })
public List<InterestPauseResponseDto> retrieveInterestPauses(
@PathParam("loanId") @Parameter(description = "loanId") final Long loanId) {
@@ -110,7 +116,8 @@ public class LoanInterestPauseApiResource {
@Path("/external-id/{loanExternalId}/interest-pauses")
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve all interest pause periods for a loan using external ID", description = "Fetches a list of all active interest pause periods for a specific loan using the external loan ID.")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK") })
@ApiResponses({
@ApiResponse(responseCode = "200", description = "List of interest pause periods", content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterestPauseResponseDto.class)))) })
public List<InterestPauseResponseDto> retrieveInterestPausesByExternalId(
@PathParam("loanExternalId") @Parameter(description = "loanExternalId") final String loanExternalId) {
@@ -135,12 +142,30 @@ public class LoanInterestPauseApiResource {
return Response.noContent().build();
}
@DELETE
@Path("/external-id/{loanExternalId}/interest-pauses/{variationId}")
@Operation(summary = "Delete an interest pause period by external id", description = "Deletes a specific interest pause period by its variation ID.")
@ApiResponses({ @ApiResponse(responseCode = "204", description = "No Content") })
public Response deleteInterestPauseByExternalId(
@PathParam("loanExternalId") @Parameter(description = "loanExternalId") final String loanExternalId,
@PathParam("variationId") @Parameter(description = "variationId") final Long variationId) {
this.context.authenticatedUser().validateHasReadPermission(MODIFY_RESOURCE_NAME_FOR_PERMISSIONS);
final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteInterestPause(loanExternalId, variationId).build();
this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
return Response.noContent().build();
}
@PUT
@Path("/{loanId}/interest-pauses/{variationId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Update an interest pause period", description = "Updates a specific interest pause period by its variation ID.")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK") })
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Command successfully processed", content = @Content(schema = @Schema(implementation = CommandProcessingResult.class))) })
public CommandProcessingResult updateInterestPause(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId,
@PathParam("variationId") @Parameter(description = "variationId") final Long variationId,
@RequestBody(required = true) final InterestPauseRequestDto request) {
@@ -152,4 +177,24 @@ public class LoanInterestPauseApiResource {
return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
}
@PUT
@Path("/external-id/{loanExternalId}/interest-pauses/{variationId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Update an interest pause period by external id", description = "Updates a specific interest pause period by its variation ID.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Command successfully processed", content = @Content(schema = @Schema(implementation = CommandProcessingResult.class))) })
public CommandProcessingResult updateInterestPauseByExternalId(
@PathParam("loanExternalId") @Parameter(description = "loanExternalId") final String loanExternalId,
@PathParam("variationId") @Parameter(description = "variationId") final Long variationId,
@RequestBody(required = true) final InterestPauseRequestDto request) {
this.context.authenticatedUser().validateHasReadPermission(MODIFY_RESOURCE_NAME_FOR_PERMISSIONS);
final CommandWrapper commandRequest = new CommandWrapperBuilder().updateInterestPause(loanExternalId, variationId)
.withJson(request.toJson()).build();
return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
}
}
@@ -22,6 +22,8 @@ import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.handler.NewCommandSourceHandler;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.portfolio.interestpauses.service.InterestPauseWritePlatformService;
import org.springframework.stereotype.Component;
@@ -34,8 +36,16 @@ public class DeleteInterestPauseCommandHandler implements NewCommandSourceHandle
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
final Long loanId = command.getLoanId();
final ExternalId loanExternalId = command.getLoanExternalId();
final Long termVariationId = command.getResourceId();
return interestPauseService.deleteInterestPause(loanId, termVariationId);
if (loanId != null) {
return interestPauseService.deleteInterestPause(loanId, termVariationId);
} else if (loanExternalId != null) {
return interestPauseService.deleteInterestPause(loanExternalId, termVariationId);
} else {
throw new PlatformApiDataValidationException("validation.msg.missing.loan.id.or.external.id",
"Either loanId or loanExternalId must be provided.", "loanId");
}
}
}
@@ -22,6 +22,8 @@ import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.handler.NewCommandSourceHandler;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.portfolio.interestpauses.service.InterestPauseWritePlatformService;
import org.springframework.stereotype.Component;
@@ -34,12 +36,20 @@ public class UpdateInterestPauseCommandHandler implements NewCommandSourceHandle
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
final Long loanId = command.getLoanId();
final ExternalId loanExternalId = command.getLoanExternalId();
final Long termVariationId = command.getResourceId();
final String startDate = command.stringValueOfParameterNamed("startDate");
final String endDate = command.stringValueOfParameterNamed("endDate");
final String dateFormat = command.stringValueOfParameterNamed("dateFormat");
final String locale = command.stringValueOfParameterNamed("locale");
return interestPauseService.updateInterestPause(loanId, termVariationId, startDate, endDate, dateFormat, locale);
if (loanId != null) {
return interestPauseService.updateInterestPause(loanId, termVariationId, startDate, endDate, dateFormat, locale);
} else if (loanExternalId != null) {
return interestPauseService.updateInterestPause(loanExternalId, termVariationId, startDate, endDate, dateFormat, locale);
} else {
throw new PlatformApiDataValidationException("validation.msg.missing.loan.id.or.external.id",
"Either loanId or loanExternalId must be provided.", "loanId");
}
}
}
@@ -69,6 +69,17 @@ public interface InterestPauseWritePlatformService {
*/
CommandProcessingResult deleteInterestPause(Long loanId, Long variationId);
/**
* Delete an existing interest pause period for a loan.
*
* @param loanExternalId
* the external ID of the loan
* @param variationId
* the ID of the loan term variation representing the interest pause
* @return the result of the delete operation
*/
CommandProcessingResult deleteInterestPause(ExternalId loanExternalId, Long variationId);
/**
* Update an existing interest pause period for a loan identified by its internal ID.
*
@@ -88,4 +99,24 @@ public interface InterestPauseWritePlatformService {
*/
CommandProcessingResult updateInterestPause(Long loanId, Long variationId, String startDateString, String endDateString,
String dateFormat, String locale);
/**
* Update an existing interest pause period for a loan identified by its internal ID.
*
* @param loanExternalId
* the external ID of the loan
* @param variationId
* the ID of the loan term variation representing the interest pause to be updated
* @param startDateString
* the new start date of the interest pause period (inclusive) as a string
* @param endDateString
* the new end date of the interest pause period (inclusive) as a string
* @param dateFormat
* the format of the provided dates (e.g., "yyyy-MM-dd")
* @param locale
* the locale used for parsing the provided dates
* @return the updated loan term variation ID along with the updated fields
*/
CommandProcessingResult updateInterestPause(ExternalId loanExternalId, Long variationId, String startDateString, String endDateString,
String dateFormat, String locale);
}
@@ -31,7 +31,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
@@ -40,7 +39,6 @@ import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.infrastructure.core.exception.GeneralPlatformDomainRuleException;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper;
import org.apache.fineract.portfolio.loanaccount.domain.LoanTermVariations;
@@ -53,7 +51,6 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
private final LoanTermVariationsRepository loanTermVariationsRepository;
private final LoanRepositoryWrapper loanRepositoryWrapper;
private final PlatformSecurityContext context;
@Override
public CommandProcessingResult createInterestPause(ExternalId loanExternalId, String startDateString, String endDateString,
@@ -61,8 +58,8 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
final LocalDate startDate = parseDate(startDateString, dateFormat, locale);
final LocalDate endDate = parseDate(endDateString, dateFormat, locale);
return processInterestPause(() -> loanRepositoryWrapper.findOneWithNotFoundDetection(loanExternalId), startDate, endDate,
dateFormat, locale);
return processInterestPause(loanRepositoryWrapper.findOneWithNotFoundDetection(loanExternalId), startDate, endDate, dateFormat,
locale);
}
@Override
@@ -71,14 +68,38 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
final LocalDate startDate = parseDate(startDateString, dateFormat, locale);
final LocalDate endDate = parseDate(endDateString, dateFormat, locale);
return processInterestPause(() -> loanRepositoryWrapper.findOneWithNotFoundDetection(loanId), startDate, endDate, dateFormat,
locale);
return processInterestPause(loanRepositoryWrapper.findOneWithNotFoundDetection(loanId), startDate, endDate, dateFormat, locale);
}
@Override
public CommandProcessingResult deleteInterestPause(ExternalId loanExternalId, Long variationId) {
return processDeleteInterestPause(loanRepositoryWrapper.findOneWithNotFoundDetection(loanExternalId), variationId);
}
@Override
public CommandProcessingResult deleteInterestPause(Long loanId, Long variationId) {
return processDeleteInterestPause(loanRepositoryWrapper.findOneWithNotFoundDetection(loanId), variationId);
}
@Override
public CommandProcessingResult updateInterestPause(ExternalId loanExternalId, Long variationId, String startDateString,
String endDateString, String dateFormat, String locale) {
return processUpdateInterestPause(loanRepositoryWrapper.findOneWithNotFoundDetection(loanExternalId), variationId, startDateString,
endDateString, dateFormat, locale);
}
@Override
public CommandProcessingResult updateInterestPause(Long loanId, Long variationId, String startDateString, String endDateString,
String dateFormat, String locale) {
return processUpdateInterestPause(loanRepositoryWrapper.findOneWithNotFoundDetection(loanId), variationId, startDateString,
endDateString, dateFormat, locale);
}
private CommandProcessingResult processDeleteInterestPause(Loan loan, Long variationId) {
validateActiveLoan(loan);
LoanTermVariations variation = loanTermVariationsRepository
.findByIdAndLoanIdAndTermType(variationId, loanId, INTEREST_PAUSE.getValue())
.findByIdAndLoanIdAndTermType(variationId, loan.getId(), INTEREST_PAUSE.getValue())
.orElseThrow(() -> new GeneralPlatformDomainRuleException("error.msg.variation.not.found",
"Variation not found for the given loan ID"));
@@ -87,18 +108,16 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
return new CommandProcessingResultBuilder().withEntityId(variationId).build();
}
@Override
public CommandProcessingResult updateInterestPause(Long loanId, Long variationId, String startDateString, String endDateString,
private CommandProcessingResult processUpdateInterestPause(Loan loan, Long variationId, String startDateString, String endDateString,
String dateFormat, String locale) {
Loan loan = loanRepositoryWrapper.findOneWithNotFoundDetection(loanId);
LocalDate startDate = parseDate(startDateString, dateFormat, locale);
LocalDate endDate = parseDate(endDateString, dateFormat, locale);
validateActiveLoan(loan);
validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale);
LoanTermVariations variation = loanTermVariationsRepository
.findByIdAndLoanIdAndTermType(variationId, loanId, INTEREST_PAUSE.getValue())
.findByIdAndLoanIdAndTermType(variationId, loan.getId(), INTEREST_PAUSE.getValue())
.orElseThrow(() -> new GeneralPlatformDomainRuleException("error.msg.variation.not.found",
"Variation not found for the given loan ID"));
@@ -113,10 +132,9 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
.with(Map.of("startDate", startDate.toString(), "endDate", endDate.toString())).build();
}
private CommandProcessingResult processInterestPause(Supplier<Loan> loanSupplier, LocalDate startDate, LocalDate endDate,
String dateFormat, String locale) {
final Loan loan = loanSupplier.get();
private CommandProcessingResult processInterestPause(Loan loan, LocalDate startDate, LocalDate endDate, String dateFormat,
String locale) {
validateActiveLoan(loan);
validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale);
LoanTermVariations variation = new LoanTermVariations(INTEREST_PAUSE.getValue(), startDate, null, endDate, false, loan);
@@ -154,11 +172,6 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
endDate, startDate);
}
if (!Objects.equals(loan.getLoanStatus(), ACTIVE.getValue())) {
throw new GeneralPlatformDomainRuleException("loan.must.be.active",
"Operations on interest pauses are restricted to active loans.");
}
if (!PROGRESSIVE.equals(loan.getLoanRepaymentScheduleDetail().getLoanScheduleType())) {
throw new GeneralPlatformDomainRuleException("loan.must.be.progressive",
"Interest pause is only supported for progressive loans.");
@@ -168,6 +181,22 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
throw new GeneralPlatformDomainRuleException("loan.must.have.recalculate.interest.enabled",
"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.");
}
}
}
}
private void validateActiveLoan(Loan loan) {
if (!Objects.equals(loan.getLoanStatus(), ACTIVE.getValue())) {
throw new GeneralPlatformDomainRuleException("loan.must.be.active",
"Operations on interest pauses are restricted to active loans.");
}
}
private void validateVariations(Loan loan, LoanTermVariations variation) {
@@ -178,9 +207,13 @@ public class InterestPauseWritePlatformServiceImpl implements InterestPauseWrite
List<LoanTermVariations> existingVariations = loan.getLoanTermVariations();
for (LoanTermVariations existingVariation : existingVariations) {
if (!existingVariation.equals(variation) && variation.getTermApplicableFrom().isBefore(existingVariation.getDateValue())
&& variation.getDateValue().isAfter(existingVariation.getTermApplicableFrom())) {
throw new GeneralPlatformDomainRuleException("interest.pause.overlapping", "Overlapping interest pauses are not allowed.");
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.");
}
}
}
}