FINERACT-1992 Emit LoanAccountDelinquencyPauseChangedBusinessEvent

This commit is contained in:
Peter Bagrij
2023-11-22 15:15:00 +01:00
committed by Adam Saghy
parent 967960d6c2
commit bef6595032
17 changed files with 295 additions and 69 deletions
@@ -24,7 +24,8 @@ import org.apache.fineract.portfolio.loanaccount.data.LoanAccountData;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(config = AvroMapperConfig.class, uses = { LoanTransactionDataMapper.class, LoanChargeDataMapper.class })
@Mapper(config = AvroMapperConfig.class, uses = { LoanTransactionDataMapper.class, LoanChargeDataMapper.class,
LoanDelinquencyPausePeriodMapper.class })
public interface LoanAccountDataMapper {
// TODO: avoid prefix "is" in class attributes; I would recommend to fix this also in the Avro structures
@@ -0,0 +1,31 @@
/**
* 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.infrastructure.event.external.service.serialization.mapper.loan;
import org.apache.fineract.avro.loan.v1.DelinquencyPausePeriodV1;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.support.AvroMapperConfig;
import org.apache.fineract.portfolio.loanaccount.data.DelinquencyPausePeriod;
import org.mapstruct.Mapper;
@Mapper(config = AvroMapperConfig.class)
public interface LoanDelinquencyPausePeriodMapper {
DelinquencyPausePeriodV1 map(DelinquencyPausePeriod source);
}
@@ -30,7 +30,6 @@ import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.infrastructure.core.service.MathUtil;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.portfolio.delinquency.data.DelinquencyBucketData;
@@ -52,6 +51,7 @@ import org.apache.fineract.portfolio.delinquency.mapper.DelinquencyRangeMapper;
import org.apache.fineract.portfolio.delinquency.mapper.LoanDelinquencyTagMapper;
import org.apache.fineract.portfolio.delinquency.validator.LoanDelinquencyActionData;
import org.apache.fineract.portfolio.loanaccount.data.CollectionData;
import org.apache.fineract.portfolio.loanaccount.data.DelinquencyPausePeriod;
import org.apache.fineract.portfolio.loanaccount.data.InstallmentLevelDelinquency;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository;
@@ -205,23 +205,20 @@ public class DelinquencyReadPlatformServiceImpl implements DelinquencyReadPlatfo
effective.add(loanDelinquencyActionData);
}
// order them by start date, filter out the future items
Optional<LoanDelinquencyActionData> last = effective.stream()
.sorted(Comparator.comparing(LoanDelinquencyActionData::getStartDate))
.filter(e -> !DateUtils.isAfter(e.getStartDate(), businessDate)) // keep only the present and the
// past ones
.reduce((first, second) -> second);
// enrich collectionData
last.ifPresent(action -> {
collectionData
.setDelinquencyCalculationPaused(!action.startDate.isAfter(businessDate) && !businessDate.isAfter(action.endDate));
collectionData.setDelinquencyPausePeriodStartDate(action.getStartDate());
collectionData.setDelinquencyPausePeriodEndDate(action.getEndDate());
});
// order them by start date, and convert to DelinquencyPausePeriod objects
List<DelinquencyPausePeriod> result = effective.stream() //
.sorted(Comparator.comparing(LoanDelinquencyActionData::getStartDate)) //
.map(lda -> toDelinquencyPausePeriod(businessDate, lda)).toList(); //
collectionData.setDelinquencyPausePeriods(result);
}
}
@NotNull
private static DelinquencyPausePeriod toDelinquencyPausePeriod(LocalDate businessDate, LoanDelinquencyActionData lda) {
return new DelinquencyPausePeriod(!lda.getStartDate().isAfter(businessDate) && !businessDate.isAfter(lda.getEndDate()),
lda.getStartDate(), lda.getEndDate());
}
private Optional<LoanDelinquencyAction> findMatchingResume(LoanDelinquencyAction pause, List<LoanDelinquencyAction> resumes) {
if (resumes != null && resumes.size() > 0) {
for (LoanDelinquencyAction resume : resumes) {
@@ -33,6 +33,7 @@ import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
import org.apache.fineract.infrastructure.core.exception.PlatformDataIntegrityException;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.infrastructure.event.business.domain.loan.LoanAccountDelinquencyPauseChangedBusinessEvent;
import org.apache.fineract.infrastructure.event.business.domain.loan.LoanDelinquencyRangeChangeBusinessEvent;
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
import org.apache.fineract.portfolio.delinquency.api.DelinquencyApiConstants;
@@ -62,6 +63,7 @@ import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Slf4j
@@ -205,6 +207,7 @@ public class DelinquencyWritePlatformServiceImpl implements DelinquencyWritePlat
}
@Override
@Transactional
public CommandProcessingResult createDelinquencyAction(Long loanId, JsonCommand command) {
final Loan loan = this.loanRepository.findOneWithNotFoundDetection(loanId);
final LocalDate businessDate = DateUtils.getBusinessLocalDate();
@@ -216,7 +219,7 @@ public class DelinquencyWritePlatformServiceImpl implements DelinquencyWritePlat
parsedDelinquencyAction.setLoan(loan);
LoanDelinquencyAction saved = loanDelinquencyActionRepository.saveAndFlush(parsedDelinquencyAction);
businessEventNotifierService.notifyPostBusinessEvent(new LoanAccountDelinquencyPauseChangedBusinessEvent(loan));
return new CommandProcessingResultBuilder().withCommandId(command.commandId()) //
.withEntityId(saved.getId()) //
.withOfficeId(loan.getOfficeId()) //
@@ -990,20 +990,27 @@ final class LoansApiResourceSwagger {
@Schema(example = "100.000000")
public Double lastRepaymentAmount;
@Schema(example = "true")
public Boolean delinquencyCalculationPaused;
@Schema(example = "[2022, 07, 05]")
public LocalDate delinquencyPausePeriodStartDate;
@Schema(example = "[2022, 07, 10]")
public LocalDate delinquencyPausePeriodEndDate;
@Schema(description = "List of GetLoansLoanIdDelinquencyPausePeriod")
public List<GetLoansLoanIdDelinquencyPausePeriod> delinquencyPausePeriods;
@Schema(description = "List of GetLoansLoanIdLoanInstallmentLevelDelinquency")
public List<GetLoansLoanIdLoanInstallmentLevelDelinquency> installmentLevelDelinquency;
}
static final class GetLoansLoanIdDelinquencyPausePeriod {
@Schema(example = "true")
public Boolean active;
@Schema(example = "[2022, 07, 05]")
public LocalDate pausePeriodStart;
@Schema(example = "[2022, 07, 10]")
public LocalDate pausePeriodEnd;
}
static final class GetLoansLoanIdLoanInstallmentLevelDelinquency {
private GetLoansLoanIdLoanInstallmentLevelDelinquency() {}
@@ -44,14 +44,12 @@ public final class CollectionData {
private LocalDate lastRepaymentDate;
private BigDecimal lastRepaymentAmount;
public boolean delinquencyCalculationPaused;
public LocalDate delinquencyPausePeriodStartDate;
public LocalDate delinquencyPausePeriodEndDate;
public Collection<DelinquencyPausePeriod> delinquencyPausePeriods;
public Collection<InstallmentLevelDelinquency> installmentLevelDelinquency;
public static CollectionData template() {
final BigDecimal zero = BigDecimal.ZERO;
return new CollectionData(zero, 0L, null, 0L, null, zero, null, zero, null, zero, false, null, null, null);
return new CollectionData(zero, 0L, null, 0L, null, zero, null, zero, null, zero, null, null);
}
}
@@ -0,0 +1,36 @@
/**
* 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.loanaccount.data;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@AllArgsConstructor
@ToString
@Getter
@EqualsAndHashCode
public class DelinquencyPausePeriod {
private boolean active;
private LocalDate pausePeriodStart;
private LocalDate pausePeriodEnd;
}