Aman-Mittal commented on code in PR #5410:
URL: https://github.com/apache/fineract/pull/5410#discussion_r2749628432


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/client/service/ClientReadPlatformServiceImpl.java:
##########
@@ -601,6 +602,31 @@ public Long retrieveClientIdByExternalId(final ExternalId 
externalId) {
         return clientRepositoryWrapper.findIdByExternalId(externalId);
     }
 
+    @Override
+    public ClientPerformanceData retrieveClientPerformance(final Long 
clientId) {
+        try {
+            final String checkClientSql = "SELECT count(*) FROM m_client WHERE 
id = ?";
+            final Integer count = 
this.jdbcTemplate.queryForObject(checkClientSql, Integer.class, clientId);
+            if (count == null || count == 0) {
+                throw new ClientNotFoundException(clientId);
+            }
+            final String sql = """
+                    SELECT
+                        (SELECT COUNT(*) FROM m_loan WHERE client_id = c.id 
AND loan_status_id = 300) as activeLoans,
+                        (SELECT COALESCE(SUM(principal_outstanding_derived + 
interest_outstanding_derived + fee_charges_outstanding_derived + 
penalty_charges_outstanding_derived), 0)
+                         FROM m_loan WHERE client_id = c.id AND loan_status_id 
= 300) as totalOutstandingBalance
+                    FROM m_client c WHERE c.id = ?
+                    """;
+            return this.jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
+                final Integer activeLoans = rs.getInt("activeLoans");
+                BigDecimal totalOutstandingBalance = 
rs.getBigDecimal("totalOutstandingBalance");
+                return ClientPerformanceData.instance(activeLoans, 
totalOutstandingBalance);
+            }, clientId);
+        } catch (EmptyResultDataAccessException e) {
+            throw new ClientNotFoundException(clientId,e);
+        }
+    }

Review Comment:
   can you add appopriate index for this too? with compound index.
   
   this will make this query faster.
   
   you can check existing implementations in codebase



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to