added salary table
This commit is contained in:
+58
-37
@@ -113,53 +113,74 @@ def penal_charge():
|
||||
|
||||
@autocall_bp.route("/analytic-salary-detect", methods=["POST"])
|
||||
def salary_detect():
|
||||
#***************************************************
|
||||
# PART 1 Accept any new import of salary detection
|
||||
#**************************************************
|
||||
data = request.get_json()
|
||||
logger.info(f"Calling Salary Detect Endpoints")
|
||||
payload = request.get_json()
|
||||
logger.info(f"Calling Salary Detect endpoint")
|
||||
|
||||
# Attempt to add the incoming salary data
|
||||
try:
|
||||
salary = SalaryService.add_salary_data(data)
|
||||
if salary:
|
||||
logger.info(f"Successful Salary Added")
|
||||
# return ResponseHelper.success(salary.to_dict(), "Successful")
|
||||
new_salary = SalaryService.add_salary_data(payload)
|
||||
if new_salary:
|
||||
logger.info(f"Salary added: {new_salary.id}" )
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to save salary: {e}")
|
||||
|
||||
# ***************************************************
|
||||
# PART 2 SELECT * FROM salaries WHERE status IS 'START' ORDER BY id ASC
|
||||
# **************************************************
|
||||
logger.info(f"Failed to save salary: ", e)
|
||||
|
||||
# Fetch all pending salaries
|
||||
pending_salaries = SalaryService.get_pending_salaries()
|
||||
|
||||
if not pending_salaries:
|
||||
logger.info(f"No pending salaries found")
|
||||
# return ResponseHelper.success([], "No pending salaries found")
|
||||
return ResponseHelper.success([], "No pending salaries")
|
||||
|
||||
logger.info(f"Found {len(pending_salaries)} pending salaries")
|
||||
logger.info(f"Found pending salaries {len(pending_salaries)}" )
|
||||
|
||||
#in the loop
|
||||
# USE the customerID to find thu user Loan
|
||||
# if loan is/are found for the user
|
||||
# INSERT INTO repayments TABLE
|
||||
# repayment = cls(
|
||||
# customer_id=customer_id,
|
||||
# loan_id=loan.id,
|
||||
# product_id=loan.product_id,
|
||||
# transaction_id = transaction_id,
|
||||
# created_at=datetime.now(timezone.utc),
|
||||
# updated_at=datetime.now(timezone.utc),
|
||||
# initiated_by='SALARY_DETECT'
|
||||
# )
|
||||
for pending_salary in pending_salaries:
|
||||
logger.info(f"Processing salary ID: {pending_salary.id}" )
|
||||
|
||||
# by the time you call this you must be on repayment table
|
||||
# Step 1: Update status to PROCESSING early to avoid race condition
|
||||
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}" )
|
||||
continue
|
||||
|
||||
# Step 2: Fetch loans for this salary's customer
|
||||
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}" )
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.info(f"Error fetching loans for customer ID : {pending_salary.customer_id} {e}")
|
||||
continue
|
||||
|
||||
# Step 3: Loop through loans and create repayment entries
|
||||
for loan in loans:
|
||||
try:
|
||||
loan_dict = loan.to_dict()
|
||||
repayment_data = {
|
||||
"customerId": pending_salary.customer_id,
|
||||
"loanId": loan_dict["debtId"],
|
||||
"productId": loan_dict["productId"],
|
||||
"transactionId": loan_dict["transactionId"],
|
||||
"initiatedBy": "SALARY_DETECT",
|
||||
"salaryAmount": pending_salary.amount,
|
||||
}
|
||||
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}", )
|
||||
except Exception as e:
|
||||
logger.info(f"Error creating repayment for loan ID : {loan.id} {e}" )
|
||||
continue
|
||||
|
||||
# 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(data)
|
||||
SimbrellaClient.collect_loan_user_salary_detect(payload)
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call collect_loan_user_salary_detect: {e}")
|
||||
logger.info(f"Failed to call Simbrella client: {e}")
|
||||
|
||||
|
||||
return_data=[]
|
||||
|
||||
return ResponseHelper.success(return_data, "AutoCall Successful")
|
||||
return ResponseHelper.success([], "AutoCall Successful")
|
||||
|
||||
Reference in New Issue
Block a user