From bc8f8e2cdd68b538e59f13eb3ab912f0fd6ccec3 Mon Sep 17 00:00:00 2001 From: VivianDee <115420678+VivianDee@users.noreply.github.com> Date: Fri, 28 Mar 2025 10:11:44 +0100 Subject: [PATCH] [update]: model class methods --- app/api/services/account_service.py | 15 --------------- app/api/services/customer_service.py | 7 ------- app/api/services/loan_service.py | 10 ---------- app/models/account.py | 9 +++++++++ app/models/customer.py | 9 ++++++++- app/models/loan.py | 13 +++++++++++++ 6 files changed, 30 insertions(+), 33 deletions(-) delete mode 100644 app/api/services/account_service.py delete mode 100644 app/api/services/customer_service.py delete mode 100644 app/api/services/loan_service.py diff --git a/app/api/services/account_service.py b/app/api/services/account_service.py deleted file mode 100644 index a452b17..0000000 --- a/app/api/services/account_service.py +++ /dev/null @@ -1,15 +0,0 @@ -from app.models import Account - -def check_account_settings(account_id, customer_id): - """ - Checks if the account belongs to the customer and if it has existing liens. - """ - account = Account.query.filter_by(id=account_id, customer_id=customer_id).first() - - if not account: - return False, "Account not found or doesn't belong to customer" - - if account.lien_amount > 0: - return False, "Account has an existing lien" - - return True, "Account is valid" \ No newline at end of file diff --git a/app/api/services/customer_service.py b/app/api/services/customer_service.py deleted file mode 100644 index 582aa1e..0000000 --- a/app/api/services/customer_service.py +++ /dev/null @@ -1,7 +0,0 @@ -from app.models import Customer - -def check_customer_eligibility(data): - # Verify customer exists - customer = Customer.query.filter_by(id=data['customerId']).first() - if not customer: - return False, "Customer not found" \ No newline at end of file diff --git a/app/api/services/loan_service.py b/app/api/services/loan_service.py deleted file mode 100644 index 3beb98c..0000000 --- a/app/api/services/loan_service.py +++ /dev/null @@ -1,10 +0,0 @@ - -from app.models import Loan - -def check_active_loans(data): - active_loans = Loan.query.filter_by( - customer_id=data['customerId'], - status='active' - ).count() - if active_loans > 0: - return False, "Customer has active loans" \ No newline at end of file diff --git a/app/models/account.py b/app/models/account.py index 1c65985..ebe299c 100644 --- a/app/models/account.py +++ b/app/models/account.py @@ -22,6 +22,15 @@ class Account(db.Model): # viewonly=True # ) + @classmethod + def is_valid_account(cls, account_id, customer_id): + account = cls.query.filter_by(id=account_id, customer_id=customer_id).first() + if not account: + return False, "Account not found or doesn't belong to customer" + if account.lien_amount > 0: + return False, "Account has an existing lien" + return True, "Account is valid" + def __repr__(self): return f'' \ No newline at end of file diff --git a/app/models/customer.py b/app/models/customer.py index 54a7807..6dc5f7e 100644 --- a/app/models/customer.py +++ b/app/models/customer.py @@ -11,6 +11,13 @@ class Customer(db.Model): created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + @classmethod + def is_eligible(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" def __repr__(self): - return f'' \ No newline at end of file + return f'' + diff --git a/app/models/loan.py b/app/models/loan.py index ce26cd8..1c27bec 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -15,5 +15,18 @@ class Loan(db.Model): created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + + @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" + + def __repr__(self): return f'' \ No newline at end of file