[add]: loan_repayment event

This commit is contained in:
VivianDee
2025-04-10 17:02:54 +01:00
parent edd19b9a39
commit 7cea5390c0
7 changed files with 101 additions and 33 deletions
+10
View File
@@ -26,6 +26,16 @@ class Loan(db.Model):
return False, "Customer has active loans"
return True, "No active loans"
@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.
"""
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}.")
return loan
def __repr__(self):
return f'<Loan {self.id}>'
+45
View File
@@ -0,0 +1,45 @@
from datetime import datetime, timezone
from app.extensions import db
from app.models.customer import Customer
from app.models.loan import Loan
class Repayment(db.Model):
__tablename__ = 'repayments'
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
)
loan_id = db.Column(db.String(50), nullable=False)
customer_id = db.Column(db.String(50), nullable=False)
product_id = db.Column(db.String(20), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
@classmethod
def create_repayment(cls, customer_id, loan_id, product_id):
# Check customer exists
if not Customer.is_valid_customer(customer_id):
raise ValueError("Invalid customer")
# 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")
repayment = cls(
customer_id=customer_id,
loan_id=loan.id,
product_id=product_id,
)
db.session.add(repayment)
db.session.commit()
return repayment
def __repr__(self):
return f'<Repayment {self.id}>'