[add]: Update loan status after repayment

This commit was merged in pull request #6.
This commit is contained in:
VivianDee
2025-04-10 18:35:26 +01:00
parent 1081467f6f
commit e5320c075e
6 changed files with 45 additions and 9 deletions
+22 -3
View File
@@ -65,16 +65,35 @@ class Loan(db.Model):
return False
return True
@classmethod
def get_customer_loan(cls, loan_id, customer_id):
"""
Check if a loan with the given ID exists and if the loan belongs to the specified customer_id.
Get customer's active loans.
"""
loan = cls.query.filter_by(id=loan_id, customer_id=customer_id).first()
loan = cls.query.filter_by(id = loan_id, customer_id = customer_id).first()
if not loan:
raise ValueError(f"Loan with ID {loan_id} does not exist or does not belong to customer {customer_id}.")
raise ValueError(f"Loan with ID {loan_id} does not exist or does not belong to customer {customer_id}.")
return loan
@classmethod
def update_status(cls, loan_id, status):
"""
Update the status of the loan with the given loan_id.
"""
# Retrieve loan
loan = cls.query.get(loan_id)
if not loan:
raise ValueError(f"Loan with ID {loan_id} does not exist.")
if loan.status == status:
return
# Update loan status and the updated_at timestamp
loan.status = status
db.session.commit()
def __repr__(self):
return f'<Loan {self.id}>'
+6 -2
View File
@@ -1,4 +1,5 @@
from datetime import datetime, timezone
from app.api.enums.loan_status import LoanStatus
from app.extensions import db
from app.models.customer import Customer
from app.models.loan import Loan
@@ -29,8 +30,11 @@ class Repayment(db.Model):
# Check loan exists
loan = Loan.get_customer_loan(loan_id = loan_id, customer_id = customer_id)
if not loan:
raise ValueError("Loan not found for customer")
# Check that the loan is active
if loan.status != LoanStatus.ACTIVE:
raise ValueError(f"Repayment cannot be processed. Loan status: ({loan.status})")
repayment = cls(
customer_id=customer_id,