Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Endpoint to fetch delinquent payments for a loan #39

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions dao/src/main/java/org/corefin/dao/LoanInstallmentDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import org.corefin.dao.mappers.LoanInstallmentMapper;
import org.corefin.dto.LoanInstallmentDto;
import org.corefin.model.common.InstallmentStatus;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

public class LoanInstallmentDao implements BaseDao<LoanInstallmentDto> {
Expand Down Expand Up @@ -74,6 +73,16 @@ public List<LoanInstallmentDto> findByLoanId(String loanId) {
.list()
);
}

public List<LoanInstallmentDto> findPastDueByLoanIdAndStatus(String loanId, InstallmentStatus status) {
return jdbi.withHandle(
handle -> handle.createQuery("SELECT * FROM loan_installment WHERE loan_id= :loan_id AND status = :status AND due_date < CURDATE()")
.bind("loan_id", loanId)
.bind("status", status.toString())
.mapTo(LoanInstallmentDto.class)
.list()
);
}
public void updateInstallmentForPayment(LoanInstallmentDto loanInstallmentDto) {
String updateQuery = """
UPDATE loan_installment set
Expand Down
4 changes: 1 addition & 3 deletions dao/src/main/java/org/corefin/dao/PaymentDao.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.corefin.dao;

import org.corefin.calculator.model.Payment;
import org.corefin.dao.mappers.PaymentMapper;
import org.corefin.dto.LoanInstallmentDto;
import org.corefin.dto.PaymentDto;
import org.jdbi.v3.core.Jdbi;

Expand Down Expand Up @@ -33,7 +31,7 @@ INSERT INTO payment(
jdbi.useHandle(
handle -> {
handle.createUpdate(insertQuery)
.bind("loan_id", dto.loandId())
.bind("loan_id", dto.loanId())
.bind("amount", dto.amount())
.bind("payment_type", dto.paymentType().toString())
.bind("payment_datetime", dto.paymentDateTime())
Expand Down
2 changes: 1 addition & 1 deletion dao/src/main/java/org/corefin/dto/PaymentDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public record PaymentDto(
String paymentId,
String loandId,
String loanId,
BigDecimal amount,
PaymentType paymentType,
ZonedDateTime paymentDateTime
Expand Down
9 changes: 8 additions & 1 deletion server/src/main/java/com/corefin/server/v1/LoanResource.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.corefin.server.v1;

import com.corefin.server.v1.request.CreateLoanRequest;
import com.corefin.server.v1.request.MakePaymentRequest;
import com.corefin.server.v1.response.GetInstallmentsResponse;
import com.corefin.server.v1.response.GetLoanResponse;
import com.corefin.server.v1.response.GetLoansResponse;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -51,4 +51,11 @@ public GetLoanResponse getLoan(@PathParam("loanId") String loanId) {
LOGGER.info("getLoan called for loan with id %s".formatted(loanId));
return loanResourceManager.doGetLoan(loanId);
}

@GET
@Path("/{loanId}/installments/pastDue")
public GetInstallmentsResponse getPastDueInstallments(@PathParam("loanId") String loanId) {
LOGGER.info("getPastDueInstallments called for loan with id %s".formatted(loanId));
return loanResourceManager.doGetPastDueInstallments(loanId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.corefin.server.transform.LoanTransformer;
import com.corefin.server.v1.model.LoanInfo;
import com.corefin.server.v1.request.CreateLoanRequest;
import com.corefin.server.v1.response.GetInstallmentsResponse;
import com.corefin.server.v1.response.GetLoanResponse;
import com.corefin.server.v1.response.GetLoansResponse;
import org.corefin.calculator.Actuarial365Calculator;
Expand All @@ -12,6 +13,7 @@
import org.corefin.dao.LoanInstallmentDao;
import org.corefin.dto.LoanDto;
import org.corefin.dto.LoanInstallmentDto;
import org.corefin.model.common.InstallmentStatus;
import org.corefin.model.common.LoanStatus;
import org.springframework.stereotype.Service;

Expand All @@ -20,7 +22,6 @@
import java.util.UUID;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class LoanResourceManager {
Expand Down Expand Up @@ -64,6 +65,11 @@ public GetLoansResponse doGetLoans() {
);
}

public GetInstallmentsResponse doGetPastDueInstallments(String loanId) {
List<LoanInstallmentDto> dueInstallments = loanInstallmentDao.findPastDueByLoanIdAndStatus(loanId, InstallmentStatus.OWED);
return new GetInstallmentsResponse(dueInstallments);
}

public GetLoanResponse createLoan(CreateLoanRequest createLoanRequest) {
String loanId = UUID.randomUUID().toString();
LoanDto loanDto = new LoanDto(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.corefin.server.v1.response;

import org.corefin.dto.LoanInstallmentDto;

import java.util.List;

public record GetInstallmentsResponse(
List<LoanInstallmentDto> installments
) {}