getcorefin / corefin

The open-source lending infra
https://corefin.com
MIT License
188 stars 19 forks source link

Installment dates are incorrect after on-time full payment is applied #29

Open albertjo opened 3 months ago

albertjo commented 3 months ago

Reproduction steps

  1. Create a loan with 4 terms
    curl --location 'http://localhost:8080/loans' \
    --header 'Content-Type: application/json' \
    --data '{
    "term": 4,
    "originatedAmount": 100.00,
    "currency": "USD",
    "targetInterestRate": 0.10,
    "effectiveInterestRate": 0.10,
    "externalReference": "orderId_12345",
    "startDate": "2024-01-01",
    "endDate": "2024-01-01",
    "timezone": "America/Los_Angeles",
    "region": "US",
    "state": "CA"
    }'

    It should create a loan with the following installments:

    "loanInstallments": [
            {
                "installmentId": "0",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 1,
                "principalAmount": 24.67,
                "interestAmount": 0.85,
                "startDate": "2024-01-01",
                "dueDate": "2024-02-01",
                "endDate": "2024-01-31",
                "status": "OWED"
            },
            {
                "installmentId": "1",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 2,
                "principalAmount": 24.92,
                "interestAmount": 0.60,
                "startDate": "2024-02-01",
                "dueDate": "2024-03-01",
                "endDate": "2024-02-29",
                "status": "OWED"
            },
            {
                "installmentId": "2",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 3,
                "principalAmount": 25.09,
                "interestAmount": 0.43,
                "startDate": "2024-03-01",
                "dueDate": "2024-04-01",
                "endDate": "2024-03-31",
                "status": "OWED"
            },
            {
                "installmentId": "3",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 4,
                "principalAmount": 25.32,
                "interestAmount": 0.21,
                "startDate": "2024-04-01",
                "dueDate": "2024-05-01",
                "endDate": "2024-04-30",
                "status": "OWED"
            }
        ]
  2. Make payment
    curl --location 'http://localhost:8080/payments/97bcdb7f-5e09-43cb-a939-94cf881b13d9/makePayment' \
    --header 'Content-Type: application/json' \
    --data '{
    "amount": "25.52",
    "paymentType": "PAYMENT",
    "paymentDateTime" : "2024-02-01T12:00:00Z"
    }'
    "loanInstallments": [
            {
                "installmentId": "1f68fb80-f0a6-11ee-81ba-0242ac140002",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 1,
                "principalAmount": 24.67,
                "interestAmount": 0.85,
                "startDate": "2024-01-01",
                "dueDate": "2024-02-01",
                "endDate": "2024-04-01",
                "status": "PAID"
            },
            {
                "installmentId": "1f6af8a2-f0a6-11ee-81ba-0242ac140002",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 2,
                "principalAmount": 25.12,
                "interestAmount": 0.40,
                "startDate": "2024-02-01",
                "dueDate": "2024-03-01",
                "endDate": "2024-04-01",
                "status": "OWED"
            },
            {
                "installmentId": "1f6c16c5-f0a6-11ee-81ba-0242ac140002",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 3,
                "principalAmount": 25.31,
                "interestAmount": 0.21,
                "startDate": "2024-03-01",
                "dueDate": "2024-04-01",
                "endDate": "2024-04-01",
                "status": "OWED"
            },
            {
                "installmentId": "1f6d0dda-f0a6-11ee-81ba-0242ac140002",
                "loanId": "97bcdb7f-5e09-43cb-a939-94cf881b13d9",
                "numTerm": 4,
                "principalAmount": 25.54,
                "interestAmount": -0.01,
                "startDate": "2024-04-01",
                "dueDate": "2024-05-01",
                "endDate": "2024-04-01",
                "status": "OWED"
            }
        ]

The startDate and dueDate for each installment are correct but the endDate is incorrect.

albertjo commented 3 months ago

@huberttsen It's because end_date is set to now() in LoanInstallmentDao.java - is this expected behavior?

https://github.com/getcorefin/corefin/blob/dev/dao/src/main/java/org/corefin/dao/LoanInstallmentDao.java#L96

jdbi.useHandle(
                handle -> {
                    handle.createUpdate(updateQuery)
                            .bind("loan_id", loanInstallmentDto.loanId())
                            .bind("loan_installment_id", loanInstallmentDto.installmentId())
                            .bind("principal_amount", loanInstallmentDto.principalAmount())
                            .bind("interest_amount", loanInstallmentDto.interestAmount())
                            .bind("status", loanInstallmentDto.status().toString())
                            .bind("end_date", ZonedDateTime.now().toLocalDate())
                            .execute();
                }
        );
piyushchhabra commented 3 months ago

@albertjo seems like this issue is fixed now. Changes were done in this PR: https://github.com/getcorefin/corefin/pull/33 This issue can be closed