FINERACT-2169: Refactor loan COB catch up api controller;

This commit is contained in:
viktorpavlenko
2025-03-25 12:11:29 +01:00
committed by Adam Saghy
parent 20195464c9
commit b646ba6ca7
4 changed files with 41 additions and 60 deletions
@@ -26,9 +26,9 @@ import java.io.IOException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.client.models.GetOldestCOBProcessedLoanResponse;
import org.apache.fineract.client.models.LoanAccountLock; import org.apache.fineract.client.models.LoanAccountLock;
import org.apache.fineract.client.models.LoanAccountLockResponseDTO; import org.apache.fineract.client.models.LoanAccountLockResponseDTO;
import org.apache.fineract.client.models.OldestCOBProcessedLoanDTO;
import org.apache.fineract.client.models.PostLoansResponse; import org.apache.fineract.client.models.PostLoansResponse;
import org.apache.fineract.client.services.DefaultApi; import org.apache.fineract.client.services.DefaultApi;
import org.apache.fineract.client.services.LoanAccountLockApi; import org.apache.fineract.client.services.LoanAccountLockApi;
@@ -54,7 +54,7 @@ public class LoanCOBStepDef extends AbstractStepDef {
@Then("The cobProcessedDate of the oldest loan processed by COB is more than 1 day earlier than cobBusinessDate") @Then("The cobProcessedDate of the oldest loan processed by COB is more than 1 day earlier than cobBusinessDate")
public void checkOldestCOBProcessed() throws IOException { public void checkOldestCOBProcessed() throws IOException {
Response<GetOldestCOBProcessedLoanResponse> response = loanCobCatchUpApi.getOldestCOBProcessedLoan().execute(); Response<OldestCOBProcessedLoanDTO> response = loanCobCatchUpApi.getOldestCOBProcessedLoan().execute();
ErrorHelper.checkSuccessfulApiCall(response); ErrorHelper.checkSuccessfulApiCall(response);
LocalDate cobDate = response.body().getCobBusinessDate(); LocalDate cobDate = response.body().getCobBusinessDate();
@@ -79,7 +79,7 @@ import org.apache.fineract.client.models.GetLoansLoanIdTransactions;
import org.apache.fineract.client.models.GetLoansLoanIdTransactionsResponse; import org.apache.fineract.client.models.GetLoansLoanIdTransactionsResponse;
import org.apache.fineract.client.models.GetLoansLoanIdTransactionsTransactionIdResponse; import org.apache.fineract.client.models.GetLoansLoanIdTransactionsTransactionIdResponse;
import org.apache.fineract.client.models.InterestPauseRequestDto; import org.apache.fineract.client.models.InterestPauseRequestDto;
import org.apache.fineract.client.models.IsCatchUpRunningResponse; import org.apache.fineract.client.models.IsCatchUpRunningDTO;
import org.apache.fineract.client.models.PaymentAllocationOrder; import org.apache.fineract.client.models.PaymentAllocationOrder;
import org.apache.fineract.client.models.PostClientsResponse; import org.apache.fineract.client.models.PostClientsResponse;
import org.apache.fineract.client.models.PostLoansLoanIdRequest; import org.apache.fineract.client.models.PostLoansLoanIdRequest;
@@ -2293,19 +2293,19 @@ public class LoanStepDef extends AbstractStepDef {
.pollInterval(Duration.ofSeconds(5)) // .pollInterval(Duration.ofSeconds(5)) //
.pollDelay(Duration.ofSeconds(5)) // .pollDelay(Duration.ofSeconds(5)) //
.until(() -> { .until(() -> {
Response<IsCatchUpRunningResponse> isCatchUpRunningResponse = loanCobCatchUpApi.isCatchUpRunning().execute(); Response<IsCatchUpRunningDTO> isCatchUpRunningResponse = loanCobCatchUpApi.isCatchUpRunning().execute();
ErrorHelper.checkSuccessfulApiCall(isCatchUpRunningResponse); ErrorHelper.checkSuccessfulApiCall(isCatchUpRunningResponse);
IsCatchUpRunningResponse isCatchUpRunning = isCatchUpRunningResponse.body(); IsCatchUpRunningDTO isCatchUpRunning = isCatchUpRunningResponse.body();
return isCatchUpRunning.getIsCatchUpRunning(); return isCatchUpRunning.getCatchUpRunning();
}); });
await().atMost(Duration.ofMinutes(4)) // await().atMost(Duration.ofMinutes(4)) //
.pollInterval(Duration.ofSeconds(5)) // .pollInterval(Duration.ofSeconds(5)) //
.pollDelay(Duration.ofSeconds(5)) // .pollDelay(Duration.ofSeconds(5)) //
.until(() -> { .until(() -> {
Response<IsCatchUpRunningResponse> isCatchUpRunningResponse = loanCobCatchUpApi.isCatchUpRunning().execute(); Response<IsCatchUpRunningDTO> isCatchUpRunningResponse = loanCobCatchUpApi.isCatchUpRunning().execute();
ErrorHelper.checkSuccessfulApiCall(isCatchUpRunningResponse); ErrorHelper.checkSuccessfulApiCall(isCatchUpRunningResponse);
IsCatchUpRunningResponse isCatchUpRunning = isCatchUpRunningResponse.body(); IsCatchUpRunningDTO isCatchUpRunning = isCatchUpRunningResponse.body();
return !isCatchUpRunning.getIsCatchUpRunning(); return !isCatchUpRunning.getCatchUpRunning();
}); });
} }
@@ -19,8 +19,6 @@
package org.apache.fineract.cob.api; package org.apache.fineract.cob.api;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@@ -31,41 +29,31 @@ import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces; import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import java.util.Objects; import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.cob.data.IsCatchUpRunningDTO; import org.apache.fineract.cob.data.IsCatchUpRunningDTO;
import org.apache.fineract.cob.data.OldestCOBProcessedLoanDTO; import org.apache.fineract.cob.data.OldestCOBProcessedLoanDTO;
import org.apache.fineract.cob.service.LoanCOBCatchUpService; import org.apache.fineract.cob.service.LoanCOBCatchUpService;
import org.apache.fineract.infrastructure.core.exception.JobIsNotFoundOrNotEnabledException; import org.apache.fineract.infrastructure.core.exception.JobIsNotFoundOrNotEnabledException;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.jobs.service.JobName; import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Path("/v1/loans") @Path("/v1/loans")
@Component @Component
@Tag(name = "Loan COB Catch Up", description = "") @Tag(name = "Loan COB Catch Up", description = "")
@RequiredArgsConstructor
public class LoanCOBCatchUpApiResource { public class LoanCOBCatchUpApiResource {
@Autowired private final Optional<LoanCOBCatchUpService> loanCOBCatchUpServiceOp;
private DefaultToApiJsonSerializer<OldestCOBProcessedLoanDTO> oldestCOBProcessedLoanSerializeService;
@Autowired
private DefaultToApiJsonSerializer<IsCatchUpRunningDTO> isCatchUpRunningSerializer;
@Autowired(required = false)
private LoanCOBCatchUpService loanCOBCatchUpService;
@GET @GET
@Path("oldest-cob-closed") @Path("oldest-cob-closed")
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieves the oldest COB processed loan", description = "Retrieves the COB business date and the oldest COB processed loan") @Operation(summary = "Retrieves the oldest COB processed loan", description = "Retrieves the COB business date and the oldest COB processed loan")
@ApiResponses({ public OldestCOBProcessedLoanDTO getOldestCOBProcessedLoan() {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = LoanCOBCatchUpApiResourceSwagger.GetOldestCOBProcessedLoanResponse.class))) }) return loanCOBCatchUpServiceOp.map(LoanCOBCatchUpService::getOldestCOBProcessedLoan)
public String getOldestCOBProcessedLoan() { .orElseThrow(() -> new JobIsNotFoundOrNotEnabledException(JobName.LOAN_COB.name()));
if (Objects.isNull(loanCOBCatchUpService)) {
throw new JobIsNotFoundOrNotEnabledException(JobName.LOAN_COB.name());
}
OldestCOBProcessedLoanDTO response = loanCOBCatchUpService.getOldestCOBProcessedLoan();
return oldestCOBProcessedLoanSerializeService.serialize(response);
} }
@POST @POST
@@ -77,20 +65,19 @@ public class LoanCOBCatchUpApiResource {
@ApiResponse(responseCode = "202", description = "Catch Up has been started"), @ApiResponse(responseCode = "202", description = "Catch Up has been started"),
@ApiResponse(responseCode = "400", description = "Catch Up is already running") }) @ApiResponse(responseCode = "400", description = "Catch Up is already running") })
public Response executeLoanCOBCatchUp() { public Response executeLoanCOBCatchUp() {
if (Objects.isNull(loanCOBCatchUpService)) { return loanCOBCatchUpServiceOp.map(loanCOBCatchUpService -> {
throw new JobIsNotFoundOrNotEnabledException(JobName.LOAN_COB.name()); if (loanCOBCatchUpService.isCatchUpRunning().isCatchUpRunning()) {
} return Response.status(Response.Status.BAD_REQUEST).build();
if (loanCOBCatchUpService.isCatchUpRunning().isCatchUpRunning()) { }
return Response.status(Response.Status.BAD_REQUEST).build(); loanCOBCatchUpService.unlockHardLockedLoans();
} OldestCOBProcessedLoanDTO oldestCOBProcessedLoan = loanCOBCatchUpService.getOldestCOBProcessedLoan();
loanCOBCatchUpService.unlockHardLockedLoans();
OldestCOBProcessedLoanDTO oldestCOBProcessedLoan = loanCOBCatchUpService.getOldestCOBProcessedLoan();
if (oldestCOBProcessedLoan.getCobProcessedDate().equals(oldestCOBProcessedLoan.getCobBusinessDate())) {
return Response.status(Response.Status.OK).build();
}
loanCOBCatchUpService.executeLoanCOBCatchUp();
return Response.status(Response.Status.ACCEPTED).build(); if (oldestCOBProcessedLoan.getCobProcessedDate().equals(oldestCOBProcessedLoan.getCobBusinessDate())) {
return Response.status(Response.Status.OK).build();
}
loanCOBCatchUpService.executeLoanCOBCatchUp();
return Response.status(Response.Status.ACCEPTED).build();
}).orElseThrow(() -> new JobIsNotFoundOrNotEnabledException(JobName.LOAN_COB.name()));
} }
@GET @GET
@@ -98,14 +85,8 @@ public class LoanCOBCatchUpApiResource {
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieves whether Loan COB catch up is running", description = "Retrieves whether Loan COB catch up is running, and the current execution date if it is running.") @Operation(summary = "Retrieves whether Loan COB catch up is running", description = "Retrieves whether Loan COB catch up is running, and the current execution date if it is running.")
@ApiResponses({ public IsCatchUpRunningDTO isCatchUpRunning() {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = LoanCOBCatchUpApiResourceSwagger.IsCatchUpRunningResponse.class))) }) return loanCOBCatchUpServiceOp.map(LoanCOBCatchUpService::isCatchUpRunning)
public String isCatchUpRunning() { .orElseThrow(() -> new JobIsNotFoundOrNotEnabledException(JobName.LOAN_COB.name()));
if (Objects.isNull(loanCOBCatchUpService)) {
throw new JobIsNotFoundOrNotEnabledException(JobName.LOAN_COB.name());
}
IsCatchUpRunningDTO response = loanCOBCatchUpService.isCatchUpRunning();
return isCatchUpRunningSerializer.serialize(response);
} }
} }
@@ -21,8 +21,8 @@ package org.apache.fineract.integrationtests.common.loans;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Objects; import java.util.Objects;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.client.models.GetOldestCOBProcessedLoanResponse; import org.apache.fineract.client.models.IsCatchUpRunningDTO;
import org.apache.fineract.client.models.IsCatchUpRunningResponse; import org.apache.fineract.client.models.OldestCOBProcessedLoanDTO;
import org.apache.fineract.client.util.Calls; import org.apache.fineract.client.util.Calls;
import org.apache.fineract.integrationtests.common.FineractClientHelper; import org.apache.fineract.integrationtests.common.FineractClientHelper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -34,16 +34,16 @@ public class LoanCOBCatchUpHelper {
public LoanCOBCatchUpHelper() {} public LoanCOBCatchUpHelper() {}
public boolean isLoanCOBCatchUpRunning() { public boolean isLoanCOBCatchUpRunning() {
Response<IsCatchUpRunningResponse> response = executeGetLoanCatchUpStatus(); Response<IsCatchUpRunningDTO> response = executeGetLoanCatchUpStatus();
return Boolean.TRUE.equals(Objects.requireNonNull(response.body()).getIsCatchUpRunning()); return Boolean.TRUE.equals(Objects.requireNonNull(response.body()).getCatchUpRunning());
} }
public boolean isLoanCOBCatchUpFinishedFor(@NotNull LocalDate cobBusinessDate) { public boolean isLoanCOBCatchUpFinishedFor(@NotNull LocalDate cobBusinessDate) {
Response<IsCatchUpRunningResponse> response = executeGetLoanCatchUpStatus(); Response<IsCatchUpRunningDTO> response = executeGetLoanCatchUpStatus();
IsCatchUpRunningResponse isCatchUpRunningResponse = Objects.requireNonNull(response.body()); IsCatchUpRunningDTO isCatchUpRunningResponse = Objects.requireNonNull(response.body());
GetOldestCOBProcessedLoanResponse getOldestCOBProcessedLoanResponse = executeRetrieveOldestCOBProcessedLoan(); OldestCOBProcessedLoanDTO getOldestCOBProcessedLoanResponse = executeRetrieveOldestCOBProcessedLoan();
return !Boolean.TRUE.equals(isCatchUpRunningResponse.getIsCatchUpRunning()) return !Boolean.TRUE.equals(isCatchUpRunningResponse.getCatchUpRunning())
&& cobBusinessDate.equals(getOldestCOBProcessedLoanResponse.getCobProcessedDate()); && cobBusinessDate.equals(getOldestCOBProcessedLoanResponse.getCobProcessedDate());
} }
@@ -51,11 +51,11 @@ public class LoanCOBCatchUpHelper {
return Calls.okR(FineractClientHelper.getFineractClient().loanCobCatchUpApi.executeLoanCOBCatchUp()); return Calls.okR(FineractClientHelper.getFineractClient().loanCobCatchUpApi.executeLoanCOBCatchUp());
} }
public GetOldestCOBProcessedLoanResponse executeRetrieveOldestCOBProcessedLoan() { public OldestCOBProcessedLoanDTO executeRetrieveOldestCOBProcessedLoan() {
return Calls.ok(FineractClientHelper.getFineractClient().loanCobCatchUpApi.getOldestCOBProcessedLoan()); return Calls.ok(FineractClientHelper.getFineractClient().loanCobCatchUpApi.getOldestCOBProcessedLoan());
} }
public Response<IsCatchUpRunningResponse> executeGetLoanCatchUpStatus() { public Response<IsCatchUpRunningDTO> executeGetLoanCatchUpStatus() {
return Calls.okR(FineractClientHelper.getFineractClient().loanCobCatchUpApi.isCatchUpRunning()); return Calls.okR(FineractClientHelper.getFineractClient().loanCobCatchUpApi.isCatchUpRunning());
} }