FINERACT-1734 - Loan Account Delinquency Changed event definition

Extend current Loan Account Delinquency range change event.
This commit is contained in:
Janos Haber
2022-12-19 15:48:40 +01:00
committed by Arnold Gálovics
parent 833dddb551
commit adcd76154e
7 changed files with 359 additions and 16 deletions
@@ -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.generic;
import org.apache.fineract.avro.generic.v1.CurrencyDataV1;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.support.AvroMapperConfig;
import org.apache.fineract.organisation.monetary.data.CurrencyData;
import org.mapstruct.Mapper;
@Mapper(config = AvroMapperConfig.class)
public interface CurrencyDataMapper {
CurrencyDataV1 map(CurrencyData source);
}
@@ -18,6 +18,7 @@
*/
package org.apache.fineract.infrastructure.event.external.service.serialization.mapper.loan;
import org.apache.fineract.avro.loan.v1.LoanChargeDataRangeViewV1;
import org.apache.fineract.avro.loan.v1.LoanChargeDataV1;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.support.AvroMapperConfig;
import org.apache.fineract.portfolio.loanaccount.data.LoanChargeData;
@@ -27,4 +28,6 @@ import org.mapstruct.Mapper;
public interface LoanChargeDataMapper {
LoanChargeDataV1 map(LoanChargeData source);
LoanChargeDataRangeViewV1 mapRangeView(LoanChargeData source);
}
@@ -18,16 +18,27 @@
*/
package org.apache.fineract.infrastructure.event.external.service.serialization.serializer.loan;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.BiFunction;
import lombok.RequiredArgsConstructor;
import org.apache.avro.generic.GenericContainer;
import org.apache.fineract.avro.generator.ByteBufferSerializable;
import org.apache.fineract.avro.loan.v1.DelinquencyRangeDataV1;
import org.apache.fineract.avro.loan.v1.LoanAccountDelinquencyRangeDataV1;
import org.apache.fineract.avro.loan.v1.LoanAmountDataV1;
import org.apache.fineract.avro.loan.v1.LoanChargeDataRangeViewV1;
import org.apache.fineract.infrastructure.event.business.domain.BusinessEvent;
import org.apache.fineract.infrastructure.event.business.domain.loan.LoanDelinquencyRangeChangeBusinessEvent;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.generic.CurrencyDataMapper;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.loan.LoanChargeDataMapper;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.loan.LoanDelinquencyRangeDataMapper;
import org.apache.fineract.infrastructure.event.external.service.serialization.serializer.AbstractBusinessEventSerializer;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import org.apache.fineract.portfolio.loanaccount.data.LoanAccountData;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanInstallmentCharge;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.apache.fineract.portfolio.loanaccount.service.LoanReadPlatformService;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@@ -39,8 +50,13 @@ import org.springframework.stereotype.Component;
public class LoanDelinquencyRangeChangeBusinessEventSerializer extends AbstractBusinessEventSerializer {
private final LoanReadPlatformService service;
private final LoanDelinquencyRangeDataMapper mapper;
private final LoanChargeDataMapper chargeMapper;
private final CurrencyDataMapper currencyMapper;
@Override
protected <T> ByteBufferSerializable toAvroDTO(BusinessEvent<T> rawEvent) {
LoanDelinquencyRangeChangeBusinessEvent event = (LoanDelinquencyRangeChangeBusinessEvent) rawEvent;
@@ -48,8 +64,45 @@ public class LoanDelinquencyRangeChangeBusinessEventSerializer extends AbstractB
Long id = data.getId();
String accountNumber = data.getAccountNo();
String externalId = data.getExternalId().getValue();
MonetaryCurrency loanCurrency = event.get().getCurrency();
List<LoanChargeDataRangeViewV1> charges = event//
.get()//
.getRepaymentScheduleInstallments()//
.stream()//
.flatMap(installment -> installment.getInstallmentCharges().stream())//
.map(LoanInstallmentCharge::getLoancharge)//
.map(charge -> chargeMapper.mapRangeView(charge.toData()))//
.toList();
LoanAmountDataV1 amount = LoanAmountDataV1.newBuilder()//
.setPrincipalAmount(calculateDataSummary(event.get(),
(loan, installment) -> installment.getPrincipalOutstanding(loanCurrency).getAmount()))//
.setFeeAmount(calculateDataSummary(event.get(),
(loan, installment) -> installment.getFeeChargesOutstanding(loanCurrency).getAmount()))//
.setInterestAmount(calculateDataSummary(event.get(),
(loan, installment) -> installment.getInterestOutstanding(loanCurrency).getAmount()))//
.setPenaltyAmount(calculateDataSummary(event.get(),
(loan, installment) -> installment.getPenaltyChargesOutstanding(loanCurrency).getAmount()))//
.setTotalAmount(
calculateDataSummary(event.get(), (loan, installment) -> installment.getTotalOutstanding(loanCurrency).getAmount()))//
.build();
DelinquencyRangeDataV1 delinquencyRange = mapper.map(data.getDelinquencyRange());
return new LoanAccountDelinquencyRangeDataV1(id, accountNumber, externalId, delinquencyRange);
LoanAccountDelinquencyRangeDataV1.Builder builder = LoanAccountDelinquencyRangeDataV1.newBuilder();
return builder//
.setLoanId(id)//
.setLoanAccountNo(accountNumber)//
.setLoanExternalId(externalId)//
.setDelinquencyRange(delinquencyRange)//
.setCharges(charges)//
.setAmount(amount)//
.setCurrency(currencyMapper.map(data.getCurrency()))//
.build();
}
private BigDecimal calculateDataSummary(Loan loan, BiFunction<Loan, LoanRepaymentScheduleInstallment, BigDecimal> mapper) {
return loan.getRepaymentScheduleInstallments().stream().map(installment -> mapper.apply(loan, installment)).reduce(BigDecimal.ZERO,
BigDecimal::add);
}
@Override