added failed loans endpoint

This commit was merged in pull request #70.
This commit is contained in:
Chinenye Nmoh
2026-04-05 16:53:02 +01:00
parent f1db12c7f2
commit c32c2502cc
7 changed files with 183 additions and 2 deletions
+37 -1
View File
@@ -9,6 +9,7 @@ from app.utils.logger import logger
from app.extensions import db
from decimal import Decimal, ROUND_HALF_UP
from datetime import datetime, timezone
from app.config import settings
class Loan(db.Model):
__tablename__ = "loans"
@@ -46,7 +47,6 @@ class Loan(db.Model):
verify_result = db.Column(db.String(10), nullable=True)
verify_description = db.Column(db.String(100), nullable=True)
reference = db.Column(db.String(50), nullable=True)
total_penal_charge = db.Column(db.Float, default=0.0)
last_penal_date = db.Column(db.DateTime, nullable=True)
@@ -102,6 +102,19 @@ class Loan(db.Model):
@classmethod
def get_loan_by_transaction_id(cls, transaction_id):
return cls.query.filter_by(transaction_id=transaction_id).first()
#update loan status
@classmethod
def update_status(cls, loan_id, status):
loan = cls.query.get(loan_id)
if loan:
loan.status = status
db.session.commit()
return loan.to_dict()
else:
raise ValueError(f"Loan with ID {loan_id} not found.")
@classmethod
def get_loan_by_loan_id(cls, loan_id):
@@ -250,6 +263,29 @@ class Loan(db.Model):
except Exception as e:
logger.error(f"Error fetching latest loan without disburse date: {e}")
raise
@classmethod
def get_failed_disbursements(cls):
"""
Get all loans with failed disbursement.
"""
try:
last_time_interval = settings.FAILED_RETRY_TIME_INTERVAL_SECONDS or 0
failed_loans = cls.query.filter(
cls.disburse_date.is_(None),
cls.created_at >= datetime.utcnow() - timedelta(seconds=last_time_interval)
).all()
if not failed_loans:
logger.info("No failed disbursements found.")
return []
logger.info(f"Found {len(failed_loans)} failed disbursements.")
return failed_loans
except Exception as e:
logger.error(f"Error fetching failed disbursements: {e}")
return []
@classmethod
def get_latest_loan_with_disburse_date(cls):