added loop

This commit is contained in:
Chinenye Nmoh
2025-06-19 23:23:06 +01:00
parent 7fbb659fc6
commit 684833bd66
2 changed files with 36 additions and 32 deletions
+2 -1
View File
@@ -227,5 +227,6 @@ class Loan(db.Model):
"""
customer_loans = cls.query.filter_by( customer_id = customer_id).all()
if not customer_loans:
raise ValueError(f"Customer with Id {customer_id} does not have any loan.")
raise ValueError(f"Customer with Id {customer_id} does not have any loan.")
logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id}")
return customer_loans
+34 -31
View File
@@ -105,54 +105,59 @@ def payment_callback():
@autocall_bp.route("/penal-charge", methods=["POST"])
def penal_charge():
data = request.get_json()
logger.info(f"Calling Penal Charge Endpoints")
logger.info(f"Calling Penal Charge Endpoint")
response = SimbrellaClient.penal_charge(data[0])
try:
response = SimbrellaClient.penal_charge(data[0])
return response
except Exception as e:
logger.error(f"Error in Penal Charge: {e}")
return ResponseHelper.error("Penal charge failed")
return response
@autocall_bp.route("/analytic-salary-detect", methods=["POST"])
def salary_detect():
payload = request.get_json()
logger.info(f"Calling Salary Detect endpoint")
logger.info("Calling Salary Detect endpoint")
# Attempt to add the incoming salary data
# Step 1: Try to add new salary data
try:
new_salary = SalaryService.add_salary_data(payload)
if new_salary:
logger.info(f"Salary added: {new_salary.id}" )
logger.info(f"Salary added: {new_salary.id}")
except Exception as e:
logger.info(f"Failed to save salary: ", e)
logger.error(f"Failed to save salary: {e}")
# Fetch all pending salaries
# Step 2: Get all pending salaries
pending_salaries = SalaryService.get_pending_salaries()
if not pending_salaries:
logger.info(f"No pending salaries found")
logger.info("No pending salaries found")
return ResponseHelper.success([], "No pending salaries")
logger.info(f"Found pending salaries {len(pending_salaries)}" )
logger.info(f"Found {len(pending_salaries)} pending salaries to process")
# Step 3: Process each salary
for pending_salary in pending_salaries:
logger.info(f"Processing salary ID: {pending_salary.id}" )
logger.info(f"Processing salary ID: {pending_salary.id}")
# Step 1: Update status to PROCESSING early to avoid race condition
# Step 3.1: Update status to PROCESSING
try:
SalaryService.update_status(pending_salary.id, "PROCESSING")
except Exception as e:
logger.info(f"Failed to update status to PROCESSING for salary ID : {pending_salary.id} {e}" )
logger.warning(f"Could not update status for salary ID {pending_salary.id}: {e}")
continue
# Step 2: Fetch loans for this salary's customer
# Step 3.2: Get loans
try:
loans = LoanService.get_customer_loans(pending_salary.customer_id)
if not loans:
logger.warning(f"No loans found for customer ID: {pending_salary.customer_id}" )
logger.warning(f"No loans found for customer ID: {pending_salary.customer_id}")
continue
except Exception as e:
logger.info(f"Error fetching loans for customer ID : {pending_salary.customer_id} {e}")
logger.error(f"Error fetching loans for customer ID {pending_salary.customer_id}: {e}")
continue
# Step 3: Loop through loans and create repayment entries
# Step 3.3: Create repayments
for loan in loans:
try:
loan_dict = loan.to_dict()
@@ -164,23 +169,21 @@ def salary_detect():
"initiatedBy": "SALARY_DETECT",
"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)
logger.info(f"Created repayment ID: {repayment.id}", )
logger.info(f"Created repayment ID: {repayment.id}")
except Exception as e:
logger.info(f"Error creating repayment for loan ID : {loan.id} {e}" )
logger.error(f"Error creating repayment for loan ID {loan.id}: {e}")
continue
# Step 4: Simbrella integration call after all processing
try:
SimbrellaClient.collect_loan_user_salary_detect(repayment.to_dict())
except Exception as e:
logger.error(f"Failed to call Simbrella client: {e}")
# Step 4: Optionally update salary to DONE after all loan processing
try:
SalaryService.update_status(pending_salary.id, "DONE")
except Exception as e:
logger.info(f"Failed to mark salary ID as DONE: {pending_salary.id} {e}")
# Step 5: Call Simbrella integration after processing all salaries
try:
SimbrellaClient.collect_loan_user_salary_detect(payload)
except Exception as e:
logger.info(f"Failed to call Simbrella client: {e}")
logger.info(f"Finished processing salary ID: {pending_salary.id}")
return ResponseHelper.success([], "AutoCall Successful")