From c88e85eda1e7f2a5c036857ee9f5d6ba8cd02fe1 Mon Sep 17 00:00:00 2001 From: "CHIEFSOFT\\ameye" Date: Sun, 2 Nov 2025 15:34:55 -0500 Subject: [PATCH] retry loans --- app/integrations/simbrella.py | 72 ++++++++++++++++++++++++++++++----- app/models/loan.py | 25 ++++++++++++ app/routes/autocall.py | 28 +++++++++++++- app/services/loan.py | 7 ++++ 4 files changed, 122 insertions(+), 10 deletions(-) diff --git a/app/integrations/simbrella.py b/app/integrations/simbrella.py index 9a6a162..8df63a1 100644 --- a/app/integrations/simbrella.py +++ b/app/integrations/simbrella.py @@ -1,6 +1,7 @@ import requests from app.config import settings from app.helpers.response_helper import ResponseHelper +#from app.routes.autocall import verify_transaction from app.services.loan import LoanService from app.services.loan_repayment_schedule import LoanRepaymentScheduleService from app.utils.auth import get_headers @@ -63,8 +64,15 @@ class SimbrellaClient: logger.info(f"Here is your loan data: {loan_data}") if loan_data['disburseDate'] is not None: + logger.info("*************************SEEM LIKE A RETRY CALL -WE WILL VERIFY Result to Continue ") logger.info( - f"Please call verify loan : {data['transactionId']} loan send for processing at {loan_data['disburseDate']}") + f"Please call verify loan : {data['transactionId']} loan sent for processing at {loan_data['disburseDate']}") + # return 0 -- we need the disburseResult = '00' to be sure all is good + + if loan_data['disburseDate'] is not None and loan_data['disburseResult'] == '00': + logger.info("*************************Duplicate call to completed loan ") + logger.info( + f"Duplicate call detected for loan : {data['transactionId']} loan sent for processing at {loan_data['disburseDate']}") return 0 # let us set disbursement date @@ -83,7 +91,7 @@ class SimbrellaClient: disbursement_data = { "transactionId": loan_data.get('transactionId'), - "FbnTransactionId": loan_data.get('transactionId'), + "fbnTransactionId": loan_data.get('transactionId'), "debtId": debtId, "customerId": loan_data.get('customerId'), "accountId": loan_data.get('accountId'), @@ -96,24 +104,60 @@ class SimbrellaClient: "countryId": "01", "comment": "Loan Disbursement", } - +# ''' +# { Veryfing with the bank +# "transactionId": "string", +# "fbnTransactionId": "string", +# "debtId": "string", +# "customerId": "string", +# "accountId": "string", +# "productId": "string", +# "provideAmount": 0, +# "collectAmountInterest": 0, +# "collectAmountMgtFee": 0, +# "collectAmountInsurance": 0, +# "collectAmountVAT": 0, +# "countryId": "string", +# "comment": "string" +# } +# ''' try: logger.info(f"Here is your Disbursement Request data ****** : {disbursement_data}") response = requests.post(api_url, json=disbursement_data, timeout=10, headers=get_headers()) + logger.info(f"Call to bank end point returned with Here is your Disbursement Request data ****** : {disbursement_data}") if response.status_code == 404: - logger.error("Received 404 from external service") + logger.error("") + LoanService.set_disbursement_loan_description(loan_data['debtId'],"Disbursement Service url not found (404)" ) return ResponseHelper.error("Disbursement Service url not found (404)", status_code=404) + logger.info(f"Disbursement response: {response.json()}") - - result = response.json() - LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', '')) - return ResponseHelper.success(response.json(), "Successful") + + if response.status_code == 200: + result = response.json() + LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', '')) + reload_loan = LoanService.get_loan_by_transaction_id(transaction_id=data['transactionId']) + reload_loan_data = reload_loan.to_dict() + verify_disbursement_transaction(reload_loan_data) + return ResponseHelper.success(response.json(), "Successful") + + else: + logger.error("") + errorMessage = "Unable to complete Disbursement Service with HTTP status code: " + str(response.status_code) + LoanService.set_disbursement_loan_description(loan_data['debtId'],errorMessage ) + return ResponseHelper.error(errorMessage, status_code=response.status_code) + except Exception as e: logger.info(f"Failed to call Disbursement endpoint: {e}") return 0 return 1 + @staticmethod + def verify_disbursement_transaction(loan_data): + if loan_data['disburseResult'] and loan_data['disburseResult'] == '00': + verify_transaction(loan_data) + + @staticmethod def verify_transaction(data): api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}{SimbrellaClient.BANK_CALL_TRANSACTION_VERIFY}" @@ -164,7 +208,17 @@ class SimbrellaClient: "countryId": "NG", "requestId": loan_data.get('transactionId') } - +# ''' +# { Verify with bank +# "accountId": "string", +# "customerId": "string", +# "transactionId": "string", +# "fbnTransactionId": "string", +# "transactionType": "string", +# "countryId": "string", +# "requestId": "string" +# } +# ''' try: logger.info(f"Here is your TransactionVerify Request data ****** : {verify_data}") response = requests.post(api_url, json=verify_data, timeout=10, headers=get_headers()) diff --git a/app/models/loan.py b/app/models/loan.py index 8a4b9a7..b352810 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -158,6 +158,29 @@ class Loan(db.Model): logger.error(f"Failed to update disburse verify date: {e}") raise + @classmethod + def set_disbursement_message(cls, loan_id, description): + """ + Update the disburse result and description of the loan with the given loan_id. + """ + # Retrieve loan + loan = cls.query.get(loan_id) + + if not loan: + raise ValueError(f"Loan with ID {loan_id} does not exist.") + + # Update disburse description only + loan.disburse_description = description + + # Commit changes to database + try: + logger.info(f"Updating disburse result for loan ID {loan_id} with description {description}") + db.session.commit() + except Exception as e: + db.session.rollback() + logger.error(f"Failed to update disbursement result: {e}") + raise + @classmethod def set_disbursement_result(cls, loan_id, result, description): @@ -182,6 +205,8 @@ class Loan(db.Model): db.session.rollback() logger.error(f"Failed to update disbursement result: {e}") raise + + @classmethod def set_disburse_verify_result(cls, loan_id, result, description): """ diff --git a/app/routes/autocall.py b/app/routes/autocall.py index f3ced32..04eb5ce 100644 --- a/app/routes/autocall.py +++ b/app/routes/autocall.py @@ -31,7 +31,7 @@ def verify_transaction(): data = { "transactionId": loan_data.get('transactionId'), - "FbnTransactionId": loan_data.get('transactionId'), + "fbnTransactionId": loan_data.get('transactionId'), "debtId": str(loan_data.get('debtId')), "customerId": loan_data.get('customerId'), "accountId": loan_data.get('accountId'), @@ -64,6 +64,32 @@ def disbursement(): response = SimbrellaClient.disburse_loan(data) return response +@autocall_bp.route("/retry-disbursement", methods=["POST"]) +def retry_disbursement(): + data = request.get_json() + logger.info(f"Data received: {data}") + + transactionId = data["transactionId"] + + logger.info(f"Calling Disbursement Components") + loan = LoanService.get_loan_by_transaction_id(transactionId) + if not loan: + logger.info(f"No loan found without disbursement date") + return 0 + logger.info(f"Calling DisburseLoan endpoint with data: {loan}") + loan_data = loan.to_dict() + + data = { + "transactionId": loan_data.get('transactionId'), + "FbnTransactionId": loan_data.get('transactionId'), + "debtId": str(loan_data.get('debtId')), + "customerId": loan_data.get('customerId'), + "accountId": loan_data.get('accountId'), + "productId": str(loan_data.get('productId', "")), + "provideAmount": loan_data.get('currentLoanAmount'), + } + response = SimbrellaClient.disburse_loan(data) + return response @autocall_bp.route("/direct/loan", methods=["POST"]) diff --git a/app/services/loan.py b/app/services/loan.py index 9ba1867..e8c12e0 100644 --- a/app/services/loan.py +++ b/app/services/loan.py @@ -54,6 +54,13 @@ class LoanService: Update the disbursement result of the loan with the given loan_id. """ return Loan.set_disbursement_result(loan_id, result, description) + + @classmethod + def set_disbursement_loan_description(cls,loan_id,description): + + return Loan.set_disbursement_message(loan_id, result, description) + + @classmethod def set_disburse_verify_result(cls, loan_id, result, description): """