added a new penal charge endpoint

This commit is contained in:
Chinenye Nmoh
2026-01-23 11:14:17 +01:00
parent 03c12fd9b5
commit 0f4455e738
4 changed files with 35 additions and 6 deletions
+1
View File
@@ -61,6 +61,7 @@ class Config:
os.getenv("OVERDUE_LOAN_BATCH_DELAY_SECONDS", 5)
)
OVERDUE_GRACE_PERIOD_DAYS = int(os.getenv("OVERDUE_GRACE_PERIOD_DAYS", 30))
OVERDUE_PROCESSING_LIST_LIMIT = int(os.getenv("OVERDUE_PROCESSING_LIST_LIMIT", 100))
BANK_CALL_API_TIME_OUT = os.getenv("BANK_CALL_API_TIME_OUT", 100)
+22 -2
View File
@@ -115,7 +115,7 @@ class LoanRepaymentSchedule(db.Model):
logger.error(f"Error fetching active overdue repayment schedules: {e}")
return []
@classmethod
def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days):
def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days, limit=None):
"""
Get all overdue repayment schedules that are not repaid and beyond the grace period.
"""
@@ -124,7 +124,7 @@ class LoanRepaymentSchedule(db.Model):
return cls.query.filter(
cls.due_date < grace_period_date,
cls.paid == False
).order_by(cls.due_date.asc()).all()
).order_by(cls.due_date.asc()).limit(limit).all()
except Exception as e:
logger.error(f"Error fetching overdue repayment schedules with grace period: {e}")
return []
@@ -274,5 +274,25 @@ class LoanRepaymentSchedule(db.Model):
db.session.rollback()
logger.error(f"Error applying repayment for schedule {schedule_id}: {e}")
raise
@classmethod
def update_due_process_date_and_count(cls, schedule_id):
"""
Update the due process date to now and increment the due process count.
"""
try:
schedule = cls.query.get(schedule_id)
if schedule.due_process_count is None:
schedule.due_process_count = 0
schedule.due_process_count += 1
schedule.due_process_date = datetime.now(timezone.utc)
schedule.updated_at = datetime.now(timezone.utc)
db.session.commit()
logger.info(f"Updated due process date and count for schedule ID {schedule.id}")
except Exception as e:
db.session.rollback()
logger.error(f"Error updating due process date and count for schedule {schedule.id}: {e}")
raise
+3 -2
View File
@@ -445,10 +445,11 @@ def report():
def process_penal_charges():
try:
OVERDUE_GRACE_PERIOD_DAYS = settings.OVERDUE_GRACE_PERIOD_DAYS
OVERDUE_PROCESSING_LIST_LIMIT = settings.OVERDUE_PROCESSING_LIST_LIMIT
overdue_loans = (
LoanRepaymentScheduleService
.get_overdue_repayment_schedule_with_grace_period(OVERDUE_GRACE_PERIOD_DAYS)
.get_overdue_repayment_schedule_with_grace_period(OVERDUE_GRACE_PERIOD_DAYS,OVERDUE_PROCESSING_LIST_LIMIT)
)
logger.info(f"Found {len(overdue_loans)} overdue loans.")
@@ -464,7 +465,7 @@ def process_penal_charges():
for loan in overdue_loans:
# TODO: apply penal charge logic here
# PenalChargeService.apply_penal_charge(loan)
LoanRepaymentScheduleService.update_due_process_date_and_count(loan.id)
processed_loans.append(loan.to_dict())
return ResponseHelper.success(
+9 -2
View File
@@ -48,8 +48,15 @@ class LoanRepaymentScheduleService:
return LoanRepaymentSchedule.update_repayment_schedule_description(schedule_id, description)
@classmethod
def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days):
return LoanRepaymentSchedule.get_overdue_repayment_schedule_with_grace_period(grace_period_days)
def get_overdue_repayment_schedule_with_grace_period(cls, grace_period_days, limit=None):
return LoanRepaymentSchedule.get_overdue_repayment_schedule_with_grace_period(grace_period_days, limit=limit)
@classmethod
def update_due_process_date_and_count(cls, schedule_id):
"""
Update due process date and count for a repayment schedule.
"""
return LoanRepaymentSchedule.update_due_process_date_and_count(schedule_id)
@staticmethod
def handle_schedule_updates(updated_loan, data, amount_collected, message, loan_data):
"""