This commit is contained in:
Azeez Muibi
2025-04-13 23:58:02 +01:00
parent 6f5b95771f
commit c00bc3ddbf
9 changed files with 563 additions and 53 deletions
+31 -33
View File
@@ -24,42 +24,36 @@ class LoanService(BaseService):
# Extract filters
customer_id = filters.get('customer_id')
account_id = filters.get('account_id')
offer_id = filters.get('offer_id')
status = filters.get('status')
offer_id = filters.get('offer_id')
product_id = filters.get('product_id')
start_date = filters.get('start_date')
end_date = filters.get('end_date')
due_before = filters.get('due_before')
due_after = filters.get('due_after')
# Convert string dates to datetime objects if provided
if start_date and isinstance(start_date, str):
start_date = datetime.fromisoformat(start_date.replace('Z', '+00:00'))
if end_date and isinstance(end_date, str):
end_date = datetime.fromisoformat(end_date.replace('Z', '+00:00'))
if due_before and isinstance(due_before, str):
due_before = datetime.fromisoformat(due_before.replace('Z', '+00:00'))
if due_after and isinstance(due_after, str):
due_after = datetime.fromisoformat(due_after.replace('Z', '+00:00'))
# Get loans with filters
query = Loan.query
if customer_id:
query = query.filter(Loan.customer_id == customer_id)
if account_id:
query = query.filter(Loan.account_id == account_id)
if offer_id:
query = query.filter(Loan.offer_id == offer_id)
if status:
query = query.filter(Loan.status == status)
if start_date:
query = query.filter(Loan.created_at >= start_date)
if end_date:
query = query.filter(Loan.created_at <= end_date)
# Order by created_at descending (newest first)
query = query.order_by(Loan.created_at.desc())
loans = query.all()
# Get loans with optional filters
loans = Loan.get_all_loans(
customer_id=customer_id,
account_id=account_id,
status=status,
offer_id=offer_id,
product_id=product_id,
start_date=start_date,
end_date=end_date,
due_before=due_before,
due_after=due_after
)
# Convert loans to dictionary format
loans_data = []
@@ -69,10 +63,15 @@ class LoanService(BaseService):
'customer_id': loan.customer_id,
'account_id': loan.account_id,
'offer_id': loan.offer_id,
'principal_amount': loan.principal_amount,
'initial_loan_amount': loan.initial_loan_amount,
'current_loan_amount': loan.current_loan_amount,
'status': loan.status,
'created_at': loan.created_at.isoformat(),
'updated_at': loan.updated_at.isoformat()
'product_id': loan.product_id,
'default_penalty_fee': loan.default_penalty_fee,
'continuous_fee': loan.continuous_fee,
'due_date': loan.due_date.isoformat() if loan.due_date else None,
'created_at': loan.created_at.isoformat() if loan.created_at else None,
'updated_at': loan.updated_at.isoformat() if loan.updated_at else None
})
response_data = {
@@ -83,8 +82,7 @@ class LoanService(BaseService):
return response_data
except Exception as e:
logger.error(f"Error retrieving loans: {str(e)}", exc_info=True)
logger.error(f"An error occurred: {str(e)}", exc_info=True)
return jsonify({
'status': 'error',
'message': f'Failed to retrieve loans: {str(e)}'
}), 500
"message": "Internal Server Error"
}), 500