Repayment updates

This commit is contained in:
CHIEFSOFT\ameye
2025-06-20 15:18:04 -04:00
parent 190cd6611a
commit 7b21140b39
5 changed files with 64 additions and 3 deletions
+19 -1
View File
@@ -237,4 +237,22 @@ class Loan(db.Model):
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
+23
View File
@@ -1,6 +1,7 @@
from app.extensions import db
from datetime import datetime, timezone
from app.utils.logger import logger
from app.enums.loan_status import LoanStatus
class Repayment(db.Model):
__tablename__ = "repayments"
@@ -47,6 +48,28 @@ class Repayment(db.Model):
'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
def add_repayment(cls, data: dict):
+5 -1
View File
@@ -175,7 +175,11 @@ def salary_detect():
"salaryAmount": pending_salary.amount,
}
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}")
except Exception as e:
logger.error(f"Error creating repayment for loan ID {loan.id}: {e}")
+8
View File
@@ -78,3 +78,11 @@ class LoanService:
"""
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)
+9 -1
View File
@@ -62,9 +62,17 @@ class RepaymentService:
Get the latest repayment with a repay date and no verification date.
"""
return Repayment.get_latest_loan_with_repay_date()
@classmethod
def add_repayment(cls, data):
"""
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)