FINERACT-2235: add summary data to loans list api

This commit is contained in:
CsengeSóti
2025-04-17 14:02:22 +02:00
committed by Arnold Gálovics
parent 6455311f6c
commit 12617ea1df
11 changed files with 208 additions and 21 deletions
@@ -0,0 +1,35 @@
/**
* 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.math.BigDecimal;
import lombok.Getter;
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
@Getter
public class LoanTransactionBalanceWithLoanId extends LoanTransactionBalance {
private final Long loanId;
public LoanTransactionBalanceWithLoanId(LoanTransactionType transactionType, boolean reversed, boolean manuallyAdjustedOrReversed,
BigDecimal amount, Long loanId) {
super(transactionType, reversed, manuallyAdjustedOrReversed, amount);
this.loanId = loanId;
}
}
@@ -26,11 +26,14 @@ import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.core.jpa.CriteriaQueryFactory;
import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionBalance;
import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionBalanceWithLoanId;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;
@@ -41,10 +44,16 @@ public class LoanSummaryBalancesRepository {
private final EntityManager entityManager;
private final CriteriaQueryFactory criteriaQueryFactory;
public Collection<LoanTransactionBalance> retrieveLoanSummaryBalancesByTransactionType(final Long loanId,
public Collection<LoanTransactionBalanceWithLoanId> retrieveLoanSummaryBalancesByTransactionType(final Long loanId,
final List<LoanTransactionType> transactionTypes) {
return retrieveLoanSummaryBalancesByTransactionType(Arrays.asList(loanId), transactionTypes).getOrDefault(loanId,
new ArrayList<>());
}
public Map<Long, List<LoanTransactionBalanceWithLoanId>> retrieveLoanSummaryBalancesByTransactionType(final List<Long> loanIds,
final List<LoanTransactionType> transactionTypes) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<LoanTransactionBalance> query = cb.createQuery(LoanTransactionBalance.class);
CriteriaQuery<LoanTransactionBalanceWithLoanId> query = cb.createQuery(LoanTransactionBalanceWithLoanId.class);
Root<LoanTransaction> root = query.from(LoanTransaction.class);
@@ -52,7 +61,7 @@ public class LoanSummaryBalancesRepository {
Path<Loan> la = r.get("loan");
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(la.get("id"), loanId)); // Loan
predicates.add(la.get("id").in(loanIds)); // Loans
predicates.add(r.get("typeOf").in(transactionTypes)); // Transaction Type
return cb.and(predicates.toArray(new Predicate[0]));
@@ -60,12 +69,14 @@ public class LoanSummaryBalancesRepository {
criteriaQueryFactory.applySpecificationToCriteria(root, spec, query);
query.groupBy(root.get("typeOf"), root.get("reversed"), root.get("manuallyAdjustedOrReversed"));
query.select(cb.construct(LoanTransactionBalance.class, root.get("typeOf"), root.get("reversed"),
root.get("manuallyAdjustedOrReversed"), cb.sum(root.get("amount"))));
query.groupBy(root.get("typeOf"), root.get("reversed"), root.get("manuallyAdjustedOrReversed"), root.get("loan").get("id"));
query.select(cb.construct(LoanTransactionBalanceWithLoanId.class, root.get("typeOf"), root.get("reversed"),
root.get("manuallyAdjustedOrReversed"), cb.sum(root.get("amount")), root.get("loan").get("id")));
TypedQuery<LoanTransactionBalance> queryToExecute = entityManager.createQuery(query);
return queryToExecute.getResultList();
TypedQuery<LoanTransactionBalanceWithLoanId> queryToExecute = entityManager.createQuery(query);
Map<Long, List<LoanTransactionBalanceWithLoanId>> collect = queryToExecute.getResultList().stream()
.collect(Collectors.groupingBy(LoanTransactionBalanceWithLoanId::getLoanId));
return collect;
}
}