Self Service - Loan Application
This commit is contained in:
+100
-1
@@ -18,20 +18,31 @@
|
||||
*/
|
||||
package org.apache.fineract.portfolio.self.loanaccount.api;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.fineract.infrastructure.core.exception.UnrecognizedQueryParamException;
|
||||
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
|
||||
import org.apache.fineract.portfolio.client.exception.ClientNotFoundException;
|
||||
import org.apache.fineract.portfolio.loanaccount.api.LoanChargesApiResource;
|
||||
import org.apache.fineract.portfolio.loanaccount.api.LoanTransactionsApiResource;
|
||||
import org.apache.fineract.portfolio.loanaccount.api.LoansApiResource;
|
||||
import org.apache.fineract.portfolio.loanaccount.exception.LoanNotFoundException;
|
||||
import org.apache.fineract.portfolio.loanaccount.exception.LoanTemplateTypeRequiredException;
|
||||
import org.apache.fineract.portfolio.loanaccount.exception.NotSupportedLoanTemplateTypeException;
|
||||
import org.apache.fineract.portfolio.self.client.service.AppuserClientMapperReadService;
|
||||
import org.apache.fineract.portfolio.self.loanaccount.data.SelfLoansDataValidator;
|
||||
import org.apache.fineract.portfolio.self.loanaccount.service.AppuserLoansMapperReadService;
|
||||
import org.apache.fineract.useradministration.domain.AppUser;
|
||||
@@ -49,6 +60,7 @@ public class SelfLoansApiResource {
|
||||
private final LoanTransactionsApiResource loanTransactionsApiResource;
|
||||
private final LoanChargesApiResource loanChargesApiResource;
|
||||
private final AppuserLoansMapperReadService appuserLoansMapperReadService;
|
||||
private final AppuserClientMapperReadService appUserClientMapperReadService;
|
||||
private final SelfLoansDataValidator dataValidator;
|
||||
|
||||
@Autowired
|
||||
@@ -57,12 +69,14 @@ public class SelfLoansApiResource {
|
||||
final LoanTransactionsApiResource loanTransactionsApiResource,
|
||||
final LoanChargesApiResource loanChargesApiResource,
|
||||
final AppuserLoansMapperReadService appuserLoansMapperReadService,
|
||||
final AppuserClientMapperReadService appUserClientMapperReadService,
|
||||
final SelfLoansDataValidator dataValidator) {
|
||||
this.context = context;
|
||||
this.loansApiResource = loansApiResource;
|
||||
this.loanTransactionsApiResource = loanTransactionsApiResource;
|
||||
this.loanChargesApiResource = loanChargesApiResource;
|
||||
this.appuserLoansMapperReadService = appuserLoansMapperReadService;
|
||||
this.appUserClientMapperReadService = appUserClientMapperReadService;
|
||||
this.dataValidator = dataValidator;
|
||||
}
|
||||
|
||||
@@ -125,7 +139,79 @@ public class SelfLoansApiResource {
|
||||
return this.retrieveLoanCharge(loanId, loanChargeId, uriInfo);
|
||||
}
|
||||
|
||||
private void validateAppuserLoanMapping(final Long loanId) {
|
||||
@GET
|
||||
@Path("template")
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String template(@QueryParam("clientId") final Long clientId,
|
||||
@QueryParam("productId") final Long productId,
|
||||
@QueryParam("templateType") final String templateType,
|
||||
@Context final UriInfo uriInfo) {
|
||||
|
||||
if(clientId != null){
|
||||
validateAppuserClientsMapping(clientId);
|
||||
}
|
||||
|
||||
if (templateType == null) {
|
||||
final String errorMsg = "Loan template type must be provided";
|
||||
throw new LoanTemplateTypeRequiredException(errorMsg);
|
||||
} else if (!(templateType.equalsIgnoreCase("individual")
|
||||
|| templateType.equalsIgnoreCase("collateral"))){
|
||||
final String errorMsg = "Loan template type '" + templateType + "' is not supported";
|
||||
throw new NotSupportedLoanTemplateTypeException(errorMsg, templateType);
|
||||
}
|
||||
final Long groupId = null;
|
||||
final boolean staffInSelectedOfficeOnly = false;
|
||||
final boolean onlyActive = true;
|
||||
return this.loansApiResource.template(clientId, groupId, productId,
|
||||
templateType, staffInSelectedOfficeOnly, onlyActive, uriInfo);
|
||||
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String calculateLoanScheduleOrSubmitLoanApplication(@QueryParam("command") final String commandParam,
|
||||
@Context final UriInfo uriInfo, final String apiRequestBodyAsJson) {
|
||||
|
||||
HashMap<String, Object> attr = this.dataValidator.validateLoanApplication(apiRequestBodyAsJson);
|
||||
final Long clientId = (Long) attr.get("clientId");
|
||||
validateAppuserClientsMapping(clientId);
|
||||
|
||||
return this.loansApiResource.calculateLoanScheduleOrSubmitLoanApplication(commandParam,
|
||||
uriInfo, apiRequestBodyAsJson);
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("{loanId}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String modifyLoanApplication(@PathParam("loanId") final Long loanId, final String apiRequestBodyAsJson) {
|
||||
|
||||
HashMap<String, Object> attr = this.dataValidator.validateModifyLoanApplication(apiRequestBodyAsJson);
|
||||
validateAppuserLoanMapping(loanId);
|
||||
final Long clientId = (Long) attr.get("clientId");
|
||||
if(clientId != null){
|
||||
validateAppuserClientsMapping(clientId);
|
||||
}
|
||||
|
||||
return this.loansApiResource.modifyLoanApplication(loanId, apiRequestBodyAsJson);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("{loanId}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String stateTransitions(@PathParam("loanId") final Long loanId, @QueryParam("command") final String commandParam,
|
||||
final String apiRequestBodyAsJson) {
|
||||
if (!is(commandParam, "withdrawnByApplicant")) {
|
||||
throw new UnrecognizedQueryParamException("command", commandParam);
|
||||
}
|
||||
validateAppuserLoanMapping(loanId);
|
||||
return this.loansApiResource.stateTransitions(loanId, commandParam, apiRequestBodyAsJson);
|
||||
}
|
||||
|
||||
private void validateAppuserLoanMapping(final Long loanId) {
|
||||
AppUser user = this.context.authenticatedUser();
|
||||
final boolean isLoanMappedToUser = this.appuserLoansMapperReadService
|
||||
.isLoanMappedToUser(loanId, user.getId());
|
||||
@@ -133,5 +219,18 @@ public class SelfLoansApiResource {
|
||||
throw new LoanNotFoundException(loanId);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateAppuserClientsMapping(final Long clientId) {
|
||||
AppUser user = this.context.authenticatedUser();
|
||||
final boolean mappedClientId = this.appUserClientMapperReadService
|
||||
.isClientMappedToUser(clientId, user.getId());
|
||||
if (!mappedClientId) {
|
||||
throw new ClientNotFoundException(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean is(final String commandParam, final String commandValue) {
|
||||
return StringUtils.isNotBlank(commandParam) && commandParam.trim().equalsIgnoreCase(commandValue);
|
||||
}
|
||||
|
||||
}
|
||||
+72
-3
@@ -20,16 +20,26 @@ package org.apache.fineract.portfolio.self.loanaccount.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.fineract.infrastructure.core.api.ApiParameterHelper;
|
||||
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
|
||||
import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
|
||||
import org.apache.fineract.infrastructure.core.exception.InvalidJsonException;
|
||||
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
|
||||
import org.apache.fineract.infrastructure.core.exception.UnsupportedParameterException;
|
||||
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
@Component
|
||||
public class SelfLoansDataValidator {
|
||||
private static final Set<String> allowedAssociationParameters = new HashSet<>(
|
||||
@@ -37,12 +47,16 @@ public class SelfLoansDataValidator {
|
||||
"originalSchedule", "transactions", "charges",
|
||||
"guarantors", "collateral", "linkedAccount",
|
||||
"multiDisburseDetails"));
|
||||
|
||||
private final FromJsonHelper fromApiJsonHelper;
|
||||
|
||||
@Autowired
|
||||
public SelfLoansDataValidator(final FromJsonHelper fromApiJsonHelper){
|
||||
this.fromApiJsonHelper = fromApiJsonHelper;
|
||||
}
|
||||
|
||||
public void validateRetrieveLoan(final UriInfo uriInfo) {
|
||||
List<String> unsupportedParams = new ArrayList<>();
|
||||
|
||||
validateTemplate(uriInfo, unsupportedParams);
|
||||
|
||||
Set<String> associationParameters = ApiParameterHelper
|
||||
.extractAssociationsForResponseIfProvided(uriInfo
|
||||
.getQueryParameters());
|
||||
@@ -68,6 +82,60 @@ public class SelfLoansDataValidator {
|
||||
throwExceptionIfReqd(unsupportedParams);
|
||||
|
||||
}
|
||||
|
||||
public HashMap<String, Object> validateLoanApplication(final String json){
|
||||
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
|
||||
|
||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
|
||||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan");
|
||||
|
||||
final JsonElement element = this.fromApiJsonHelper.parse(json);
|
||||
|
||||
final String loanTypeParameterName = "loanType";
|
||||
final String loanTypeStr = this.fromApiJsonHelper.extractStringNamed(loanTypeParameterName, element);
|
||||
baseDataValidator.reset().parameter(loanTypeParameterName).value(loanTypeStr).notNull().equals("individual");
|
||||
|
||||
final String clientIdParameterName = "clientId";
|
||||
final String clientId = this.fromApiJsonHelper.extractStringNamed(clientIdParameterName, element);
|
||||
baseDataValidator.reset().parameter(clientIdParameterName).value(clientId).notNull().longGreaterThanZero();
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); }
|
||||
|
||||
HashMap<String, Object> retAttr = new HashMap<>();
|
||||
retAttr.put("clientId", Long.parseLong(clientId));
|
||||
|
||||
return retAttr;
|
||||
|
||||
}
|
||||
|
||||
public HashMap<String, Object> validateModifyLoanApplication(final String json) {
|
||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
|
||||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan");
|
||||
|
||||
final JsonElement element = this.fromApiJsonHelper.parse(json);
|
||||
|
||||
final String loanTypeParameterName = "loanType";
|
||||
if(this.fromApiJsonHelper.parameterExists(loanTypeParameterName, element)){
|
||||
final String loanTypeStr = this.fromApiJsonHelper.extractStringNamed(loanTypeParameterName, element);
|
||||
baseDataValidator.reset().parameter(loanTypeParameterName).value(loanTypeStr).notNull().equals("individual");
|
||||
}
|
||||
|
||||
final String clientIdParameterName = "clientId";
|
||||
String clientId = null;
|
||||
if(this.fromApiJsonHelper.parameterExists(clientIdParameterName, element)){
|
||||
clientId = this.fromApiJsonHelper.extractStringNamed(clientIdParameterName, element);
|
||||
baseDataValidator.reset().parameter(clientIdParameterName).value(clientId).notNull().longGreaterThanZero();
|
||||
}
|
||||
|
||||
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); }
|
||||
|
||||
HashMap<String, Object> retAttr = new HashMap<>();
|
||||
if(clientId != null){
|
||||
retAttr.put("clientId", Long.parseLong(clientId));
|
||||
}
|
||||
|
||||
return retAttr;
|
||||
}
|
||||
|
||||
private void throwExceptionIfReqd(List<String> unsupportedParams) {
|
||||
if (unsupportedParams.size() > 0) {
|
||||
@@ -84,4 +152,5 @@ public class SelfLoansDataValidator {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -325,7 +325,7 @@ public class AppUser extends AbstractPersistable<Long> implements PlatformUser {
|
||||
}else if(!this.isSelfServiceUser && actualChanges.containsKey(AppUserConstants.IS_SELF_SERVICE_USER)){
|
||||
actualChanges.put(AppUserConstants.CLIENTS, new ArrayList<>());
|
||||
if(this.appUserClientMappings != null){
|
||||
this.appUserClientMappings = null;
|
||||
this.appUserClientMappings.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user