debugging
This commit was merged in pull request #34.
This commit is contained in:
+50
-26
@@ -124,22 +124,27 @@ def salary_detect():
|
||||
|
||||
if payload is None:
|
||||
logger.warning("No payload received in request")
|
||||
#- Sometimes no paylod return ResponseHelper.error("Missing request payload", status_code=400)
|
||||
return ResponseHelper.error("Missing request payload", status_code=400)
|
||||
|
||||
if payload:
|
||||
# Step 1: Try to add new salary data
|
||||
try:
|
||||
new_salary = SalaryService.add_salary_data(payload) # TODO - This will come as array of salaries - not just one
|
||||
if new_salary:
|
||||
logger.info(f"Salary added: {new_salary.id}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save salary: {e}")
|
||||
# Step 1: Try to add new salary data
|
||||
try:
|
||||
new_salary = SalaryService.add_salary_data(payload) # TODO - This will come as array of salaries - not just one
|
||||
if new_salary:
|
||||
logger.info(f"Salary added: {new_salary.id}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save salary: {e}")
|
||||
|
||||
process_salary_list() # TODO - This should be threaded out or removed from here finally
|
||||
# Step 2: Try processing salary list
|
||||
try:
|
||||
process_salary_list()
|
||||
except Exception as e:
|
||||
logger.exception("Unhandled error occurred while processing salary list")
|
||||
return ResponseHelper.error("Failed to process salary list", status_code=500, error=str(e))
|
||||
|
||||
logger.info(f"Finished processing List")
|
||||
logger.info("Finished processing List")
|
||||
return ResponseHelper.success([], "AutoCall Add Salary Successful")
|
||||
|
||||
|
||||
@autocall_bp.route("/analytic-salary-process", methods=["POST"])
|
||||
def salary_process():
|
||||
response = process_salary_list()
|
||||
@@ -147,7 +152,7 @@ def salary_process():
|
||||
|
||||
|
||||
def process_salary_list():
|
||||
# Step 2: Get all pending salaries
|
||||
# Step 1: Get all pending salaries
|
||||
pending_salaries = SalaryService.get_pending_salaries()
|
||||
if not pending_salaries:
|
||||
logger.info("No pending salaries found")
|
||||
@@ -155,18 +160,17 @@ def process_salary_list():
|
||||
|
||||
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}")
|
||||
|
||||
# Step 3.1: Update status to PROCESSING
|
||||
# Step 2: Update salary status to PROCESSING
|
||||
try:
|
||||
SalaryService.update_status(pending_salary.id, "PROCESSING")
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not update status for salary ID {pending_salary.id}: {e}")
|
||||
continue
|
||||
|
||||
# Step 3.2: Get loans
|
||||
# Step 3: Get customer's active loans
|
||||
try:
|
||||
loans, total_amount = LoanService.get_customer_active_loans(pending_salary.customer_id)
|
||||
if not loans:
|
||||
@@ -176,12 +180,10 @@ def process_salary_list():
|
||||
logger.error(f"Error fetching loans for customer ID {pending_salary.customer_id}: {e}")
|
||||
continue
|
||||
|
||||
# Step 3.3: Create repayments
|
||||
# Step 4: Create repayments for each loan
|
||||
for loan in loans:
|
||||
logger.info(f"Loan LOOP LoanID = :{loan.id}")
|
||||
logger.info(f"Processing Loan ID: {loan.id}")
|
||||
try:
|
||||
# loan_dict = loan.to_dict()
|
||||
logger.info(f"loan_dict ==== Repayment Data:{loan}")
|
||||
repayment_data = {
|
||||
"customerId": loan.customer_id,
|
||||
"loanId": loan.id,
|
||||
@@ -192,21 +194,43 @@ def process_salary_list():
|
||||
"LoanStatus": loan.status,
|
||||
}
|
||||
|
||||
logger.info(f"Saving/Creating Repayment Data:{repayment_data}")
|
||||
logger.info(f"Creating repayment with data: {repayment_data}")
|
||||
repayment = RepaymentService.create_repayment(repayment_data)
|
||||
LoanService.update_status(loan_id=repayment_data["loanId"],
|
||||
status=LoanStatus.START_REPAY) # repay started
|
||||
|
||||
if not repayment:
|
||||
logger.error(f"Repayment creation failed for loan ID {loan.id}")
|
||||
continue
|
||||
|
||||
# Update loan status to START_REPAY
|
||||
try:
|
||||
LoanService.update_status(loan_id=loan.id, status=LoanStatus.START_REPAY)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to update loan status for loan ID {loan.id}: {e}")
|
||||
|
||||
logger.info(f"Created repayment ID: {repayment.id}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating repayment for loan ID {loan.id}: {e}")
|
||||
continue
|
||||
# Step 4: Simbrella integration call after all processing
|
||||
|
||||
# Step 5: Call Simbrella to collect loan
|
||||
|
||||
try:
|
||||
SimbrellaClient.collect_loan_user_salary_detect(repayment.to_dict())
|
||||
simbrella_response = SimbrellaClient.collect_loan_user_salary_detect(repayment.to_dict())
|
||||
|
||||
if isinstance(simbrella_response, tuple):
|
||||
simbrella_response, status_code = simbrella_response
|
||||
logger.warning(f"Simbrella returned tuple: status={status_code}, response={simbrella_response}")
|
||||
|
||||
if isinstance(simbrella_response, dict):
|
||||
status = simbrella_response.get("status")
|
||||
if status != "success":
|
||||
logger.warning(f"Simbrella call failed for repayment ID {repayment.id}: {simbrella_response}")
|
||||
else:
|
||||
logger.warning(f"Unexpected Simbrella response type: {type(simbrella_response)}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to call Simbrella client: {e}")
|
||||
logger.error(f"Failed to call Simbrella for repayment ID {repayment.id}: {e}")
|
||||
|
||||
|
||||
logger.info(f"Finished processing salary ID: {pending_salary.id}")
|
||||
return []
|
||||
|
||||
return ResponseHelper.success([], "Processed all pending salaries")
|
||||
|
||||
Reference in New Issue
Block a user