Files
digifi-BankToProductCore/app/models/loan.py
T
2025-04-10 17:02:54 +01:00

41 lines
1.4 KiB
Python

from datetime import datetime, timezone
from app.extensions import db
class Loan(db.Model):
__tablename__ = 'loans'
id = db.Column(db.String(50), primary_key=True)
customer_id = db.Column(db.String(50), nullable=False)
account_id = db.Column(db.String(50), nullable=False)
product_id = db.Column(db.String(20), nullable=False)
principal_amount = db.Column(db.Float, nullable=False)
status = db.Column(db.String(20), default='pending')
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 has_active_loans(cls, customer_id):
active_loans = cls.query.filter_by(
customer_id=customer_id,
status='active'
).count()
if active_loans > 0:
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}>'