FINERACT-1593: Bugfixes for Client Address creation

This commit is contained in:
Arnold Galovics
2022-04-26 13:48:23 +02:00
committed by Arnold Gálovics
parent df5718ad04
commit 9d212ba471
16 changed files with 434 additions and 258 deletions
@@ -134,6 +134,24 @@ public class GlobalConfigurationApiResource {
return this.propertyDataJsonSerializer.serialize(settings, configurationData, RESPONSE_DATA_PARAMETERS);
}
@GET
@Path("name/{name}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve Global Configuration", description = "Returns a global enable/disable configuration.\n" + "\n"
+ "Example Requests:\n" + "\n" + "configurations/name/Enable-Address")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = GlobalConfigurationPropertyData.class))) })
public String retrieveOneByName(@PathParam("name") @Parameter(description = "name") final String name, @Context final UriInfo uriInfo) {
this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermissions);
final GlobalConfigurationPropertyData configurationData = this.readPlatformService.retrieveGlobalConfiguration(name);
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.propertyDataJsonSerializer.serialize(settings, configurationData, RESPONSE_DATA_PARAMETERS);
}
@PUT
@Path("{configId}")
@Consumes({ MediaType.APPLICATION_JSON })
@@ -24,7 +24,6 @@ import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@@ -182,14 +181,12 @@ public class Address extends AbstractPersistableCustom {
BigDecimal latitude = BigDecimal.ZERO;
BigDecimal longitude = BigDecimal.ZERO;
String createdBy = "";
Locale locale = Locale.ENGLISH;
String updatedBy = "";
LocalDate updatedOnDate = null;
LocalDate createdOnDate = null;
if (jsonObject.has("street")) {
street = jsonObject.get("street").getAsString();
}
if (jsonObject.has("addressLine1")) {
@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
@@ -34,22 +35,15 @@ import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidati
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
import org.apache.fineract.portfolio.address.data.FieldConfigurationData;
import org.apache.fineract.portfolio.address.service.FieldConfigurationReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class AddressCommandFromApiJsonDeserializer {
private final FromJsonHelper fromApiJsonHelper;
private final FieldConfigurationReadPlatformService readservice;
@Autowired
public AddressCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper,
final FieldConfigurationReadPlatformService readservice) {
this.fromApiJsonHelper = fromApiJsonHelper;
this.readservice = readservice;
}
public void validateForUpdate(final String json) {
validate(json, false);
}
@@ -22,6 +22,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.math.BigDecimal;
import java.time.LocalDate;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.codes.domain.CodeValue;
import org.apache.fineract.infrastructure.codes.domain.CodeValueRepository;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
@@ -38,10 +39,10 @@ import org.apache.fineract.portfolio.client.domain.ClientAddress;
import org.apache.fineract.portfolio.client.domain.ClientAddressRepository;
import org.apache.fineract.portfolio.client.domain.ClientAddressRepositoryWrapper;
import org.apache.fineract.portfolio.client.domain.ClientRepositoryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class AddressWritePlatformServiceImpl implements AddressWritePlatformService {
private final PlatformSecurityContext context;
@@ -52,112 +53,77 @@ public class AddressWritePlatformServiceImpl implements AddressWritePlatformServ
private final ClientAddressRepositoryWrapper clientAddressRepositoryWrapper;
private final AddressCommandFromApiJsonDeserializer fromApiJsonDeserializer;
@Autowired
public AddressWritePlatformServiceImpl(final PlatformSecurityContext context, final CodeValueRepository codeValueRepository,
final ClientAddressRepository clientAddressRepository, final ClientRepositoryWrapper clientRepositoryWrapper,
final AddressRepository addressRepository, final ClientAddressRepositoryWrapper clientAddressRepositoryWrapper,
final AddressCommandFromApiJsonDeserializer fromApiJsonDeserializer) {
this.context = context;
this.codeValueRepository = codeValueRepository;
this.clientAddressRepository = clientAddressRepository;
this.clientRepositoryWrapper = clientRepositoryWrapper;
this.addressRepository = addressRepository;
this.clientAddressRepositoryWrapper = clientAddressRepositoryWrapper;
this.fromApiJsonDeserializer = fromApiJsonDeserializer;
}
@Override
public CommandProcessingResult addClientAddress(final Long clientId, final Long addressTypeId, final JsonCommand command) {
CodeValue stateIdobj = null;
CodeValue countryIdObj = null;
long stateId;
long countryId;
JsonObject jsonObject = command.parsedJson().getAsJsonObject();
context.authenticatedUser();
fromApiJsonDeserializer.validateForCreate(jsonObject.toString(), false);
this.context.authenticatedUser();
this.fromApiJsonDeserializer.validateForCreate(command.json(), true);
final CodeValue addressTypeIdCodeValue = codeValueRepository.getById(addressTypeId);
final Client client = clientRepositoryWrapper.findOneWithNotFoundDetection(clientId);
if (command.longValueOfParameterNamed("stateProvinceId") != null) {
stateId = command.longValueOfParameterNamed("stateProvinceId");
stateIdobj = this.codeValueRepository.getById(stateId);
}
final Address address = createAddress(jsonObject);
addressRepository.save(address);
if (command.longValueOfParameterNamed("countryId") != null) {
countryId = command.longValueOfParameterNamed("countryId");
countryIdObj = this.codeValueRepository.getById(countryId);
}
final ClientAddress clientAddress = createClientAddress(client, jsonObject, addressTypeIdCodeValue, address);
clientAddressRepository.saveAndFlush(clientAddress);
final CodeValue addressTypeIdObj = this.codeValueRepository.getById(addressTypeId);
final Address add = Address.fromJson(command, stateIdobj, countryIdObj);
add.setCreatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant()));
add.setUpdatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant()));
this.addressRepository.save(add);
final Long addressid = add.getId();
final Address addobj = this.addressRepository.getById(addressid);
final Client client = this.clientRepositoryWrapper.findOneWithNotFoundDetection(clientId);
final boolean isActive = command.booleanPrimitiveValueOfParameterNamed("isActive");
final ClientAddress clientAddressobj = ClientAddress.fromJson(isActive, client, addobj, addressTypeIdObj);
this.clientAddressRepository.saveAndFlush(clientAddressobj);
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(clientAddressobj.getId()).build();
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(clientAddress.getId()).build();
}
// following method is used for adding multiple addresses while creating new
// client
@Override
public CommandProcessingResult addNewClientAddress(final Client client, final JsonCommand command) {
CodeValue stateIdobj = null;
CodeValue countryIdObj = null;
long stateId;
long countryId;
ClientAddress clientAddressobj = new ClientAddress();
ClientAddress clientAddress = new ClientAddress();
final JsonArray addressArray = command.arrayOfParameterNamed("address");
if (addressArray != null) {
for (int i = 0; i < addressArray.size(); i++) {
final JsonObject jsonObject = addressArray.get(i).getAsJsonObject();
// validate every address
this.fromApiJsonDeserializer.validateForCreate(jsonObject.toString(), true);
if (jsonObject.get("stateProvinceId") != null) {
stateId = jsonObject.get("stateProvinceId").getAsLong();
stateIdobj = this.codeValueRepository.getById(stateId);
}
if (jsonObject.get("countryId") != null) {
countryId = jsonObject.get("countryId").getAsLong();
countryIdObj = this.codeValueRepository.getById(countryId);
}
fromApiJsonDeserializer.validateForCreate(jsonObject.toString(), true);
final long addressTypeId = jsonObject.get("addressTypeId").getAsLong();
final CodeValue addressTypeIdObj = this.codeValueRepository.getById(addressTypeId);
final CodeValue addressTypeIdCodeValue = codeValueRepository.getById(addressTypeId);
final Address add = Address.fromJsonObject(jsonObject, stateIdobj, countryIdObj);
add.setCreatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant()));
add.setUpdatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant()));
this.addressRepository.save(add);
final Long addressid = add.getId();
final Address addobj = this.addressRepository.getById(addressid);
final Address address = createAddress(jsonObject);
addressRepository.save(address);
// final boolean isActive =
// jsonObject.get("isActive").getAsBoolean();
boolean isActive = false;
if (jsonObject.get("isActive") != null) {
isActive = jsonObject.get("isActive").getAsBoolean();
}
clientAddressobj = ClientAddress.fromJson(isActive, client, addobj, addressTypeIdObj);
this.clientAddressRepository.saveAndFlush(clientAddressobj);
clientAddress = createClientAddress(client, jsonObject, addressTypeIdCodeValue, address);
clientAddressRepository.saveAndFlush(clientAddress);
}
}
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(clientAddressobj.getId()).build();
// This is confusing because only the last client address id is returned
// TODO: clean this up
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(clientAddress.getId()).build();
}
private ClientAddress createClientAddress(Client client, JsonObject jsonObject, CodeValue addressTypeIdCodeValue, Address address) {
boolean clientAddressIsActive = false;
if (jsonObject.get("isActive") != null) {
clientAddressIsActive = jsonObject.get("isActive").getAsBoolean();
}
return ClientAddress.fromJson(clientAddressIsActive, client, address, addressTypeIdCodeValue);
}
private Address createAddress(JsonObject jsonObject) {
CodeValue stateIdCodeValue = null;
if (jsonObject.get("stateProvinceId") != null) {
long stateId = jsonObject.get("stateProvinceId").getAsLong();
stateIdCodeValue = codeValueRepository.getById(stateId);
}
CodeValue countryIdCodeValue = null;
if (jsonObject.get("countryId") != null) {
long countryId = jsonObject.get("countryId").getAsLong();
countryIdCodeValue = codeValueRepository.getById(countryId);
}
final Address address = Address.fromJsonObject(jsonObject, stateIdCodeValue, countryIdCodeValue);
address.setCreatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant()));
address.setUpdatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant()));
return address;
}
@Override
@@ -133,6 +133,8 @@ public class ClientAddressApiResources {
this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermissions);
// TODO: This is quite a confusing implementation with all these checks
// These have to be considered as filtering criterias instead
if (addressTypeId == 0 && status == null) {
address = this.readPlatformService.retrieveAllClientAddress(clientid);
} else if (addressTypeId != 0 && status == null) {
@@ -49,6 +49,8 @@ final class ClientAddressApiResourcesSwagger {
public Integer countryId;
@Schema(example = "400064")
public Long postalCode;
@Schema(example = "true")
public Boolean isActive;
}
@Schema(description = "PostClientClientIdAddressesResponse")
@@ -48,6 +48,7 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.commands.domain.CommandWrapper;
import org.apache.fineract.commands.service.CommandWrapperBuilder;
@@ -74,7 +75,6 @@ import org.apache.fineract.portfolio.savings.data.SavingsAccountData;
import org.apache.fineract.portfolio.savings.service.SavingsAccountReadPlatformService;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@@ -83,6 +83,7 @@ import org.springframework.stereotype.Component;
@Scope("singleton")
@Tag(name = "Client", description = "Clients are people and businesses that have applied (or may apply) to an MFI for loans.\n" + "\n"
+ "Clients can be created in Pending or straight into Active state.")
@RequiredArgsConstructor
public class ClientsApiResource {
private final PlatformSecurityContext context;
@@ -97,29 +98,6 @@ public class ClientsApiResource {
private final BulkImportWorkbookPopulatorService bulkImportWorkbookPopulatorService;
private final GuarantorReadPlatformService guarantorReadPlatformService;
@Autowired
public ClientsApiResource(final PlatformSecurityContext context, final ClientReadPlatformService readPlatformService,
final ToApiJsonSerializer<ClientData> toApiJsonSerializer,
final ToApiJsonSerializer<AccountSummaryCollectionData> clientAccountSummaryToApiJsonSerializer,
final ApiRequestParameterHelper apiRequestParameterHelper,
final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService,
final AccountDetailsReadPlatformService accountDetailsReadPlatformService,
final SavingsAccountReadPlatformService savingsAccountReadPlatformService,
final BulkImportWorkbookPopulatorService bulkImportWorkbookPopulatorService,
final BulkImportWorkbookService bulkImportWorkbookService, final GuarantorReadPlatformService guarantorReadPlatformService) {
this.context = context;
this.clientReadPlatformService = readPlatformService;
this.toApiJsonSerializer = toApiJsonSerializer;
this.clientAccountSummaryToApiJsonSerializer = clientAccountSummaryToApiJsonSerializer;
this.apiRequestParameterHelper = apiRequestParameterHelper;
this.commandsSourceWritePlatformService = commandsSourceWritePlatformService;
this.accountDetailsReadPlatformService = accountDetailsReadPlatformService;
this.savingsAccountReadPlatformService = savingsAccountReadPlatformService;
this.bulkImportWorkbookPopulatorService = bulkImportWorkbookPopulatorService;
this.bulkImportWorkbookService = bulkImportWorkbookService;
this.guarantorReadPlatformService = guarantorReadPlatformService;
}
@GET
@Path("template")
@Consumes({ MediaType.APPLICATION_JSON })
@@ -266,12 +266,42 @@ final class ClientsApiResourceSwagger {
}
static final class PostClientsAddressRequest {
@Schema(example = "Ipca")
public String street;
@Schema(example = "Kandivali")
public String addressLine1;
@Schema(example = "plot47")
public String addressLine2;
@Schema(example = "charkop")
public String addressLine3;
@Schema(example = "Mumbai")
public String city;
@Schema(example = "800")
public Integer stateProvinceId;
@Schema(example = "802")
public Integer countryId;
@Schema(example = "400064")
public Long postalCode;
@Schema(example = "1")
public Long addressTypeId;
@Schema(example = "true")
public Boolean isActive;
}
@Schema(example = "1")
public Integer officeId;
@Schema(example = "1")
public Integer legalFormId;
@Schema(example = "Client of group")
public String fullname;
@Schema(example = "Client_FirstName")
public String firstname;
@Schema(example = "123")
public String externalId;
@Schema(example = "Client_LastName")
public String lastname;
@Schema(example = "1")
public Integer groupId;
@Schema(example = "dd MMMM yyyy")
@@ -284,6 +314,8 @@ final class ClientsApiResourceSwagger {
public String activationDate;
@Schema(description = "List of PostClientsDatatable")
public List<PostClientsDatatable> datatables;
@Schema(description = "Address requests")
public List<PostClientsAddressRequest> address;
}