FINERACT-628 Savings account APIs for Self Service App

This commit is contained in:
ShruthiRajaram
2018-07-17 17:05:55 +05:30
parent 2a3aaecf6a
commit 424bd0884e
4 changed files with 599 additions and 2 deletions
@@ -18,9 +18,13 @@
*/
package org.apache.fineract.portfolio.self.savings.api;
import java.util.HashMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
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;
@@ -30,10 +34,13 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.client.exception.ClientNotFoundException;
import org.apache.fineract.portfolio.savings.api.SavingsAccountChargesApiResource;
import org.apache.fineract.portfolio.savings.api.SavingsAccountTransactionsApiResource;
import org.apache.fineract.portfolio.savings.api.SavingsAccountsApiResource;
import org.apache.fineract.portfolio.savings.exception.SavingsAccountNotFoundException;
import org.apache.fineract.portfolio.self.client.service.AppuserClientMapperReadService;
import org.apache.fineract.portfolio.self.savings.data.SelfSavingsAccountConstants;
import org.apache.fineract.portfolio.self.savings.data.SelfSavingsDataValidator;
import org.apache.fineract.portfolio.self.savings.service.AppuserSavingsMapperReadService;
import org.apache.fineract.useradministration.domain.AppUser;
@@ -52,6 +59,7 @@ public class SelfSavingsApiResource {
private final SavingsAccountTransactionsApiResource savingsAccountTransactionsApiResource;
private final AppuserSavingsMapperReadService appuserSavingsMapperReadService;
private final SelfSavingsDataValidator dataValidator;
private final AppuserClientMapperReadService appUserClientMapperReadService;
@Autowired
public SelfSavingsApiResource(
@@ -60,13 +68,15 @@ public class SelfSavingsApiResource {
final SavingsAccountChargesApiResource savingsAccountChargesApiResource,
final SavingsAccountTransactionsApiResource savingsAccountTransactionsApiResource,
final AppuserSavingsMapperReadService appuserSavingsMapperReadService,
final SelfSavingsDataValidator dataValidator) {
final SelfSavingsDataValidator dataValidator,
final AppuserClientMapperReadService appUserClientMapperReadService) {
this.context = context;
this.savingsAccountsApiResource = savingsAccountsApiResource;
this.savingsAccountChargesApiResource = savingsAccountChargesApiResource;
this.savingsAccountTransactionsApiResource = savingsAccountTransactionsApiResource;
this.appuserSavingsMapperReadService = appuserSavingsMapperReadService;
this.dataValidator = dataValidator;
this.appUserClientMapperReadService = appUserClientMapperReadService;
}
@GET
@@ -145,4 +155,51 @@ public class SelfSavingsApiResource {
}
}
@GET
@Path("template")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String template(@QueryParam("clientId") final Long clientId, @QueryParam("productId") final Long productId,
final String apiRequestBodyAsJson, @Context final UriInfo uriInfo) {
validateAppuserClientsMapping(clientId);
Long groupId = null;
boolean staffInSelectedOfficeOnly = false;
return this.savingsAccountsApiResource.template(clientId, groupId, productId, staffInSelectedOfficeOnly,
uriInfo);
}
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String submitSavingsAccountApplication(@QueryParam("command") final String commandParam,
@Context final UriInfo uriInfo, final String apiRequestBodyAsJson) {
HashMap<String, Object> parameterMap = this.dataValidator.validateSavingsApplication(apiRequestBodyAsJson);
final Long clientId = (Long) parameterMap.get(SelfSavingsAccountConstants.clientIdParameterName);
validateAppuserClientsMapping(clientId);
return this.savingsAccountsApiResource.submitApplication(apiRequestBodyAsJson);
}
@PUT
@Path("{accountId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String modifySavingsAccountApplication(@PathParam("accountId") final Long accountId,
@QueryParam("command") final String commandParam, final String apiRequestBodyAsJson) {
validateAppuserSavingsAccountMapping(accountId);
this.dataValidator.validateSavingsApplication(apiRequestBodyAsJson);
return this.savingsAccountsApiResource.update(accountId, apiRequestBodyAsJson, commandParam);
}
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);
}
}
}
@@ -0,0 +1,28 @@
/**
* 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.self.savings.data;
public class SelfSavingsAccountConstants {
public static final String savingsAccountResource = "savings";
public static final String clientIdParameterName = "clientId";
public static final String productIdParameterName = "productId";
}
@@ -20,19 +20,36 @@ package org.apache.fineract.portfolio.self.savings.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.apache.fineract.portfolio.savings.SavingsApiConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.JsonElement;
@Component
public class SelfSavingsDataValidator {
private final FromJsonHelper fromApiJsonHelper;
@Autowired
public SelfSavingsDataValidator(final FromJsonHelper fromApiJsonHelper) {
this.fromApiJsonHelper = fromApiJsonHelper;
}
private static final Set<String> allowedAssociationParameters = new HashSet<>(
Arrays.asList(SavingsApiConstants.transactions,
@@ -82,5 +99,32 @@ public class SelfSavingsDataValidator {
unsupportedParams.add("template");
}
}
public HashMap<String, Object> validateSavingsApplication(final String json) {
if (StringUtils.isBlank(json)) {
throw new InvalidJsonException();
}
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
.resource(SelfSavingsAccountConstants.savingsAccountResource);
final JsonElement element = this.fromApiJsonHelper.parse(json);
final Long clientId = this.fromApiJsonHelper.extractLongNamed(SelfSavingsAccountConstants.clientIdParameterName,
element);
baseDataValidator.reset().parameter(SelfSavingsAccountConstants.clientIdParameterName).value(clientId).notNull()
.longGreaterThanZero();
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException(dataValidationErrors);
}
HashMap<String, Object> parameterMap = new HashMap<>();
parameterMap.put(SelfSavingsAccountConstants.clientIdParameterName, clientId);
return parameterMap;
}
}