diff --git a/app/routes/autocall.py b/app/routes/autocall.py index 2d551a5..1f8d50e 100644 --- a/app/routes/autocall.py +++ b/app/routes/autocall.py @@ -117,54 +117,29 @@ def retry_verify_disbursement(): 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'), "productId": str(loan_data.get('productId', "")), "provideAmount": loan_data.get('currentLoanAmount'), } - # response = SimbrellaClient.disburse_loan(data) - # logger.info(f"Retry Disbursement Transaction ID Result Received for :::: {response}") - return ResponseHelper.success(message="Retry Disbursement Request Sent Successfully", status_code=200) + response = SimbrellaClient.verify_transaction(data) + return ResponseHelper.success(message="Retry Verify Disbursement Request Sent Successfully", status_code=200) except Exception as e: logger.error(f"Failed to call retry disbursement {data}: {e}") @autocall_bp.route("/start-repayment", methods=["POST"]) -def start_repayment(): +def start_repayment_office(): try: data = request.get_json() logger.info(f"Start Repay Transaction ID Data Received for :::: {data}") - - transactionId = data["transactionId"] - logger.info(f"Starting Transaction ID Data Received for :::: {transactionId}") - - logger.info(f"Calling Disbursement Components for Retry Transaction ID Data Received for :::: {transactionId}") - 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) - # logger.info(f"Retry Disbursement Transaction ID Result Received for :::: {response}") + loan_repayment_function(data, 'OFFICE') return ResponseHelper.success(message="Retry Disbursement Request Sent Successfully", status_code=200) except Exception as e: logger.error(f"Failed to call retry disbursement {data}: {e}") - - @autocall_bp.route("/direct/loan", methods=["POST"]) def direct_loan(): data = request.get_json() @@ -304,8 +279,87 @@ def direct_repayment(): response = SimbrellaClient.collect_loan_user_initiated(data_to_process) return response - - + + +def loan_repayment_function(data, initiatedBy): + logger.info(f"Data Received Loan Repayment: {data} By {initiatedBy}") + + REQUIRED_KEYS = ["transactionId"] + + # Check for missing keys + missing_keys = [key for key in REQUIRED_KEYS if key not in data or data[key] is None] + if missing_keys: + logger.warning(f"Missing required keys: {missing_keys}") + return jsonify({ + "status": "error", + "message": f"Missing required fields: {', '.join(missing_keys)}" + }), 400 + + # Check if the loan exists + logger.info(f"Checking if loan with transaction id {data['transactionId']} exists") + loan = LoanService.get_loan_by_transaction_id(transaction_id=data['transactionId']) + if not loan: + logger.info(f"Loan with transaction id {data['transactionId']} does not exist") + return jsonify({ + "status": "error", + "message": f"Loan with transaction id {data['transactionId']} does not exist" + }), 400 + + loan_data = loan.to_dict() + + # check if loan has been repaid + if loan_data.get("status") == LoanStatus.REPAID and loan_data.get("balance") <= 0: + logger.info(f"Loan with Id {loan_data.get('debtId')} has been repaid") + return jsonify({ + "status": "error", + "message": f"loan with Id {loan_data.get('debtId')} has been repaid" + }), 400 + + repayment_data = { + "customerId": loan_data.get("customerId"), + "loanId": loan_data.get("debtId"), + "productId": loan_data.get("productId"), + "transactionId": loan_data.get("transactionId"), + "initiatedBy": initiatedBy, + "salaryAmount": 0, + "LoanStatus": loan_data.get("status"), + } + + logger.info(f"Creating repayment with data: {repayment_data}") + + try: + repayment = RepaymentService.create_repayment(repayment_data) + logger.info(f"Repayment created: {repayment}") + except Exception as e: + db.session.rollback() + logger.error(f"Repayment creation raised exception: {e}") + return jsonify({ + "status": "error", + "message": "Failed to create repayment" + }), 500 + + if not repayment or (isinstance(repayment, dict) and "error" in repayment): + db.session.rollback() + logger.error(f"Repayment creation failed for loan ID {loan_data.get('debtId')}: {repayment}") + + try: + if loan_data.get('status') == LoanStatus.ACTIVE: + LoanService.update_status(loan_id=loan_data.get('debtId'), status=LoanStatus.START_REPAY) + except Exception as e: + db.session.rollback() + logger.error(f"Failed to update loan status for loan ID {loan_data.get('debtId')}: {e}") + repayment_data_dict = repayment.to_dict() + + data_to_process = { + "transactionId": repayment_data_dict['transactionId'], + "debtId": repayment_data_dict['loanId'], + "customerId": repayment_data_dict['customerId'], + "productId": repayment_data_dict['productId'], + "Id": repayment_data_dict['Id'] + } + + response = SimbrellaClient.collect_loan_user_initiated(data_to_process) + return response @autocall_bp.route("/refresh-verify-collection", methods=["GET"])