Skip to content

Commit

Permalink
Merge pull request #9 from openMF/enhancement/fd_account_undo_activate
Browse files Browse the repository at this point in the history
Fixed Deposit Account Enhancement to Undo Active account
  • Loading branch information
josehernandezfintecheandomx authored Oct 20, 2023
2 parents 0871bcf + d10daf8 commit 5287d44
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2400,6 +2400,15 @@ public CommandWrapperBuilder fixedDepositAccountActivation(final Long accountId)
return this;
}

public CommandWrapperBuilder fixedDepositAccountUndoActivation(final Long accountId) {
this.actionName = "UNDO_ACTIVATE";
this.entityName = "FIXEDDEPOSITACCOUNT";
this.savingsId = accountId;
this.entityId = accountId;
this.href = "/fixeddepositaccounts/" + accountId + "?command=activate";
return this;
}

public CommandWrapperBuilder closeFixedDepositAccount(final Long accountId) {
this.actionName = "CLOSE";
this.entityName = "FIXEDDEPOSITACCOUNT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class InterestRateChartReadPlatformServiceImpl implements InterestRateChartReadPlatformService {

private final PlatformSecurityContext context;
Expand Down Expand Up @@ -100,6 +103,8 @@ public Collection<InterestRateChartData> retrieveAllWithSlabs(Long productId) {
sql.append("WHEN NOT irc.is_primary_grouping_by_amount then ircd.amount_range_to ");
sql.append("END");

log.info("SQL: {}", sql.toString());

return this.jdbcTemplate.query(
con -> con.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE),
ps -> ps.setLong(1, productId), this.chartExtractor);
Expand Down Expand Up @@ -208,7 +213,7 @@ private InterestRateChartExtractor(DatabaseSpecificSQLGenerator sqlGenerator) {
.append("from ")
.append("m_interest_rate_chart irc left join m_interest_rate_slab ircd on irc.id=ircd.interest_rate_chart_id ")
.append(" left join m_interest_incentives iri on iri.interest_rate_slab_id = ircd.id ")
.append(" left join m_code_value code on " + sqlGenerator.castChar("code.id") + " = iri.attribute_value ")
.append(" left join m_code_value code on code.id = iri.attribute_value ")
.append("left join m_currency curr on ircd.currency_code= curr.code ")
.append("left join m_deposit_product_interest_rate_chart dpirc on irc.id=dpirc.interest_rate_chart_id ")
.append("left join m_savings_product sp on sp.id=dpirc.deposit_product_id ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.apache.fineract.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
import org.apache.fineract.infrastructure.core.service.CommandParameterUtil;
import org.apache.fineract.infrastructure.core.service.Page;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.account.data.PortfolioAccountData;
Expand Down Expand Up @@ -191,7 +192,7 @@ public String retrieveOne(@PathParam("accountId") @Parameter(description = "acco

this.context.authenticatedUser().validateHasReadPermission(DepositsApiConstants.FIXED_DEPOSIT_ACCOUNT_RESOURCE_NAME);

if (!(is(chargeStatus, "all") || is(chargeStatus, "active") || is(chargeStatus, "inactive"))) {
if (!(CommandParameterUtil.is(chargeStatus, "all") || CommandParameterUtil.is(chargeStatus, "active") || CommandParameterUtil.is(chargeStatus, "inactive"))) {
throw new UnrecognizedQueryParamException("status", chargeStatus, new Object[] { "all", "active", "inactive" });
}

Expand Down Expand Up @@ -335,35 +336,28 @@ public String handleCommands(@PathParam("accountId") @Parameter(description = "a

final CommandWrapperBuilder builder = new CommandWrapperBuilder().withJson(jsonApiRequest);

CommandProcessingResult result = null;
if (is(commandParam, "reject")) {
final CommandWrapper commandRequest = builder.rejectFixedDepositAccountApplication(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "withdrawnByApplicant")) {
final CommandWrapper commandRequest = builder.withdrawFixedDepositAccountApplication(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "approve")) {
final CommandWrapper commandRequest = builder.approveFixedDepositAccountApplication(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "undoapproval")) {
final CommandWrapper commandRequest = builder.undoFixedDepositAccountApplication(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "activate")) {
final CommandWrapper commandRequest = builder.fixedDepositAccountActivation(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "calculateInterest")) {
final CommandWrapper commandRequest = builder.withNoJsonBody().fixedDepositAccountInterestCalculation(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "postInterest")) {
final CommandWrapper commandRequest = builder.fixedDepositAccountInterestPosting(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "close")) {
final CommandWrapper commandRequest = builder.closeFixedDepositAccount(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "prematureClose")) {
final CommandWrapper commandRequest = builder.prematureCloseFixedDepositAccount(accountId).build();
result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
} else if (is(commandParam, "calculatePrematureAmount")) {
CommandWrapper commandRequest = null;
if (CommandParameterUtil.is(commandParam, "reject")) {
commandRequest = builder.rejectFixedDepositAccountApplication(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "withdrawnByApplicant")) {
commandRequest = builder.withdrawFixedDepositAccountApplication(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "approve")) {
commandRequest = builder.approveFixedDepositAccountApplication(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "undoapproval")) {
commandRequest = builder.undoFixedDepositAccountApplication(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "activate")) {
commandRequest = builder.fixedDepositAccountActivation(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "undoactivate")) {
commandRequest = builder.fixedDepositAccountUndoActivation(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "calculateInterest")) {
commandRequest = builder.withNoJsonBody().fixedDepositAccountInterestCalculation(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "postInterest")) {
commandRequest = builder.fixedDepositAccountInterestPosting(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "close")) {
commandRequest = builder.closeFixedDepositAccount(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "prematureClose")) {
commandRequest = builder.prematureCloseFixedDepositAccount(accountId).build();
} else if (CommandParameterUtil.is(commandParam, "calculatePrematureAmount")) {
final JsonElement parsedQuery = this.fromJsonHelper.parse(apiRequestBodyAsJson);
final JsonQuery query = JsonQuery.from(apiRequestBodyAsJson, parsedQuery, this.fromJsonHelper);
final DepositAccountData account = this.accountPreMatureCalculationPlatformService.calculatePreMatureAmount(accountId, query,
Expand All @@ -373,19 +367,16 @@ public String handleCommands(@PathParam("accountId") @Parameter(description = "a
DepositsApiConstants.FIXED_DEPOSIT_ACCOUNT_RESPONSE_DATA_PARAMETERS);
}

if (result == null) {
if (commandRequest == null) {
throw new UnrecognizedQueryParamException("command", commandParam,
new Object[] { "reject", "withdrawnByApplicant", "approve", "undoapproval", "activate", "calculateInterest",
"postInterest", "close", "prematureClose", "calculatePrematureAmount" });
new Object[] { "reject", "withdrawnByApplicant", "approve", "undoapproval", "activate", "undoactivate",
"calculateInterest", "postInterest", "close", "prematureClose", "calculatePrematureAmount" });
}


final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
return this.toApiJsonSerializer.serialize(result);
}

private boolean is(final String commandParam, final String commandValue) {
return StringUtils.isNotBlank(commandParam) && commandParam.trim().equalsIgnoreCase(commandValue);
}

@DELETE
@Path("{accountId}")
@Consumes({ MediaType.APPLICATION_JSON })
Expand All @@ -411,7 +402,7 @@ public String accountClosureTemplate(@PathParam("accountId") @Parameter(descript

this.context.authenticatedUser().validateHasReadPermission(DepositsApiConstants.FIXED_DEPOSIT_ACCOUNT_RESOURCE_NAME);
DepositAccountData account = null;
if (is(commandParam, "close")) {
if (CommandParameterUtil.is(commandParam, "close")) {
account = this.depositAccountReadPlatformService.retrieveOneWithClosureTemplate(DepositAccountType.FIXED_DEPOSIT, accountId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@ public Map<String, Object> activate(final AppUser currentUser, final JsonCommand
return actualChanges;
}

@Override
public Map<String, Object> undoActivate(final AppUser currentUser, final JsonCommand command, final LocalDate tenantsTodayDate) {
return super.undoActivate(currentUser, command, tenantsTodayDate);
}

private LocalDate depositStartDate() {
// TODO: Support to add deposit start date which can be a date after
// account activation date.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,98 @@ public Map<String, Object> activate(final AppUser currentUser, final JsonCommand
return actualChanges;
}

protected Map<String, Object> undoActivate(final AppUser currentUser, final JsonCommand command, final LocalDate operationDate) {

final Map<String, Object> actualChanges = new LinkedHashMap<>();

final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
.resource(depositAccountType().resourceName() + SavingsApiConstants.activateAction);

final SavingsAccountStatusType currentStatus = SavingsAccountStatusType.fromInt(this.status);
if (!SavingsAccountStatusType.ACTIVE.hasStateOf(currentStatus)) {

baseDataValidator.reset().parameter(SavingsApiConstants.activatedOnDateParamName)
.failWithCodeNoParameterAddedToErrorCode("not.in.active.state");

if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException(dataValidationErrors);
}
}

this.status = SavingsAccountStatusType.APPROVED.getValue();
actualChanges.put(SavingsApiConstants.statusParamName, SavingsEnumerations.status(this.status));

this.rejectedOnDate = null;
this.rejectedBy = null;
this.withdrawnOnDate = null;
this.withdrawnBy = null;
this.closedOnDate = null;
this.closedBy = null;
this.activatedOnDate = null;
this.activatedBy = currentUser;
this.lockedInUntilDate = calculateDateAccountIsLockedUntil(getActivationLocalDate());

if (this.client != null && this.client.isActivatedAfter(operationDate)) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(command.dateFormat()).withLocale(command.extractLocale());
final String dateAsString = formatter.format(this.client.getActivationLocalDate());
baseDataValidator.reset().parameter(SavingsApiConstants.activatedOnDateParamName).value(dateAsString)
.failWithCodeNoParameterAddedToErrorCode("cannot.be.before.client.activation.date");
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException(dataValidationErrors);
}
}

if (this.group != null && this.group.isActivatedAfter(operationDate)) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(command.dateFormat()).withLocale(command.extractLocale());
final String dateAsString = formatter.format(this.client.getActivationLocalDate());
baseDataValidator.reset().parameter(SavingsApiConstants.activatedOnDateParamName).value(dateAsString)
.failWithCodeNoParameterAddedToErrorCode("cannot.be.before.group.activation.date");
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException(dataValidationErrors);
}
}

final LocalDate approvalDate = getApprovedOnLocalDate();
if (operationDate.isBefore(approvalDate)) {

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(command.dateFormat()).withLocale(command.extractLocale());
final String dateAsString = formatter.format(approvalDate);

baseDataValidator.reset().parameter(SavingsApiConstants.activatedOnDateParamName).value(dateAsString)
.failWithCodeNoParameterAddedToErrorCode("cannot.be.before.approval.date");

if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException(dataValidationErrors);
}
}

validateActivityNotBeforeClientOrGroupTransferDate(SavingsEvent.SAVINGS_UNDO_ACTIVATE, operationDate);

updateSavingsToApprovedState();

return actualChanges;
}

protected void updateSavingsToApprovedState() {
reverseExistingTransactions();

for (SavingsAccountCharge charge : this.charges()) {
charge.resetToOriginal(currency);
}
}

protected void reverseExistingTransactions() {
Collection<SavingsAccountTransaction> retainTransactions = new ArrayList<>();
for (final SavingsAccountTransaction transaction : this.transactions) {
transaction.reverse();
if (transaction.getId() != null) {
retainTransactions.add(transaction);
}
}
this.transactions.retainAll(retainTransactions);
}

public void processAccountUponActivation(final boolean isSavingsInterestPostingAtCurrentPeriodEnd,
final Integer financialYearBeginningMonth, final AppUser user) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum SavingsEvent {
SAVINGS_APPLICATION_APPROVED("application.approval"), //
SAVINGS_APPLICATION_APPROVAL_UNDO("application.approval.undo"), //
SAVINGS_ACTIVATE("activate"), //
SAVINGS_UNDO_ACTIVATE("undo.activate"), //
SAVINGS_DEPOSIT("deposit"), //
SAVINGS_WITHDRAWAL("withdraw"), //
SAVINGS_POST_INTEREST("interest.post"), //
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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.savings.handler;

import org.apache.fineract.commands.annotation.CommandType;
import org.apache.fineract.commands.handler.NewCommandSourceHandler;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
@CommandType(entity = "FIXEDDEPOSITACCOUNT", action = "UNDO_ACTIVATE")
public class UndoActivationFixedDepositAccountCommandHandler implements NewCommandSourceHandler {

private final DepositAccountWritePlatformService depositAccountWritePlatformService;

@Transactional
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
return this.depositAccountWritePlatformService.undoActivateFDAccount(command.entityId(), command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private DepositAccountInterestRateChartExtractor(DatabaseSpecificSQLGenerator sq
.append("from ")
.append("m_savings_account_interest_rate_chart irc left join m_savings_account_interest_rate_slab ircd on irc.id=ircd.savings_account_interest_rate_chart_id ")
.append(" left join m_savings_interest_incentives iri on iri.deposit_account_interest_rate_slab_id =ircd.id ")
.append(" left join m_code_value code on " + sqlGenerator.castChar("code.id") + " = iri.attribute_value ")
.append(" left join m_code_value code on code.id = iri.attribute_value ")
.append("left join m_currency curr on ircd.currency_code= curr.code ")
.append("left join m_savings_account sa on irc.savings_account_id=sa.id ");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface DepositAccountWritePlatformService {

CommandProcessingResult activateFDAccount(Long savingsId, JsonCommand command);

CommandProcessingResult undoActivateFDAccount(Long savingsId, JsonCommand command);

CommandProcessingResult activateRDAccount(Long savingsId, JsonCommand command);

CommandProcessingResult updateDepositAmountForRDAccount(Long savingsId, JsonCommand command);
Expand Down
Loading

0 comments on commit 5287d44

Please sign in to comment.