fix loan
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from flask import jsonify
|
||||
from app.models.loan import Loan
|
||||
|
||||
# Configure logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Loan: # Mock Loan class for demonstration
|
||||
class LoanDelete: # Mock Loan class for demonstration
|
||||
def __init__(self, id, customer_id, account_id, offer_id, initial_loan_amount, current_loan_amount, status,
|
||||
product_id, default_penalty_fee, continuous_fee, due_date, created_at, updated_at):
|
||||
self.id = id
|
||||
|
||||
+25
-96
@@ -62,78 +62,7 @@ class Loan(db.Model):
|
||||
back_populates="loans",
|
||||
)
|
||||
|
||||
# @classmethod
|
||||
# def create_loan(cls, customer_id, account_id, offer_id, initial_loan_amount, status='pending',
|
||||
# product_id=None, current_loan_amount=None, default_penalty_fee=None,
|
||||
# continuous_fee=None, due_date=None):
|
||||
#
|
||||
# # 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(
|
||||
# customer_id=customer_id,
|
||||
# account_id=account_id,
|
||||
# offer_id=offer_id,
|
||||
# initial_loan_amount=initial_loan_amount,
|
||||
# status=status,
|
||||
# product_id=product_id,
|
||||
# current_loan_amount=current_loan_amount or initial_loan_amount,
|
||||
# default_penalty_fee=default_penalty_fee,
|
||||
# continuous_fee=continuous_fee,
|
||||
# due_date=due_date
|
||||
# )
|
||||
#
|
||||
# try:
|
||||
# db.session.add(loan)
|
||||
# except IntegrityError as err:
|
||||
# raise ValueError(f"Database integrity error: {err}")
|
||||
# return loan
|
||||
|
||||
@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
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def get_customer_loan(cls, loan_id, customer_id):
|
||||
"""
|
||||
Get customer's active loans.
|
||||
"""
|
||||
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
|
||||
|
||||
@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
|
||||
|
||||
@classmethod
|
||||
def get_all_loans(cls, customer_id=None, account_id=None, status=None, offer_id=None,
|
||||
@@ -197,31 +126,31 @@ class Loan(db.Model):
|
||||
query = query.limit(limit).offset(offset)
|
||||
|
||||
return query.all(), total_count
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Convert the Loan object to a dictionary format for JSON serialization.
|
||||
"""
|
||||
return {
|
||||
'id': self.id,
|
||||
'customer_id': self.customer_id,
|
||||
'account_id': self.account_id,
|
||||
'transaction_id': self.transaction_id,
|
||||
'original_transaction': self.original_transaction,
|
||||
'offer_id': self.offer_id,
|
||||
'initial_loan_amount': self.initial_loan_amount,
|
||||
'current_loan_amount': self.current_loan_amount,
|
||||
'status': self.status,
|
||||
'product_id': self.product_id,
|
||||
'default_penalty_fee': self.default_penalty_fee,
|
||||
'continuous_fee': self.continuous_fee,
|
||||
'upfront_fee': self.upfront_fee,
|
||||
'repayment_amount': self.repayment_amount,
|
||||
'installment_amount': self.installment_amount,
|
||||
'due_date': self.due_date.isoformat() if self.due_date else None,
|
||||
'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
'updated_at': self.updated_at.isoformat() if self.updated_at else None
|
||||
}
|
||||
#
|
||||
# def to_dict(self):
|
||||
# """
|
||||
# Convert the Loan object to a dictionary format for JSON serialization.
|
||||
# """
|
||||
# return {
|
||||
# 'id': self.id,
|
||||
# 'customer_id': self.customer_id,
|
||||
# 'account_id': self.account_id,
|
||||
# 'transaction_id': self.transaction_id,
|
||||
# 'original_transaction': self.original_transaction,
|
||||
# 'offer_id': self.offer_id,
|
||||
# 'initial_loan_amount': self.initial_loan_amount,
|
||||
# 'current_loan_amount': self.current_loan_amount,
|
||||
# 'status': self.status,
|
||||
# 'product_id': self.product_id,
|
||||
# 'default_penalty_fee': self.default_penalty_fee,
|
||||
# 'continuous_fee': self.continuous_fee,
|
||||
# 'upfront_fee': self.upfront_fee,
|
||||
# 'repayment_amount': self.repayment_amount,
|
||||
# 'installment_amount': self.installment_amount,
|
||||
# 'due_date': self.due_date.isoformat() if self.due_date else None,
|
||||
# 'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
# 'updated_at': self.updated_at.isoformat() if self.updated_at else None
|
||||
# }
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Loan {self.id}>'
|
||||
Reference in New Issue
Block a user