You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by GitBox <gi...@apache.org> on 2022/03/31 07:27:37 UTC

[GitHub] [fineract] galovics commented on a change in pull request #2224: Enhancement for new fields in APIs

galovics commented on a change in pull request #2224:
URL: https://github.com/apache/fineract/pull/2224#discussion_r839263063



##########
File path: fineract-provider/src/main/java/org/apache/fineract/infrastructure/dataqueries/service/ReadWriteNonCoreDataServiceImpl.java
##########
@@ -340,6 +340,17 @@ private boolean isSurveyCategory(final Integer category) {
         return category.equals(DataTableApiConstant.CATEGORY_PPI);
     }
 
+    private JsonElement addColumn(final String name, final String dataType, final boolean isMandatory, final Integer length) {

Review comment:
       Do you think we could call this method "newColumn" or something instead since it's not really adding a new column anywhere but returns just part of a JsonObject?
   
   I feel it's a little weird when I read the code below
   `columns.add(addColumn("created_at", "DateTime", false, null));`

##########
File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java
##########
@@ -2358,4 +2359,56 @@ public Integer retrieveNumberOfActiveLoans() {
         final String sql = "select count(*) from m_loan";
         return this.jdbcTemplate.queryForObject(sql, Integer.class);
     }
+
+    @Override
+    public CollectionData retrieveLoanCollectionData(Long loanId) {
+        final CollectionDataMapper mapper = new CollectionDataMapper(sqlGenerator);
+        String sql = "select " + mapper.schema();
+        CollectionData collectionData = this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { loanId });
+        return collectionData;
+    }
+
+    private static final class CollectionDataMapper implements RowMapper<CollectionData> {
+
+        private final DatabaseSpecificSQLGenerator sqlGenerator;
+
+        CollectionDataMapper(DatabaseSpecificSQLGenerator sqlGenerator) {
+            this.sqlGenerator = sqlGenerator;
+        }
+
+        public String schema() {
+            StringBuilder sqlBuilder = new StringBuilder();
+
+            sqlBuilder.append(
+                    "l.id as loanId, coalesce((l.approved_principal - l.principal_disbursed_derived), 0) as availableDisbursementAmount, ");
+            sqlBuilder.append("datediff(" + sqlGenerator.currentDate() + ", laa.overdue_since_date_derived) as pastDueDays, ");

Review comment:
       datediff is going to fail here on PostgreSQL. Please check the DatabaseSpecificSQLGenerator class, there's a corresponding method there which generates the proper statements for both MySQL and PG.

##########
File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java
##########
@@ -2358,4 +2359,56 @@ public Integer retrieveNumberOfActiveLoans() {
         final String sql = "select count(*) from m_loan";
         return this.jdbcTemplate.queryForObject(sql, Integer.class);
     }
+
+    @Override
+    public CollectionData retrieveLoanCollectionData(Long loanId) {
+        final CollectionDataMapper mapper = new CollectionDataMapper(sqlGenerator);
+        String sql = "select " + mapper.schema();
+        CollectionData collectionData = this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { loanId });
+        return collectionData;
+    }
+
+    private static final class CollectionDataMapper implements RowMapper<CollectionData> {
+
+        private final DatabaseSpecificSQLGenerator sqlGenerator;
+
+        CollectionDataMapper(DatabaseSpecificSQLGenerator sqlGenerator) {
+            this.sqlGenerator = sqlGenerator;
+        }
+
+        public String schema() {
+            StringBuilder sqlBuilder = new StringBuilder();
+
+            sqlBuilder.append(
+                    "l.id as loanId, coalesce((l.approved_principal - l.principal_disbursed_derived), 0) as availableDisbursementAmount, ");
+            sqlBuilder.append("datediff(" + sqlGenerator.currentDate() + ", laa.overdue_since_date_derived) as pastDueDays, ");
+            sqlBuilder.append(
+                    "(select coalesce(min(lrs.duedate), null) as duedate from m_loan_repayment_schedule lrs where lrs.loan_id=1 and lrs.completed_derived is false and lrs.duedate >= "

Review comment:
       you sure about the `lrs.loan_id=1` condition? I'm not sure it's intentional to fix a single loan_id in the query.

##########
File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java
##########
@@ -2358,4 +2359,56 @@ public Integer retrieveNumberOfActiveLoans() {
         final String sql = "select count(*) from m_loan";
         return this.jdbcTemplate.queryForObject(sql, Integer.class);
     }
+
+    @Override
+    public CollectionData retrieveLoanCollectionData(Long loanId) {
+        final CollectionDataMapper mapper = new CollectionDataMapper(sqlGenerator);
+        String sql = "select " + mapper.schema();
+        CollectionData collectionData = this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { loanId });
+        return collectionData;
+    }
+
+    private static final class CollectionDataMapper implements RowMapper<CollectionData> {
+
+        private final DatabaseSpecificSQLGenerator sqlGenerator;
+
+        CollectionDataMapper(DatabaseSpecificSQLGenerator sqlGenerator) {
+            this.sqlGenerator = sqlGenerator;
+        }
+
+        public String schema() {
+            StringBuilder sqlBuilder = new StringBuilder();
+
+            sqlBuilder.append(
+                    "l.id as loanId, coalesce((l.approved_principal - l.principal_disbursed_derived), 0) as availableDisbursementAmount, ");
+            sqlBuilder.append("datediff(" + sqlGenerator.currentDate() + ", laa.overdue_since_date_derived) as pastDueDays, ");
+            sqlBuilder.append(
+                    "(select coalesce(min(lrs.duedate), null) as duedate from m_loan_repayment_schedule lrs where lrs.loan_id=1 and lrs.completed_derived is false and lrs.duedate >= "
+                            + sqlGenerator.currentDate() + ") as nextPaymentDueDate, ");
+            sqlBuilder.append("datediff(" + sqlGenerator.currentDate() + ", laa.overdue_since_date_derived) as delinquentDays, ");

Review comment:
       Same datediff problem here.

##########
File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoansApiResource.java
##########
@@ -639,6 +640,12 @@ public String retrieveLoan(@PathParam("loanId") @Parameter(description = "loanId
                 linkedAccount = this.accountAssociationsReadPlatformService.retriveLoanLinkedAssociation(loanId);
             }
 
+            if (associationParameters.contains("collection")) {

Review comment:
       I know the existing codebase doesn't really use constants but rather hardcoded values; can we make sure to extract the "collection" magic string to somewhere and just reference it? 
   Would be a shame if in the future some functionality breaks because we change it here but not the other place(s).

##########
File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java
##########
@@ -2358,4 +2359,56 @@ public Integer retrieveNumberOfActiveLoans() {
         final String sql = "select count(*) from m_loan";
         return this.jdbcTemplate.queryForObject(sql, Integer.class);
     }
+
+    @Override
+    public CollectionData retrieveLoanCollectionData(Long loanId) {
+        final CollectionDataMapper mapper = new CollectionDataMapper(sqlGenerator);
+        String sql = "select " + mapper.schema();
+        CollectionData collectionData = this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { loanId });
+        return collectionData;
+    }
+
+    private static final class CollectionDataMapper implements RowMapper<CollectionData> {
+
+        private final DatabaseSpecificSQLGenerator sqlGenerator;
+
+        CollectionDataMapper(DatabaseSpecificSQLGenerator sqlGenerator) {
+            this.sqlGenerator = sqlGenerator;
+        }
+
+        public String schema() {
+            StringBuilder sqlBuilder = new StringBuilder();
+
+            sqlBuilder.append(
+                    "l.id as loanId, coalesce((l.approved_principal - l.principal_disbursed_derived), 0) as availableDisbursementAmount, ");
+            sqlBuilder.append("datediff(" + sqlGenerator.currentDate() + ", laa.overdue_since_date_derived) as pastDueDays, ");
+            sqlBuilder.append(
+                    "(select coalesce(min(lrs.duedate), null) as duedate from m_loan_repayment_schedule lrs where lrs.loan_id=1 and lrs.completed_derived is false and lrs.duedate >= "
+                            + sqlGenerator.currentDate() + ") as nextPaymentDueDate, ");
+            sqlBuilder.append("datediff(" + sqlGenerator.currentDate() + ", laa.overdue_since_date_derived) as delinquentDays, ");
+            sqlBuilder.append(
+                    sqlGenerator.currentDate() + " as delinquentDate, coalesce(laa.total_overdue_derived, 0) as delinquentAmount, ");
+            sqlBuilder.append("lre.transactionDate as lastPaymentDate, coalesce(lre.amount, 0) as lastPaymentAmount ");
+            sqlBuilder.append("from m_loan l inner join m_loan_arrears_aging laa on laa.loan_id = l.id ");
+            sqlBuilder.append(
+                    "left join (select lt.loan_id, lt.transaction_date as transactionDate, lt.amount as amount from m_loan_transaction lt where lt.is_reversed = 0 and lt.transaction_type_enum=2 order by lt.transaction_date desc limit 1) lre on lre.loan_id = l.id ");

Review comment:
       `lt.is_reversed = 0` should be `lt.is_reversed = false` if the mapping is a boolean column. PG won't accept the 0 right-hand side comparison value without explicit casting.




-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org