166 lines
5.8 KiB
Python
166 lines
5.8 KiB
Python
from flask import Blueprint, request, jsonify, current_app
|
|
import requests
|
|
from app.config import settings
|
|
from app.helpers.response_helper import ResponseHelper
|
|
from app.utils.auth import get_headers
|
|
from app.utils.logger import logger
|
|
from app.integrations.simbrella import SimbrellaClient
|
|
from app.services.loan import LoanService
|
|
from app.services.repayment import RepaymentService
|
|
from app.services.salary import SalaryService
|
|
|
|
autocall_bp = Blueprint("autocall", __name__)
|
|
|
|
@autocall_bp.route("/refresh-verify-disbursement", methods=["GET"])
|
|
def verify_transaction():
|
|
logger.info(f"Calling VerifyTransaction Components")
|
|
|
|
loan = LoanService.get_latest_loan_with_disburse_date()
|
|
if not loan:
|
|
logger.info(f"No loan found without disbursement date")
|
|
return 0
|
|
logger.info(f"Calling VerifyTransaction 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.verify_transaction(data)
|
|
return response
|
|
|
|
@autocall_bp.route("/refresh-disbursement", methods=["GET"])
|
|
def disbursement():
|
|
# data = request.json()
|
|
logger.info(f"Calling Disbursement Components")
|
|
loan = LoanService.get_latest_loan_without_disburse_date()
|
|
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("/refresh-verify-collection", methods=["GET"])
|
|
def refresh_verify_collection():
|
|
data = request.get_json()
|
|
logger.info(f"Calling Verify Collection")
|
|
|
|
response = SimbrellaClient.collect_loan(data)
|
|
|
|
return response
|
|
|
|
@autocall_bp.route("/refresh-collection", methods=["GET"])
|
|
def refresh_collection():
|
|
#data = request.get_json()
|
|
logger.info(f"Calling Collection ")
|
|
#grab the last repayments with repay date is none
|
|
repayment = RepaymentService.get_latest_repayment_without_repay_date()
|
|
#repayment = RepaymentService.get_latest_repayment_with_loanId(13735)
|
|
if not repayment:
|
|
logger.info(f"No repayment found without disbursement date")
|
|
return 0
|
|
logger.info(f"Calling repay loan endpoint with data: {repayment}")
|
|
repayment_data = repayment.to_dict()
|
|
logger.info(f"here is the dict form of repayment {repayment_data}")
|
|
|
|
data = {
|
|
"transactionId": repayment_data['transactionId'],
|
|
"debtId": repayment_data['loanId'],
|
|
"customerId": repayment_data['customerId'],
|
|
"productId": repayment_data['productId'],
|
|
}
|
|
logger.info(f"Data being sent to Simbrella: {data}")
|
|
logger.info(f"calling simbrella")
|
|
response = SimbrellaClient.collect_loan_user_initiated(data)
|
|
|
|
return response
|
|
|
|
@autocall_bp.route("/payment-callback", methods=["POST"])
|
|
def payment_callback():
|
|
data = request.get_json()
|
|
logger.info(f"Calling Callback Components")
|
|
|
|
response = SimbrellaClient.payment_callback(data)
|
|
|
|
return response
|
|
|
|
@autocall_bp.route("/penal-charge", methods=["POST"])
|
|
def penal_charge():
|
|
data = request.get_json()
|
|
logger.info(f"Calling Penal Charge Endpoints")
|
|
|
|
response = SimbrellaClient.penal_charge(data[0])
|
|
|
|
return response
|
|
|
|
@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")
|
|
|
|
try:
|
|
salary = SalaryService.add_salary_data(data)
|
|
if salary:
|
|
logger.info(f"Successful Salary Added")
|
|
# return ResponseHelper.success(salary.to_dict(), "Successful")
|
|
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
|
|
# **************************************************
|
|
|
|
pending_salaries = SalaryService.get_pending_salaries()
|
|
|
|
if not pending_salaries:
|
|
logger.info(f"No pending salaries found")
|
|
# return ResponseHelper.success([], "No pending salaries found")
|
|
|
|
logger.info(f"Found {len(pending_salaries)} 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'
|
|
# )
|
|
|
|
# by the time you call this you must be on repayment table
|
|
try:
|
|
SimbrellaClient.collect_loan_user_salary_detect(data)
|
|
except Exception as e:
|
|
logger.info(f"Failed to call collect_loan_user_salary_detect: {e}")
|
|
|
|
|
|
return_data=[]
|
|
|
|
return ResponseHelper.success(return_data, "AutoCall Successful")
|