catch 404 error #35
@@ -4,6 +4,9 @@ from app.helpers.response_helper import ResponseHelper
|
|||||||
from app.services.loan import LoanService
|
from app.services.loan import LoanService
|
||||||
from app.utils.auth import get_headers
|
from app.utils.auth import get_headers
|
||||||
from app.utils.extras import preprocess_loan_charges_data
|
from app.utils.extras import preprocess_loan_charges_data
|
||||||
|
import random
|
||||||
|
import random
|
||||||
|
import string
|
||||||
from app.utils.logger import logger
|
from app.utils.logger import logger
|
||||||
from flask import jsonify, current_app
|
from flask import jsonify, current_app
|
||||||
from app.services.transactions import TransactionService
|
from app.services.transactions import TransactionService
|
||||||
@@ -90,6 +93,9 @@ class SimbrellaClient:
|
|||||||
try:
|
try:
|
||||||
logger.info(f"Here is your Disbursement Request data ****** : {disbursement_data}")
|
logger.info(f"Here is your Disbursement Request data ****** : {disbursement_data}")
|
||||||
response = requests.post(api_url, json=disbursement_data, timeout=10, headers=get_headers())
|
response = requests.post(api_url, json=disbursement_data, timeout=10, headers=get_headers())
|
||||||
|
if response.status_code == 404:
|
||||||
|
logger.error("Received 404 from external service")
|
||||||
|
return ResponseHelper.error("Disbursement Service url not found (404)", status_code=404)
|
||||||
logger.info(f"Disbursement response: {response.json()}")
|
logger.info(f"Disbursement response: {response.json()}")
|
||||||
result = response.json()
|
result = response.json()
|
||||||
LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
||||||
@@ -154,6 +160,9 @@ class SimbrellaClient:
|
|||||||
try:
|
try:
|
||||||
logger.info(f"Here is your TransactionVerify Request data ****** : {verify_data}")
|
logger.info(f"Here is your TransactionVerify Request data ****** : {verify_data}")
|
||||||
response = requests.post(api_url, json=verify_data, timeout=10, headers=get_headers())
|
response = requests.post(api_url, json=verify_data, timeout=10, headers=get_headers())
|
||||||
|
if response.status_code == 404:
|
||||||
|
logger.error("Received 404 from external service")
|
||||||
|
return ResponseHelper.error("Verify Service url not found (404)", status_code=404)
|
||||||
result = response.json()
|
result = response.json()
|
||||||
logger.info(f"this is verify result, {result}")
|
logger.info(f"this is verify result, {result}")
|
||||||
LoanService.set_disburse_verify_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
LoanService.set_disburse_verify_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
||||||
@@ -165,6 +174,7 @@ class SimbrellaClient:
|
|||||||
try:
|
try:
|
||||||
sms_response = requests.post(sms_url, json=sms_data, timeout=10, headers=get_headers())
|
sms_response = requests.post(sms_url, json=sms_data, timeout=10, headers=get_headers())
|
||||||
sms_response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
|
sms_response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
|
||||||
|
|
||||||
result = sms_response.json()
|
result = sms_response.json()
|
||||||
logger.info(f"SMS Response JSON: {result}")
|
logger.info(f"SMS Response JSON: {result}")
|
||||||
if result.get('isSuccess'):
|
if result.get('isSuccess'):
|
||||||
@@ -241,14 +251,15 @@ class SimbrellaClient:
|
|||||||
repayment_data = repayment.to_dict()
|
repayment_data = repayment.to_dict()
|
||||||
|
|
||||||
debtId = str(loan_data.get('debtId', "")).strip().zfill(6)
|
debtId = str(loan_data.get('debtId', "")).strip().zfill(6)
|
||||||
|
t_id = ''.join(random.choices(string.ascii_uppercase, k=22))
|
||||||
collect_loan_data = {
|
collect_loan_data = {
|
||||||
"transactionId": loan_data['transactionId'],
|
"transactionId": t_id, #made this a random string to avoid duplicate transaction with transactionId from Charles
|
||||||
"fbnTransactionId": loan_data['transactionId'],
|
"fbnTransactionId": loan_data['transactionId'],
|
||||||
"debtId": debtId,
|
"debtId": debtId,
|
||||||
"customerId": repayment_data['customerId'],
|
"customerId": repayment_data['customerId'],
|
||||||
"accountId": loan_data['accountId'],
|
"accountId": loan_data['accountId'],
|
||||||
"productId": repayment_data['productId'],
|
"productId": repayment_data['productId'],
|
||||||
"collectAmount": loan_data['repaymentAmount'] or 0,
|
"collectAmount": loan_data['balance'] or 0,
|
||||||
"penalCharge": 0,
|
"penalCharge": 0,
|
||||||
"channel": "USSD",
|
"channel": "USSD",
|
||||||
"collectionMethod": collectionMethod,
|
"collectionMethod": collectionMethod,
|
||||||
@@ -258,8 +269,17 @@ class SimbrellaClient:
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.info(f"Sending CollectLoan request: {collect_loan_data}")
|
logger.info(f"Sending CollectLoan request............ {collect_loan_data}")
|
||||||
response = requests.post(api_url, json=collect_loan_data, timeout=30, headers=get_headers())
|
response = requests.post(api_url, json=collect_loan_data, timeout=30, headers=get_headers())
|
||||||
|
logger.info(f"respnse structure {response}")
|
||||||
|
if response.status_code == 404:
|
||||||
|
RepaymentService.set_repay_result(
|
||||||
|
repayment_data['Id'],
|
||||||
|
'404',
|
||||||
|
'Collection Service url not found')
|
||||||
|
logger.error("Received 404 from external service")
|
||||||
|
return ResponseHelper.error("Collection Service url not found", status_code=404)
|
||||||
|
|
||||||
result = response.json()
|
result = response.json()
|
||||||
logger.info(f"CollectLoan response: {result}")
|
logger.info(f"CollectLoan response: {result}")
|
||||||
|
|
||||||
@@ -269,13 +289,9 @@ class SimbrellaClient:
|
|||||||
result.get('responseMessage', '')
|
result.get('responseMessage', '')
|
||||||
)
|
)
|
||||||
|
|
||||||
if not result.get('transactionId'):
|
|
||||||
logger.error("Missing transactionId in response")
|
|
||||||
return ResponseHelper.error("Missing transactionId from Simbrella response", status_code=500)
|
|
||||||
|
|
||||||
data_to_add = {
|
data_to_add = {
|
||||||
"transactionId": result.get('transactionId') or collect_loan_data.get('transactionId'),
|
"transactionId": result.get('transactionId') or collect_loan_data.get('transactionId'),
|
||||||
"fbnTransactionId": result.get('fbnTransactionId') or collect_loan_data.get('fbnTransactionId'),
|
"fbnTransactionId": loan_data['transactionId'],
|
||||||
"accountId": result.get('accountId') or collect_loan_data.get('accountId'),
|
"accountId": result.get('accountId') or collect_loan_data.get('accountId'),
|
||||||
"customerId": result.get('customerId') or collect_loan_data.get('customerId'),
|
"customerId": result.get('customerId') or collect_loan_data.get('customerId'),
|
||||||
"amountCollected": float(result.get('amountCollected', 0)),
|
"amountCollected": float(result.get('amountCollected', 0)),
|
||||||
@@ -325,8 +341,8 @@ class SimbrellaClient:
|
|||||||
return ResponseHelper.success(result, "Successful")
|
return ResponseHelper.success(result, "Successful")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception("Failed to call CollectLoan endpoint")
|
logger.exception("Failed to call CollectLoan endpoint, {e}")
|
||||||
return ResponseHelper.error("Failed to call CollectLoan endpoint")
|
return ResponseHelper.error("Failed to process salary list", status_code=500, error=str(e))
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ from app.extensions import db
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from app.utils.logger import logger
|
from app.utils.logger import logger
|
||||||
from app.enums.loan_status import LoanStatus
|
from app.enums.loan_status import LoanStatus
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
|
|
||||||
class Repayment(db.Model):
|
class Repayment(db.Model):
|
||||||
__tablename__ = "repayments"
|
__tablename__ = "repayments"
|
||||||
@@ -51,7 +53,7 @@ class Repayment(db.Model):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def create_repayment(cls, repayment_data):
|
def create_repayment(cls, repayment_data):
|
||||||
|
|
||||||
if repayment_data["LoanStatus"] not in [LoanStatus.ACTIVE, LoanStatus.START_REPAY]:
|
if repayment_data["LoanStatus"] not in [LoanStatus.ACTIVE, LoanStatus.START_REPAY,LoanStatus.ACTIVE_PARTIAL]:
|
||||||
raise ValueError(f"Repayment cannot be processed. Loan status: ({repayment_data['LoanStatus']})")
|
raise ValueError(f"Repayment cannot be processed. Loan status: ({repayment_data['LoanStatus']})")
|
||||||
|
|
||||||
repayment = cls(
|
repayment = cls(
|
||||||
@@ -70,9 +72,9 @@ class Repayment(db.Model):
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
logger.info("Repayment record committed.")
|
logger.info("Repayment record committed.")
|
||||||
return repayment
|
return repayment
|
||||||
except InqtegrityError as err:
|
except IntegrityError as err:
|
||||||
logger.error(f"Database integrity error: {err}")
|
logger.error(f"Database integrity error: {err}")
|
||||||
return [q]
|
return {"error": "Integrity error", "details": str(err)}
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user