From 99f313e02c1d9ed09227d15fcd26203cc57ada38 Mon Sep 17 00:00:00 2001 From: viktorpavlenko Date: Wed, 5 Mar 2025 16:04:39 +0200 Subject: [PATCH] FINERACT-2169: Account rules api refactor; --- .../rule/api/AccountingRuleApiResource.java | 136 ++++++------------ .../api/AccountingRuleApiResourceSwagger.java | 67 --------- .../rule/data/request/AccountRuleRequest.java | 30 ++++ .../AccountingRuleIntegrationTest.java | 4 +- .../common/accounting/AccountRuleHelper.java | 6 +- 5 files changed, 75 insertions(+), 168 deletions(-) create mode 100644 fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/data/request/AccountRuleRequest.java diff --git a/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResource.java b/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResource.java index 40e155e48..e69994034 100644 --- a/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResource.java +++ b/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResource.java @@ -20,7 +20,6 @@ package org.apache.fineract.accounting.rule.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,10 +38,8 @@ import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.UriInfo; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -52,6 +49,7 @@ import org.apache.fineract.accounting.glaccount.data.GLAccountData; import org.apache.fineract.accounting.glaccount.service.GLAccountReadPlatformService; import org.apache.fineract.accounting.rule.data.AccountingRuleData; import org.apache.fineract.accounting.rule.data.AccountingTagRuleData; +import org.apache.fineract.accounting.rule.data.request.AccountRuleRequest; import org.apache.fineract.accounting.rule.service.AccountingRuleReadPlatformService; import org.apache.fineract.commands.domain.CommandWrapper; import org.apache.fineract.commands.service.CommandWrapperBuilder; @@ -81,11 +79,6 @@ import org.springframework.stereotype.Component; @RequiredArgsConstructor public class AccountingRuleApiResource { - private static final Set RESPONSE_DATA_PARAMETERS = new HashSet<>( - Arrays.asList("id", "officeId", "officeName", "accountToDebitId", "accountToCreditId", "name", "description", "systemDefined", - "allowedCreditTagOptions", "allowedDebitTagOptions", "debitTags", "creditTags", "creditAccounts", "debitAccounts", - "allowMultipleCreditEntries", "allowMultipleDebitEntries", "tag")); - private static final String RESOURCE_NAME_FOR_PERMISSION = "ACCOUNTINGRULE"; private final AccountingRuleReadPlatformService accountingRuleReadPlatformService; @@ -103,17 +96,9 @@ public class AccountingRuleApiResource { @Produces({ MediaType.APPLICATION_JSON }) @Operation(summary = "Retrieve Accounting Rule Details Template", description = "This is a convenience resource. It can be useful when building maintenance user interface screens for client applications. The template data returned consists of any or all of:\n" + "\n" + "Field Defaults\n" + "Allowed Value Lists\n" + "Example Request:\n" + "\n" + "accountingrules/template") - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = AccountingRuleApiResourceSwagger.GetAccountRulesTemplateResponse.class))) }) - public String retrieveTemplate(@Context final UriInfo uriInfo) { - - this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION); - - AccountingRuleData accountingRuleData = null; - accountingRuleData = handleTemplate(accountingRuleData); - - final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - return this.apiJsonSerializerService.serialize(settings, accountingRuleData, RESPONSE_DATA_PARAMETERS); + public AccountingRuleData retrieveTemplate() { + context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION); + return handleTemplate(null); } @GET @@ -121,30 +106,16 @@ public class AccountingRuleApiResource { @Produces({ MediaType.APPLICATION_JSON }) @Operation(summary = "Retrieve Accounting Rules", description = "Returns the list of defined accounting rules.\n" + "\n" + "Example Requests:\n" + "\n" + "accountingrules") - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = AccountingRuleApiResourceSwagger.GetAccountRulesResponse.class)))) }) - public String retrieveAllAccountingRules(@Context final UriInfo uriInfo) { - - final AppUser currentUser = this.context.authenticatedUser(); + public List retrieveAllAccountingRules(@Context final UriInfo uriInfo) { + final AppUser currentUser = context.authenticatedUser(); currentUser.validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION); final String hierarchy = currentUser.getOffice().getHierarchy(); final String hierarchySearchString = hierarchy + "%"; final Set associationParameters = ApiParameterHelper.extractAssociationsForResponseIfProvided(uriInfo.getQueryParameters()); - boolean isAssociationParametersExists = false; - if (!associationParameters.isEmpty()) { - if (associationParameters.contains("all")) { - isAssociationParametersExists = true; // If true, retrieve - // additional fields for - // journal entry form. - } - } - final List accountingRuleDatas = this.accountingRuleReadPlatformService - .retrieveAllAccountingRules(hierarchySearchString, isAssociationParametersExists); - - final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - return this.apiJsonSerializerService.serialize(settings, accountingRuleDatas, RESPONSE_DATA_PARAMETERS); + boolean isAssociationParametersExists = !associationParameters.isEmpty() && associationParameters.contains("all"); + return accountingRuleReadPlatformService.retrieveAllAccountingRules(hierarchySearchString, isAssociationParametersExists); } @GET @@ -153,21 +124,15 @@ public class AccountingRuleApiResource { @Produces({ MediaType.APPLICATION_JSON }) @Operation(summary = "Retrieve a Accounting rule", description = "Returns the details of a defined Accounting rule.\n" + "\n" + "Example Requests:\n" + "\n" + "accountingrules/1") - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = AccountingRuleData.class))) }) - public String retreiveAccountingRule( + public AccountingRuleData retreiveAccountingRule( @PathParam("accountingRuleId") @Parameter(description = "accountingRuleId") final Long accountingRuleId, @Context final UriInfo uriInfo) { - this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION); - final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - AccountingRuleData accountingRuleData = this.accountingRuleReadPlatformService.retrieveAccountingRuleById(accountingRuleId); - if (settings.isTemplate()) { - accountingRuleData = handleTemplate(accountingRuleData); - } - - return this.apiJsonSerializerService.serialize(settings, accountingRuleData, RESPONSE_DATA_PARAMETERS); + final AccountingRuleData accountingRuleData = accountingRuleReadPlatformService.retrieveAccountingRuleById(accountingRuleId); + return settings.isTemplate() ? handleTemplate(accountingRuleData) : accountingRuleData; } @POST @@ -176,16 +141,14 @@ public class AccountingRuleApiResource { @Operation(summary = "Create/Define a Accounting rule", description = "Define a new Accounting rule.\n" + "\n" + "Mandatory Fields\n" + "name, officeId,\n" + "accountToDebit OR debitTags,\n" + "accountToCredit OR creditTags.\n" + "\n" + "Optional Fields\n" + "description") - @RequestBody(content = @Content(schema = @Schema(implementation = AccountingRuleApiResourceSwagger.PostAccountingRulesRequest.class))) + @RequestBody(content = @Content(schema = @Schema(implementation = AccountRuleRequest.class))) @ApiResponses({ @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = AccountingRuleApiResourceSwagger.PostAccountingRulesResponse.class))) }) - public String createAccountingRule(@Parameter(hidden = true) final String jsonRequestBody) { + public CommandProcessingResult createAccountingRule(@Parameter(hidden = true) AccountRuleRequest accountRuleRequest) { + final CommandWrapper commandRequest = new CommandWrapperBuilder().createAccountingRule() + .withJson(apiJsonSerializerService.serialize(accountRuleRequest)).build(); - final CommandWrapper commandRequest = new CommandWrapperBuilder().createAccountingRule().withJson(jsonRequestBody).build(); - - final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); - - return this.apiJsonSerializerService.serialize(result); + return commandsSourceWritePlatformService.logCommandSource(commandRequest); } @PUT @@ -193,18 +156,16 @@ public class AccountingRuleApiResource { @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @Operation(summary = "Update a Accounting Rule", description = "Updates the details of a Accounting rule.") - @RequestBody(content = @Content(schema = @Schema(implementation = AccountingRuleApiResourceSwagger.PutAccountingRulesRequest.class))) + @RequestBody(content = @Content(schema = @Schema(implementation = AccountRuleRequest.class))) @ApiResponses({ @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = AccountingRuleApiResourceSwagger.PutAccountingRulesResponse.class))) }) - public String updateAccountingRule( + public CommandProcessingResult updateAccountingRule( @PathParam("accountingRuleId") @Parameter(description = "accountingRuleId") final Long accountingRuleId, - @Parameter(hidden = true) final String jsonRequestBody) { - final CommandWrapper commandRequest = new CommandWrapperBuilder().updateAccountingRule(accountingRuleId).withJson(jsonRequestBody) - .build(); + @Parameter(hidden = true) AccountRuleRequest accountRuleRequest) { + final CommandWrapper commandRequest = new CommandWrapperBuilder().updateAccountingRule(accountingRuleId) + .withJson(apiJsonSerializerService.serialize(accountRuleRequest)).build(); - final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); - - return this.apiJsonSerializerService.serialize(result); + return commandsSourceWritePlatformService.logCommandSource(commandRequest); } @DELETE @@ -214,55 +175,38 @@ public class AccountingRuleApiResource { @Operation(summary = "Delete a Accounting Rule", description = "Deletes a Accounting rule.") @ApiResponses({ @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = AccountingRuleApiResourceSwagger.DeleteAccountingRulesResponse.class))) }) - public String deleteAccountingRule( + public CommandProcessingResult deleteAccountingRule( @PathParam("accountingRuleId") @Parameter(description = "accountingRuleId") final Long accountingRuleId) { final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteAccountingRule(accountingRuleId).build(); - final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); - - return this.apiJsonSerializerService.serialize(result); + return commandsSourceWritePlatformService.logCommandSource(commandRequest); } private AccountingRuleData handleTemplate(AccountingRuleData accountingRuleData) { - final List allowedAccounts = this.accountReadPlatformService.retrieveAllEnabledDetailGLAccounts(); - final List allowedOffices = (List) this.officeReadPlatformService.retrieveAllOfficesForDropdown(); - final Collection allowedTagOptions = this.codeValueReadPlatformService + final List allowedAccounts = accountReadPlatformService.retrieveAllEnabledDetailGLAccounts(); + final List allowedOffices = (List) officeReadPlatformService.retrieveAllOfficesForDropdown(); + final Collection allowedTagOptions = codeValueReadPlatformService .retrieveCodeValuesByCode(AccountingConstants.ASSESTS_TAG_OPTION_CODE_NAME); allowedTagOptions - .addAll(this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.LIABILITIES_TAG_OPTION_CODE_NAME)); - allowedTagOptions - .addAll(this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.EQUITY_TAG_OPTION_CODE_NAME)); - allowedTagOptions - .addAll(this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.INCOME_TAG_OPTION_CODE_NAME)); - allowedTagOptions - .addAll(this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.EXPENSES_TAG_OPTION_CODE_NAME)); + .addAll(codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.LIABILITIES_TAG_OPTION_CODE_NAME)); + allowedTagOptions.addAll(codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.EQUITY_TAG_OPTION_CODE_NAME)); + allowedTagOptions.addAll(codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.INCOME_TAG_OPTION_CODE_NAME)); + allowedTagOptions.addAll(codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.EXPENSES_TAG_OPTION_CODE_NAME)); if (accountingRuleData == null) { - - final Collection allowedCreditTagOptions = allowedTagOptions; - final Collection allowedDebitTagOptions = allowedTagOptions; - accountingRuleData = new AccountingRuleData().setAllowedOffices(allowedOffices).setAllowedAccounts(allowedAccounts) - .setAllowedCreditTagOptions(allowedCreditTagOptions).setAllowedDebitTagOptions(allowedDebitTagOptions); + .setAllowedCreditTagOptions(allowedTagOptions).setAllowedDebitTagOptions(allowedTagOptions); } else { - final Collection allowedCreditTagOptions; - final Collection allowedDebitTagOptions; - - if (accountingRuleData.getCreditTags() != null) { - allowedCreditTagOptions = retrieveSelectedTags(allowedTagOptions, accountingRuleData.getCreditTags()); - } else { - allowedCreditTagOptions = allowedTagOptions; - } - - if (accountingRuleData.getDebitTags() != null) { - allowedDebitTagOptions = retrieveSelectedTags(allowedTagOptions, accountingRuleData.getDebitTags()); - } else { - allowedDebitTagOptions = allowedTagOptions; - } + final Collection allowedCreditTagOptions = accountingRuleData.getCreditTags() != null + ? retrieveSelectedTags(allowedTagOptions, accountingRuleData.getCreditTags()) + : allowedTagOptions; + final Collection allowedDebitTagOptions = accountingRuleData.getDebitTags() != null + ? retrieveSelectedTags(allowedTagOptions, accountingRuleData.getDebitTags()) + : allowedTagOptions; accountingRuleData = new AccountingRuleData().setId(accountingRuleData.getId()).setOfficeId(accountingRuleData.getOfficeId()) .setOfficeName(accountingRuleData.getOfficeName()).setName(accountingRuleData.getName()) diff --git a/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResourceSwagger.java b/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResourceSwagger.java index e5c8979aa..7a1cb92e2 100644 --- a/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResourceSwagger.java +++ b/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/api/AccountingRuleApiResourceSwagger.java @@ -19,11 +19,6 @@ package org.apache.fineract.accounting.rule.api; import io.swagger.v3.oas.annotations.media.Schema; -import java.util.ArrayList; -import java.util.List; -import org.apache.fineract.accounting.glaccount.data.GLAccountData; -import org.apache.fineract.accounting.rule.data.AccountingTagRuleData; -import org.apache.fineract.organisation.office.data.OfficeData; /** * Created by sanyam on 26/7/17. @@ -34,57 +29,6 @@ final class AccountingRuleApiResourceSwagger { // For Swagger Documentation } - @Schema(description = "GetAccountRulesResponse") - public static final class GetAccountRulesResponse { - - private GetAccountRulesResponse() { - - } - - public Long id; - public Long officeId; - public String officeName; - public String name; - public String description; - public boolean systemDefined; - public boolean allowMultipleDebitEntries; - public boolean allowMultipleCreditEntries; - public List creditTags; - public List debitTags; - } - - @Schema(description = "GetAccountRulesTemplateResponse") - public static final class GetAccountRulesTemplateResponse { - - private GetAccountRulesTemplateResponse() { - - } - - @Schema(example = "false") - public boolean systemDefined; - public List allowedOffices = new ArrayList(); - public List allowedAccounts = new ArrayList(); - } - - @Schema(description = "PostAccountingRulesRequest") - public static final class PostAccountingRulesRequest { - - private PostAccountingRulesRequest() { - - } - - @Schema(example = "test") - public String name; - @Schema(example = "1") - public Long officeId; - @Schema(example = "21") - public Long accountToDebit; - @Schema(example = "9") - public Long accountToCredit; - @Schema(example = "Employee salary") - public String description; - } - @Schema(description = "PostAccountingRulesResponse") public static final class PostAccountingRulesResponse { @@ -98,17 +42,6 @@ final class AccountingRuleApiResourceSwagger { public Long resourceId; } - @Schema(description = "PutAccountingRulesRequest") - public static final class PutAccountingRulesRequest { - - private PutAccountingRulesRequest() { - - } - - @Schema(example = "Employee Salary posting rule") - public String name; - } - @Schema(description = "PutAccountingRulesResponse") public static final class PutAccountingRulesResponse { diff --git a/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/data/request/AccountRuleRequest.java b/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/data/request/AccountRuleRequest.java new file mode 100644 index 000000000..0b83aea6f --- /dev/null +++ b/fineract-accounting/src/main/java/org/apache/fineract/accounting/rule/data/request/AccountRuleRequest.java @@ -0,0 +1,30 @@ +/** + * 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.accounting.rule.data.request; + +import java.io.Serial; +import java.io.Serializable; + +public record AccountRuleRequest(String name, Long officeId, Long accountToDebit, Long accountToCredit, + String description) implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + +} diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java index 9d6698dc0..049384c4e 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java @@ -27,7 +27,7 @@ import io.restassured.http.ContentType; import io.restassured.specification.RequestSpecification; import io.restassured.specification.ResponseSpecification; import java.util.ArrayList; -import org.apache.fineract.client.models.GetAccountRulesResponse; +import org.apache.fineract.client.models.AccountingRuleData; import org.apache.fineract.client.models.GetOfficesResponse; import org.apache.fineract.client.models.PostAccountingRulesResponse; import org.apache.fineract.integrationtests.common.OfficeHelper; @@ -68,7 +68,7 @@ public class AccountingRuleIntegrationTest { // when final PostAccountingRulesResponse accountingRule = accountRuleHelper.createAccountRule(headOffice.getId(), accountToCredit, accountToDebit); - final ArrayList accountingRules = accountRuleHelper.getAccountingRules(); + final ArrayList accountingRules = accountRuleHelper.getAccountingRules(); // then assertNotNull(accountingRule); diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java index a725931c9..75123ce3e 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java @@ -24,7 +24,7 @@ import io.restassured.specification.RequestSpecification; import io.restassured.specification.ResponseSpecification; import java.lang.reflect.Type; import java.util.ArrayList; -import org.apache.fineract.client.models.GetAccountRulesResponse; +import org.apache.fineract.client.models.AccountingRuleData; import org.apache.fineract.client.models.PostAccountingRulesResponse; import org.apache.fineract.client.util.JSON; import org.apache.fineract.integrationtests.common.Utils; @@ -51,9 +51,9 @@ public class AccountRuleHelper { // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, // org.apache.fineract.client.models.PostLoansLoanIdRequest) @Deprecated(forRemoval = true) - public ArrayList getAccountingRules() { + public ArrayList getAccountingRules() { final String response = Utils.performServerGet(this.requestSpec, this.responseSpec, ACCOUNTINGRULES_URL); - Type accountRuleListType = new TypeToken>() {}.getType(); + Type accountRuleListType = new TypeToken>() {}.getType(); return GSON.fromJson(response, accountRuleListType); }