FINERACT-2169: Apply dto in ClientCollateralManagementApiResource

This commit is contained in:
oleksandr.masliuk
2025-04-02 10:55:53 +02:00
committed by Adam Saghy
parent f3606bb2d1
commit a0a6d3aa14
7 changed files with 95 additions and 180 deletions
@@ -20,7 +20,11 @@ package org.apache.fineract.portfolio.collateralmanagement.data;
import java.math.BigDecimal;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public final class ClientCollateralManagementData {
private final String name;
@@ -33,52 +37,13 @@ public final class ClientCollateralManagementData {
private final Long clientId;
private final Long id;
private final List<LoanTransactionData> loanTransactionData;
private ClientCollateralManagementData(final String name, final BigDecimal quantity, final BigDecimal total,
final BigDecimal totalCollateral, final Long clientId, final List<LoanTransactionData> loanTransactionData, final Long id) {
this.clientId = clientId;
this.totalCollateral = totalCollateral;
this.total = total;
this.name = name;
this.quantity = quantity;
this.loanTransactionData = loanTransactionData;
this.id = id;
}
private final Long id;
public static ClientCollateralManagementData instance(final String name, final BigDecimal quantity, final BigDecimal total,
final BigDecimal totalCollateral, final Long clientId, final List<LoanTransactionData> loanTransactionData, final Long id) {
return new ClientCollateralManagementData(name, quantity, total, totalCollateral, clientId, loanTransactionData, id);
}
public String getName() {
return this.name;
}
public Long getClientId() {
return this.clientId;
}
public BigDecimal getQuantity() {
return this.quantity;
}
public BigDecimal getTotal() {
return this.total;
}
public BigDecimal getTotalCollateral() {
return this.totalCollateral;
}
public Long getId() {
return this.id;
}
public List<LoanTransactionData> getLoanTransactionData() {
return this.loanTransactionData;
}
}
@@ -20,7 +20,9 @@ package org.apache.fineract.portfolio.collateralmanagement.data;
import java.math.BigDecimal;
import java.time.OffsetDateTime;
import lombok.Getter;
@Getter
public final class LoanTransactionData {
private final BigDecimal lastRepayment;
@@ -20,7 +20,6 @@ package org.apache.fineract.portfolio.collateralmanagement.api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
@@ -39,21 +38,18 @@ import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.UriInfo;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.domain.CommandWrapper;
import org.apache.fineract.commands.service.CommandWrapperBuilder;
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
import org.apache.fineract.infrastructure.codes.service.CodeValueReadPlatformService;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.collateralmanagement.data.ClientCollateralManagementData;
import org.apache.fineract.portfolio.collateralmanagement.data.ClientCollateralRequest;
import org.apache.fineract.portfolio.collateralmanagement.data.LoanCollateralTemplateData;
import org.apache.fineract.portfolio.collateralmanagement.domain.ClientCollateralManagement;
import org.apache.fineract.portfolio.collateralmanagement.data.UpdateClientCollateralRequest;
import org.apache.fineract.portfolio.collateralmanagement.service.ClientCollateralManagementReadPlatformService;
import org.springframework.stereotype.Component;
@@ -63,33 +59,24 @@ import org.springframework.stereotype.Component;
@RequiredArgsConstructor
public class ClientCollateralManagementApiResource {
private final DefaultToApiJsonSerializer<ClientCollateralManagement> apiJsonSerializerService;
private final DefaultToApiJsonSerializer<ClientCollateralManagementData> apiJsonSerializerDataService;
private final DefaultToApiJsonSerializer<LoanCollateralTemplateData> apiJsonSerializerForLoanCollateralTemplateService;
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
private final PlatformSecurityContext context;
private final CodeValueReadPlatformService codeValueReadPlatformService;
private final ClientCollateralManagementReadPlatformService clientCollateralManagementReadPlatformService;
private static final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<>(
Arrays.asList("name", "quantity", "total", "totalCollateral", "clientId", "loanTransactionData"));
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Operation(summary = "Get Clients Collateral Products", description = "Get Collateral Product of a Client")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.GetClientCollateralManagementsResponse.class)))) })
public String getClientCollateral(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
@Context final UriInfo uriInfo, @QueryParam("prodId") @Parameter(description = "prodId") final Long prodId) {
public List<ClientCollateralManagementData> getClientCollateral(
@PathParam("clientId") @Parameter(description = "clientId") final Long clientId, @Context final UriInfo uriInfo,
@QueryParam("prodId") @Parameter(description = "prodId") final Long prodId) {
this.context.authenticatedUser()
.validateHasReadPermission(CollateralManagementJsonInputParams.CLIENT_COLLATERAL_PRODUCT_READ_PERMISSION.getValue());
List<ClientCollateralManagementData> collateralProductList = null;
collateralProductList = this.clientCollateralManagementReadPlatformService.getClientCollaterals(clientId, prodId);
return this.apiJsonSerializerDataService.serialize(collateralProductList);
return this.clientCollateralManagementReadPlatformService.getClientCollaterals(clientId, prodId);
}
@GET
@@ -97,18 +84,14 @@ public class ClientCollateralManagementApiResource {
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Operation(summary = "Get Client Collateral Data", description = "Get Client Collateral Data")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.GetClientCollateralManagementsResponse.class))) })
public String getClientCollateralData(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
public ClientCollateralManagementData getClientCollateralData(
@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
@PathParam("clientCollateralId") @Parameter(description = "clientCollateralId") final Long collateralId) {
this.context.authenticatedUser()
.validateHasReadPermission(CollateralManagementJsonInputParams.CLIENT_COLLATERAL_PRODUCT_READ_PERMISSION.getValue());
ClientCollateralManagementData clientCollateralManagementData = this.clientCollateralManagementReadPlatformService
.getClientCollateralManagementData(collateralId);
return this.apiJsonSerializerDataService.serialize(clientCollateralManagementData);
return this.clientCollateralManagementReadPlatformService.getClientCollateralManagementData(collateralId);
}
@GET
@@ -116,30 +99,24 @@ public class ClientCollateralManagementApiResource {
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Get Client Collateral Template", description = "Get Client Collateral Template")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.GetLoanCollateralManagementTemplate.class)))) })
public String getClientCollateralTemplate(@Context final UriInfo uriInfo,
public List<LoanCollateralTemplateData> getClientCollateralTemplate(
@PathParam("clientId") @Parameter(description = "clientId") final Long clientId) {
List<LoanCollateralTemplateData> loanCollateralTemplateDataList = this.clientCollateralManagementReadPlatformService
.getLoanCollateralTemplate(clientId);
return this.apiJsonSerializerForLoanCollateralTemplateService.serialize(loanCollateralTemplateDataList);
return this.clientCollateralManagementReadPlatformService.getLoanCollateralTemplate(clientId);
}
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Operation(summary = "Add New Collateral For a Client", description = "Add New Collateral For a Client")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.PostClientCollateralRequest.class)))
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = ClientCollateralRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.PostClientCollateralResponse.class))) })
public String addCollateral(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
@Parameter(hidden = true) String apiJsonRequestBody) {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().addClientCollateralProduct(clientId).withJson(apiJsonRequestBody)
.build();
public CommandProcessingResult addCollateral(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
@Parameter(hidden = true) ClientCollateralRequest clientCollateralRequest) {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().addClientCollateralProduct(clientId)
.withJson(apiJsonSerializerDataService.serialize(clientCollateralRequest)).build();
final CommandProcessingResult commandProcessingResult = this.commandsSourceWritePlatformService.logCommandSource(commandWrapper);
return this.apiJsonSerializerService.serialize(commandProcessingResult);
return this.commandsSourceWritePlatformService.logCommandSource(commandWrapper);
}
@PUT
@@ -147,19 +124,17 @@ public class ClientCollateralManagementApiResource {
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Operation(summary = "Update New Collateral of a Client", description = "Update New Collateral of a Client")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.PutClientCollateralRequest.class)))
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = UpdateClientCollateralRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.PutClientCollateralResponse.class))) })
public String updateCollateral(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
public CommandProcessingResult updateCollateral(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
@PathParam("collateralId") @Parameter(description = "collateralId") final Long collateralId,
@Parameter(hidden = true) String apiJsonRequestBody) {
@Parameter(hidden = true) UpdateClientCollateralRequest updateClientCollateralRequest) {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().updateClientCollateralProduct(clientId, collateralId)
.withJson(apiJsonRequestBody).build();
.withJson(apiJsonSerializerDataService.serialize(updateClientCollateralRequest)).build();
final CommandProcessingResult commandProcessingResult = this.commandsSourceWritePlatformService.logCommandSource(commandWrapper);
return this.apiJsonSerializerService.serialize(commandProcessingResult);
return this.commandsSourceWritePlatformService.logCommandSource(commandWrapper);
}
@DELETE
@@ -169,12 +144,10 @@ public class ClientCollateralManagementApiResource {
@Operation(summary = "Delete Client Collateral", description = "Delete Client Collateral")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = ClientCollateralManagementApiResourceSwagger.DeleteClientCollateralResponse.class))) })
public String deleteCollateral(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
public CommandProcessingResult deleteCollateral(@PathParam("clientId") @Parameter(description = "clientId") final Long clientId,
@PathParam("collateralId") @Parameter(description = "collateralId") final Long collateralId) {
final CommandWrapper commandWrapper = new CommandWrapperBuilder().deleteClientCollateralProduct(collateralId, clientId).build();
final CommandProcessingResult commandProcessingResult = this.commandsSourceWritePlatformService.logCommandSource(commandWrapper);
return this.apiJsonSerializerService.serialize(commandProcessingResult);
return this.commandsSourceWritePlatformService.logCommandSource(commandWrapper);
}
}
@@ -20,78 +20,12 @@ package org.apache.fineract.portfolio.collateralmanagement.api;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Set;
import org.apache.fineract.portfolio.collateralmanagement.data.UpdateClientCollateralRequest;
final class ClientCollateralManagementApiResourceSwagger {
private ClientCollateralManagementApiResourceSwagger() {}
@Schema(description = "GetClientCollateralManagementsResponse")
public static final class GetClientCollateralManagementsResponse {
private GetClientCollateralManagementsResponse() {}
static final class GetClientCollateralDataResponse {
private GetClientCollateralDataResponse() {}
@Schema(example = "Gold")
public String name;
@Schema(example = "1")
public Long id;
@Schema(example = "10")
public BigDecimal quantity;
@Schema(example = "10000.00")
public BigDecimal total;
@Schema(example = "9000.00")
public BigDecimal totalCollateral;
public GetClientIdResponse client;
public Set<GetTransactionDataResponse> transaction;
}
static final class GetTransactionDataResponse {
private GetTransactionDataResponse() {}
@Schema(example = "5000.00")
public BigDecimal lastRepayment;
@Schema(example = "3000.00")
public BigDecimal remainingAmount;
@Schema(example = "1")
public Long loanId;
@Schema(example = "[2021, 6, 19]")
public LocalDateTime lastRepaymentDate;
}
static final class GetClientIdResponse {
private GetClientIdResponse() {}
@Schema(example = "1")
public Long clientId;
}
}
@Schema(description = "GetLoanCollateralManagementTemplate")
public static final class GetLoanCollateralManagementTemplate {
private GetLoanCollateralManagementTemplate() {}
@Schema(example = "1")
public Long collateralId;
@Schema(example = "10000.00")
public BigDecimal basePrice;
@Schema(example = "40")
public BigDecimal pctToBase;
@Schema(example = "10")
public BigDecimal quantity;
@Schema(example = "Vehicle")
public String name;
}
@Schema(description = "PostClientCollateralRequest")
public static final class PostClientCollateralRequest {
@@ -118,18 +52,6 @@ final class ClientCollateralManagementApiResourceSwagger {
}
@Schema(description = "PutClientCollateralRequest")
public static final class PutClientCollateralRequest {
private PutClientCollateralRequest() {}
@Schema(example = "14")
public BigDecimal quantity;
@Schema(example = "en")
public String locale;
}
@Schema(description = "PutClientCollateralResponse")
public static final class PutClientCollateralResponse {
@@ -139,7 +61,7 @@ final class ClientCollateralManagementApiResourceSwagger {
public Long resourceId;
@Schema(example = "1")
public Long clientId;
public PutClientCollateralRequest changes;
public UpdateClientCollateralRequest changes;
}
@Schema(description = "DeleteClientCollateralResponse")
@@ -0,0 +1,29 @@
/**
* 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.collateralmanagement.data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
public record ClientCollateralRequest(BigDecimal quantity, Long collateralId, String locale) implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
}
@@ -19,8 +19,12 @@
package org.apache.fineract.portfolio.collateralmanagement.data;
import java.math.BigDecimal;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.fineract.portfolio.collateralmanagement.domain.ClientCollateralManagement;
@Getter
@AllArgsConstructor
public final class LoanCollateralTemplateData {
private Long collateralId;
@@ -33,15 +37,6 @@ public final class LoanCollateralTemplateData {
private String name;
private LoanCollateralTemplateData(final Long collateralId, final BigDecimal basePrice, final BigDecimal pctToBase,
final BigDecimal quantity, final String name) {
this.quantity = quantity;
this.collateralId = collateralId;
this.basePrice = basePrice;
this.pctToBase = pctToBase;
this.name = name;
}
public static LoanCollateralTemplateData instanceOf(final ClientCollateralManagement clientCollateralManagement) {
return new LoanCollateralTemplateData(clientCollateralManagement.getId(),
clientCollateralManagement.getCollaterals().getBasePrice(), clientCollateralManagement.getCollaterals().getPctToBase(),
@@ -0,0 +1,29 @@
/**
* 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.collateralmanagement.data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
public record UpdateClientCollateralRequest(BigDecimal quantity, String locale) implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
}