This commit is contained in:
Chinenye Nmoh
2025-06-26 13:31:51 +01:00
parent d863285e9a
commit 9feab46016
2 changed files with 27 additions and 3 deletions
+10 -3
View File
@@ -251,7 +251,7 @@ class SimbrellaClient:
logger.info(f"Here is your repayment data after setting repay date: {repayment_data}")
debtId = str(loan_data.get('debtId', "")).strip().zfill(6)
collect_loan_data = {
"transactionId": repayment_data['transactionId'],
"transactionId": loan_data['transactionId'],
"fbnTransactionId": loan_data['transactionId'],
"debtId": debtId,
"customerId": repayment_data['customerId'],
@@ -269,7 +269,6 @@ class SimbrellaClient:
try:
logger.info(f"Here is your CollectLoan Request data ***** : {collect_loan_data}")
response = requests.post(api_url, json=collect_loan_data,timeout=30, headers=get_headers())
logger.info(f"headers: {get_headers()}")
logger.info(f"CollectLoan response: {response.json()}")
RepaymentService.set_repay_result(repayment_data['Id'], response.json().get('responseCode', ''), response.json().get('responseMessage', ''))
@@ -297,7 +296,15 @@ class SimbrellaClient:
if result.get('responseCode') == '00':
amount_collected = Decimal(str(result.get('amountCollected', 0))).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
logger.info(f'amount collected {amount_collected}')
updated_loan = LoanService.update_loan_balance(int(loan_data['debtId']), amount_collected)
try:
updated_loan = LoanService.update_loan_balance(int(loan_data['debtId']), amount_collected)
except ValueError as ve:
logger.error(f"Validation error updating loan balance: {ve}")
return ResponseHelper.error(str(ve), status_code=400)
except Exception as ex:
logger.error(f"Unexpected error updating loan balance: {ex}")
return ResponseHelper.error("Unexpected error updating loan balance", status_code=500)
logger.info(f'updated loan {updated_loan}')
updated_balance = Decimal(str(updated_loan['balance'])).quantize(Decimal('0.01'))
logger.info(f'updated balance {updated_balance}')
+17
View File
@@ -242,7 +242,24 @@ class Loan(db.Model):
logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}")
return customer_loans, total_amount
@classmethod
def get_customer_active_loans(cls, customer_id):
"""
Get customer's active loans and sum by customer_id.
"""
customer_loans = cls.query.filter_by( customer_id = customer_id).all()
if not customer_loans:
raise ValueError(f"Customer with Id {customer_id} does not have any loan.")
total_amount = (
cls.query.with_entities(func.coalesce(func.sum(cls.balance), 0.0))
.filter_by(customer_id=customer_id)
.scalar()
)
logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}")
return customer_loans, total_amount
@classmethod