[add]: Loan details and repayment fix

This commit is contained in:
VivianDee
2025-04-10 12:20:57 +01:00
parent edd19b9a39
commit 1b973322c9
6 changed files with 80 additions and 33 deletions
+3 -3
View File
@@ -20,11 +20,11 @@ class Customer(db.Model):
)
@classmethod
def is_eligible(cls, customer_id):
def is_valid_customer(cls, customer_id):
customer = cls.query.filter_by(id=customer_id).first()
if not customer:
return False, "Customer not found"
return True, "Customer is eligible"
return False
return True
@classmethod
def create_customer(cls, id, msisdn, country_code, account_id, account_type='savings'):
+32 -2
View File
@@ -1,5 +1,7 @@
from datetime import datetime, timezone
from app.extensions import db
from app.models.customer import Customer
from app.models.account import Account
class Loan(db.Model):
@@ -15,6 +17,34 @@ class Loan(db.Model):
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
@classmethod
def create_loan(cls, id, customer_id, account_id, product_id, principal_amount, status='pending'):
# Check if customer exists
is_valid = Customer.is_valid_customer(customer_id)
if not is_valid:
raise ValueError("Customer does not exist")
# # Check for active loans
# has_active_loans = cls.has_active_loans(customer_id)
# if has_active_loans:
# raise ValueError("Customer has active loans")
# Create and save the loan
loan = cls(
id=id,
customer_id=customer_id,
account_id=account_id,
product_id=product_id,
principal_amount=principal_amount,
status=status
)
db.session.add(loan)
db.session.commit()
return loan
@classmethod
def has_active_loans(cls, customer_id):
active_loans = cls.query.filter_by(
@@ -23,8 +53,8 @@ class Loan(db.Model):
).count()
if active_loans > 0:
return False, "Customer has active loans"
return True, "No active loans"
return False
return True
def __repr__(self):