FINERACT-2080: Extract accountNumberRequiresAutoGeneration from loan entity

This commit is contained in:
Oleksii Novikov
2025-04-24 11:55:36 +02:00
committed by Adam Saghy
parent 205b39ddf0
commit e5a23406ca
3 changed files with 66 additions and 73 deletions
@@ -35,7 +35,6 @@ import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne; import jakarta.persistence.OneToOne;
import jakarta.persistence.OrderBy; import jakarta.persistence.OrderBy;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.persistence.UniqueConstraint; import jakarta.persistence.UniqueConstraint;
import jakarta.persistence.Version; import jakarta.persistence.Version;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@@ -322,9 +321,6 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom<Long> {
@Embedded @Embedded
private LoanSummary summary; private LoanSummary summary;
@Transient
private boolean accountNumberRequiresAutoGeneration;
@Setter() @Setter()
@Column(name = "principal_amount_proposed", scale = 6, precision = 19, nullable = false) @Column(name = "principal_amount_proposed", scale = 6, precision = 19, nullable = false)
private BigDecimal proposedPrincipal; private BigDecimal proposedPrincipal;
@@ -506,7 +502,6 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom<Long> {
if (StringUtils.isBlank(accountNo)) { if (StringUtils.isBlank(accountNo)) {
this.accountNumber = new RandomPasswordGenerator(19).generate(); this.accountNumber = new RandomPasswordGenerator(19).generate();
this.accountNumberRequiresAutoGeneration = true;
} else { } else {
this.accountNumber = accountNo; this.accountNumber = accountNo;
} }
@@ -841,11 +836,6 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom<Long> {
this.loanProduct = loanProduct; this.loanProduct = loanProduct;
} }
public void updateAccountNo(final String newAccountNo) {
this.accountNumber = newAccountNo;
this.accountNumberRequiresAutoGeneration = false;
}
public void updateFund(final Fund fund) { public void updateFund(final Fund fund) {
this.fund = fund; this.fund = fund;
} }
@@ -131,7 +131,6 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
@Transactional @Transactional
@Override @Override
public CommandProcessingResult submitApplication(final JsonCommand command) { public CommandProcessingResult submitApplication(final JsonCommand command) {
try { try {
// Validations (prior assembling) // Validations (prior assembling)
this.loanApplicationValidator.validateForCreate(command); this.loanApplicationValidator.validateForCreate(command);
@@ -292,17 +292,22 @@ public class LoanAssemblerImpl implements LoanAssembler {
// TODO: Review... it might be better somewhere else and rethink due to the account number generation logic is // TODO: Review... it might be better somewhere else and rethink due to the account number generation logic is
// intertwined with GLIM logic // intertwined with GLIM logic
@Override @Override
public void accountNumberGeneration(JsonCommand command, Loan loan) { public void accountNumberGeneration(final JsonCommand command, final Loan loan) {
if (loan.isAccountNumberRequiresAutoGeneration()) { final JsonElement element = command.parsedJson();
JsonElement element = command.parsedJson();
final String accountNo = this.fromApiJsonHelper.extractStringNamed("accountNo", element);
final boolean isAccountNumberRequiresAutoGeneration = StringUtils.isBlank(accountNo);
if (!isAccountNumberRequiresAutoGeneration) {
return;
}
final AccountNumberFormat accountNumberFormat = this.accountNumberFormatRepository.findByAccountType(EntityAccountType.LOAN); final AccountNumberFormat accountNumberFormat = this.accountNumberFormatRepository.findByAccountType(EntityAccountType.LOAN);
// TODO: It is really weird to set GLIM info only if account number was not provided // TODO: It is really weird to set GLIM info only if account number was not provided
// if application is of GLIM type // if application is of GLIM type
if (loan.getLoanType().isGLIMAccount()) { if (loan.getLoanType().isGLIMAccount()) {
Group group = loan.getGroup(); Group group = loan.getGroup();
String accountNumber = ""; String accountNumber;
BigDecimal applicationId = BigDecimal.ZERO; BigDecimal applicationId = BigDecimal.ZERO;
Boolean isLastChildApplication = false;
// GLIM specific parameters // GLIM specific parameters
final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(element.getAsJsonObject()); final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(element.getAsJsonObject());
BigDecimal applicationIdFromParam = this.fromApiJsonHelper.extractBigDecimalNamed("applicationId", element, locale); BigDecimal applicationIdFromParam = this.fromApiJsonHelper.extractBigDecimalNamed("applicationId", element, locale);
@@ -312,6 +317,7 @@ public class LoanAssemblerImpl implements LoanAssembler {
} }
Boolean isLastChildApplicationFromParam = this.fromApiJsonHelper.extractBooleanNamed("lastApplication", element); Boolean isLastChildApplicationFromParam = this.fromApiJsonHelper.extractBooleanNamed("lastApplication", element);
boolean isLastChildApplication = false;
if (isLastChildApplicationFromParam != null) { if (isLastChildApplicationFromParam != null) {
isLastChildApplication = isLastChildApplicationFromParam; isLastChildApplication = isLastChildApplicationFromParam;
} }
@@ -335,7 +341,7 @@ public class LoanAssemblerImpl implements LoanAssembler {
GroupLoanIndividualMonitoringAccount glimAccount = glimRepository.findOneByIsAcceptingChildAndApplicationId(true, GroupLoanIndividualMonitoringAccount glimAccount = glimRepository.findOneByIsAcceptingChildAndApplicationId(true,
applicationId); applicationId);
accountNumber = glimAccount.getAccountNumber() + (glimAccount.getChildAccountsCount() + 1); accountNumber = glimAccount.getAccountNumber() + (glimAccount.getChildAccountsCount() + 1);
loan.updateAccountNo(accountNumber); loan.setAccountNumber(accountNumber);
this.glimAccountInfoWritePlatformService.incrementChildAccountCount(glimAccount); this.glimAccountInfoWritePlatformService.incrementChildAccountCount(glimAccount);
loan.setGlim(glimAccount); loan.setGlim(glimAccount);
} else { } else {
@@ -352,16 +358,14 @@ public class LoanAssemblerImpl implements LoanAssembler {
} }
} }
} else { // for applications other than GLIM } else { // for applications other than GLIM
loan.updateAccountNo(this.accountNumberGenerator.generate(loan, accountNumberFormat)); loan.setAccountNumber(this.accountNumberGenerator.generate(loan, accountNumberFormat));
}
} }
} }
private void createAndSetGLIMAccount(BigDecimal totalLoan, Loan loan, AccountNumberFormat accountNumberFormat, Group group, private void createAndSetGLIMAccount(BigDecimal totalLoan, Loan loan, AccountNumberFormat accountNumberFormat, Group group,
BigDecimal applicationId) { BigDecimal applicationId) {
String accountNumber; final String accountNumber = this.accountNumberGenerator.generate(loan, accountNumberFormat);
accountNumber = this.accountNumberGenerator.generate(loan, accountNumberFormat); loan.setAccountNumber(accountNumber + "1");
loan.updateAccountNo(accountNumber + "1");
GroupLoanIndividualMonitoringAccount glimAccount = glimAccountInfoWritePlatformService.createGLIMAccount(accountNumber, group, GroupLoanIndividualMonitoringAccount glimAccount = glimAccountInfoWritePlatformService.createGLIMAccount(accountNumber, group,
totalLoan, 1L, true, LoanStatus.SUBMITTED_AND_PENDING_APPROVAL.getValue(), applicationId); totalLoan, 1L, true, LoanStatus.SUBMITTED_AND_PENDING_APPROVAL.getValue(), applicationId);
loan.setGlim(glimAccount); loan.setGlim(glimAccount);