Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,30 @@ public CommandWrapperBuilder deleteWorkingCapitalLoanApplication() {
return this;
}

public CommandWrapperBuilder approveWorkingCapitalLoanApplication(final Long loanId) {
this.actionName = "APPROVE";
this.entityName = "WORKINGCAPITALLOAN";
this.entityId = loanId;
this.href = "/workingcapitalloans/" + loanId;
return this;
}

public CommandWrapperBuilder rejectWorkingCapitalLoanApplication(final Long loanId) {
this.actionName = "REJECT";
this.entityName = "WORKINGCAPITALLOAN";
this.entityId = loanId;
this.href = "/workingcapitalloans/" + loanId;
return this;
}

public CommandWrapperBuilder undoWorkingCapitalLoanApplicationApproval(final Long loanId) {
this.actionName = "APPROVALUNDO";
this.entityName = "WORKINGCAPITALLOAN";
this.entityId = loanId;
this.href = "/workingcapitalloans/" + loanId;
return this;
}

public CommandWrapperBuilder createClientIdentifier(final Long clientId) {
this.actionName = "CREATE";
this.entityName = "CLIENTIDENTIFIER";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ private WorkingCapitalLoanConstants() {
// Loan commands
public static final String APPROVE_LOAN_COMMAND = "approve";
public static final String DISBURSE_LOAN_COMMAND = "disburse";

// Approval / Rejection / Undo-approval parameters
public static final String RESOURCE_NAME = WCL_RESOURCE_NAME;
public static final String approvedOnDateParamName = "approvedOnDate";
public static final String approvedLoanAmountParamName = "approvedLoanAmount";
public static final String expectedDisbursementDateParamName = "expectedDisbursementDate";
public static final String discountAmountParamName = "discountAmount";
public static final String noteParamName = "note";
public static final String rejectedOnDateParamName = "rejectedOnDate";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.apache.fineract.infrastructure.core.api.jersey.Pagination;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.infrastructure.core.exception.UnrecognizedQueryParamException;
import org.apache.fineract.infrastructure.core.service.CommandParameterUtil;
import org.apache.fineract.infrastructure.core.service.ExternalIdFactory;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.workingcapitalloan.WorkingCapitalLoanConstants;
Expand Down Expand Up @@ -190,6 +192,36 @@ public CommandProcessingResult deleteLoanApplication(
return deleteLoanApplication(null, loanExternalId);
}

@POST
@Path("{loanId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(operationId = "stateTransitionWorkingCapitalLoanById", summary = "Approve/Reject/Undo-approve a Working Capital Loan", description = "Mandatory command query parameter: approve, reject, or undoapproval.")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = WorkingCapitalLoanApiResourceSwagger.PostWorkingCapitalLoansLoanIdRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = WorkingCapitalLoanApiResourceSwagger.PostWorkingCapitalLoansLoanIdResponse.class))) })
public CommandProcessingResult stateTransitionById(
@PathParam("loanId") @Parameter(description = "loanId", required = true) final Long loanId,
@QueryParam("command") @Parameter(description = "command", required = true) final String commandParam,
@Parameter(hidden = true) final String apiRequestBodyAsJson) {
return handleStateTransition(loanId, null, commandParam, apiRequestBodyAsJson);
}

@POST
@Path("external-id/{loanExternalId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(operationId = "stateTransitionWorkingCapitalLoanByExternalId", summary = "Approve/Reject/Undo-approve a Working Capital Loan by external id", description = "Mandatory command query parameter: approve, reject, or undoapproval.")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = WorkingCapitalLoanApiResourceSwagger.PostWorkingCapitalLoansLoanIdRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = WorkingCapitalLoanApiResourceSwagger.PostWorkingCapitalLoansLoanIdResponse.class))) })
public CommandProcessingResult stateTransitionByExternalId(
@PathParam("loanExternalId") @Parameter(description = "loanExternalId", required = true) final String loanExternalId,
@QueryParam("command") @Parameter(description = "command", required = true) final String commandParam,
@Parameter(hidden = true) final String apiRequestBodyAsJson) {
return handleStateTransition(null, loanExternalId, commandParam, apiRequestBodyAsJson);
}

private CommandProcessingResult modifyLoanApplication(final Long loanId, final String loanExternalIdStr,
final String apiRequestBodyAsJson) {
final Long resolvedLoanId = loanId != null ? loanId
Expand All @@ -212,4 +244,29 @@ private CommandProcessingResult deleteLoanApplication(final Long loanId, final S
.build();
return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
}

private CommandProcessingResult handleStateTransition(final Long loanId, final String loanExternalIdStr, final String commandParam,
final String apiRequestBodyAsJson) {
final Long resolvedLoanId = loanId != null ? loanId
: readPlatformService.getResolvedLoanId(ExternalIdFactory.produce(loanExternalIdStr));
if (resolvedLoanId == null) {
throw new WorkingCapitalLoanNotFoundException(ExternalIdFactory.produce(loanExternalIdStr));
}

final CommandWrapperBuilder builder = new CommandWrapperBuilder().withJson(apiRequestBodyAsJson);
CommandWrapper commandRequest = null;
if (CommandParameterUtil.is(commandParam, "approve")) {
commandRequest = builder.approveWorkingCapitalLoanApplication(resolvedLoanId).build();
} else if (CommandParameterUtil.is(commandParam, "reject")) {
commandRequest = builder.rejectWorkingCapitalLoanApplication(resolvedLoanId).build();
} else if (CommandParameterUtil.is(commandParam, "undoapproval")) {
commandRequest = builder.undoWorkingCapitalLoanApplicationApproval(resolvedLoanId).build();
}

if (commandRequest == null) {
throw new UnrecognizedQueryParamException("command", commandParam);
}

return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,45 @@ private DeleteWorkingCapitalLoansLoanIdResponse() {}
@Schema(example = "1")
public Long resourceId;
}

@Schema(description = "PostWorkingCapitalLoansLoanIdResponse")
public static final class PostWorkingCapitalLoansLoanIdResponse {

private PostWorkingCapitalLoansLoanIdResponse() {}

@Schema(example = "2")
public Long officeId;
@Schema(example = "6")
public Long clientId;
@Schema(example = "3")
public Long loanId;
@Schema(example = "3")
public Long resourceId;
@Schema(example = "95174ff9-1a75-4d72-a413-6f9b1cb988b7")
public String resourceExternalId;
public Object changes;
}

@Schema(description = "PostWorkingCapitalLoansLoanIdRequest")
public static final class PostWorkingCapitalLoansLoanIdRequest {

private PostWorkingCapitalLoansLoanIdRequest() {}

@Schema(example = "15 January 2024", description = "Date of approval")
public String approvedOnDate;
@Schema(example = "10000.00", description = "Approved principal amount (optional, defaults to proposed principal)")
public BigDecimal approvedLoanAmount;
@Schema(example = "1 February 2024", description = "Expected disbursement date")
public String expectedDisbursementDate;
@Schema(example = "0.0", description = "Discount amount (cannot exceed creation-time discount)")
public BigDecimal discountAmount;
@Schema(example = "15 January 2024", description = "Date of rejection")
public String rejectedOnDate;
@Schema(example = "Approval/Rejection note")
public String note;
@Schema(example = "en_GB")
public String locale;
@Schema(example = "dd MMMM yyyy")
public String dateFormat;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.workingcapitalloan.domain;

public enum WorkingCapitalLoanEvent {

LOAN_APPROVED, //
LOAN_APPROVAL_UNDO, //
LOAN_REJECTED //
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.workingcapitalloan.domain;

import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.portfolio.loanaccount.domain.LoanStatus;
import org.springframework.stereotype.Component;

@Component
public class WorkingCapitalLoanLifecycleStateMachine {

public void transition(final WorkingCapitalLoanEvent event, final WorkingCapitalLoan loan) {
LoanStatus newStatus = getNextStatus(event, loan);
if (newStatus != null) {
loan.setLoanStatus(newStatus);
} else {
throw new PlatformApiDataValidationException("validation.msg.wc.loan.transition.not.allowed",
"Transition " + event + " is not allowed from status " + loan.getLoanStatus(), "loanStatus");
}
}

private LoanStatus getNextStatus(final WorkingCapitalLoanEvent event, final WorkingCapitalLoan loan) {
LoanStatus from = loan.getLoanStatus();
if (from == null) {
return null;
}

return switch (event) {
case LOAN_APPROVED -> from.isSubmittedAndPendingApproval() ? LoanStatus.APPROVED : null;
case LOAN_APPROVAL_UNDO -> from.isApproved() ? LoanStatus.SUBMITTED_AND_PENDING_APPROVAL : null;
case LOAN_REJECTED -> from.isSubmittedAndPendingApproval() ? LoanStatus.REJECTED : null;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.workingcapitalloan.handler;

import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.annotation.CommandType;
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.portfolio.workingcapitalloan.service.WorkingCapitalLoanWritePlatformService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@CommandType(entity = "WORKINGCAPITALLOAN", action = "APPROVE")
public class ApproveWorkingCapitalLoanCommandHandler implements NewCommandSourceHandler {

private final WorkingCapitalLoanWritePlatformService writePlatformService;

@Transactional
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
return this.writePlatformService.approveApplication(command.entityId(), command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.workingcapitalloan.handler;

import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.annotation.CommandType;
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.portfolio.workingcapitalloan.service.WorkingCapitalLoanWritePlatformService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@CommandType(entity = "WORKINGCAPITALLOAN", action = "REJECT")
public class RejectWorkingCapitalLoanCommandHandler implements NewCommandSourceHandler {

private final WorkingCapitalLoanWritePlatformService writePlatformService;

@Transactional
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
return this.writePlatformService.rejectApplication(command.entityId(), command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.workingcapitalloan.handler;

import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.annotation.CommandType;
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.portfolio.workingcapitalloan.service.WorkingCapitalLoanWritePlatformService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@CommandType(entity = "WORKINGCAPITALLOAN", action = "APPROVALUNDO")
public class UndoApproveWorkingCapitalLoanCommandHandler implements NewCommandSourceHandler {

private final WorkingCapitalLoanWritePlatformService writePlatformService;

@Transactional
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
return this.writePlatformService.undoApplicationApproval(command.entityId(), command);
}
}
Loading
Loading