Repayment updates
This commit is contained in:
@@ -238,3 +238,21 @@ class Loan(db.Model):
|
|||||||
logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}")
|
logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}")
|
||||||
|
|
||||||
return customer_loans, total_amount
|
return customer_loans, total_amount
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from app.utils.logger import logger
|
from app.utils.logger import logger
|
||||||
|
from app.enums.loan_status import LoanStatus
|
||||||
|
|
||||||
class Repayment(db.Model):
|
class Repayment(db.Model):
|
||||||
__tablename__ = "repayments"
|
__tablename__ = "repayments"
|
||||||
@@ -47,6 +48,28 @@ class Repayment(db.Model):
|
|||||||
'VerifyDate': self.verify_date.isoformat() if self.verify_date else None,
|
'VerifyDate': self.verify_date.isoformat() if self.verify_date else None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_repayment(cls, repayment_data):
|
||||||
|
|
||||||
|
if loan.status not in [LoanStatus.ACTIVE, LoanStatus.START_REPAY]:
|
||||||
|
raise ValueError(f"Repayment cannot be processed. Loan status: ({loan.status})")
|
||||||
|
|
||||||
|
repayment = cls(
|
||||||
|
customer_id=repayment_data.customerId,
|
||||||
|
loan_id=repayment_data.loanId,
|
||||||
|
product_id=repayment_data.productId,
|
||||||
|
transaction_id=repayment_data.transactionId,
|
||||||
|
created_at=datetime.now(timezone.utc),
|
||||||
|
updated_at=datetime.now(timezone.utc),
|
||||||
|
initiated_by= repayment_data.initiated_by
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.session.add(repayment)
|
||||||
|
except IntegrityError as err:
|
||||||
|
logger.error(f"Database integrity error: {err}")
|
||||||
|
return repayment
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def add_repayment(cls, data: dict):
|
def add_repayment(cls, data: dict):
|
||||||
|
|||||||
@@ -175,7 +175,11 @@ def salary_detect():
|
|||||||
"salaryAmount": pending_salary.amount,
|
"salaryAmount": pending_salary.amount,
|
||||||
}
|
}
|
||||||
logger.info(f"Creating repayment for loan ID {loan_dict['debtId']}")
|
logger.info(f"Creating repayment for loan ID {loan_dict['debtId']}")
|
||||||
repayment = RepaymentService.add_repayment(repayment_data)
|
# repayment = RepaymentService.add_repayment(repayment_data)
|
||||||
|
repayment = RepaymentService.create_repayment(repayment_data)
|
||||||
|
Loan.update_status(loan_id=repayment_data.loanId,
|
||||||
|
status=LoanStatus.START_REPAY) # repay started
|
||||||
|
|
||||||
logger.info(f"Created repayment ID: {repayment.id}")
|
logger.info(f"Created repayment ID: {repayment.id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error creating repayment for loan ID {loan.id}: {e}")
|
logger.error(f"Error creating repayment for loan ID {loan.id}: {e}")
|
||||||
|
|||||||
@@ -78,3 +78,11 @@ class LoanService:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
return Loan.get_customer_loans(customer_id=customer_id)
|
return Loan.get_customer_loans(customer_id=customer_id)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def update_status(cls, loan_id, status):
|
||||||
|
"""
|
||||||
|
Update the status of the loan with the given loan_id.
|
||||||
|
"""
|
||||||
|
# Retrieve loan
|
||||||
|
return Loan.update_status(loan_id, status)
|
||||||
|
|||||||
@@ -62,9 +62,17 @@ class RepaymentService:
|
|||||||
Get the latest repayment with a repay date and no verification date.
|
Get the latest repayment with a repay date and no verification date.
|
||||||
"""
|
"""
|
||||||
return Repayment.get_latest_loan_with_repay_date()
|
return Repayment.get_latest_loan_with_repay_date()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def add_repayment(cls, data):
|
def add_repayment(cls, data):
|
||||||
"""
|
"""
|
||||||
Add a new repayment entry.
|
Add a new repayment entry.
|
||||||
"""
|
"""
|
||||||
return Repayment.add_repayment(data)
|
return Repayment.add_repayment(data)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_repayment(cls, repayment_data):
|
||||||
|
"""
|
||||||
|
Add a new repayment entry.
|
||||||
|
"""
|
||||||
|
return Repayment.create_repayment(customer_id, repayment_data)
|
||||||
Reference in New Issue
Block a user