This commit is contained in:
Azeez Muibi
2025-04-28 08:40:14 +01:00
parent 0052340843
commit 75e9a96ba3
11 changed files with 529 additions and 44 deletions
+22 -3
View File
@@ -1,6 +1,7 @@
from flask import Blueprint, request, jsonify, send_from_directory
from flask import Blueprint, request, jsonify
from app.api.services import LoanRepaymentScheduleService
from app.api.services.repayment_service import RepaymentService
from app.api.services.loan_charge_service import LoanChargeService
from app.api.services.loan_service import LoanService
@@ -98,18 +99,19 @@ def get_dashboard():
result = DashboardService.get_dashboard_data()
return jsonify(result)
@api.route('/loans', methods=['GET'])
# @token_required
def get_loans():
# Extract query parameters for filtering
filters = {
'id': request.args.get('id'),
'customer_id': request.args.get('customer_id'),
'account_id': request.args.get('account_id'),
'status': request.args.get('status'),
'offer_id': request.args.get('offer_id'),
'product_id': request.args.get('product_id'),
'transaction_id': request.args.get('transaction_id'),
'original_transaction': request.args.get('original_transaction'),
'start_date': request.args.get('start_date'),
'end_date': request.args.get('end_date'),
'due_before': request.args.get('due_before'),
@@ -121,7 +123,6 @@ def get_loans():
response = LoanService.process_request(filters)
return response
@api.route('/transactions', methods=['GET'])
# @token_required
def get_transactions():
@@ -172,4 +173,22 @@ def get_all_loan_charges():
}
# logger.info(f"Get loan charges request received with filters: {filters}")
response = LoanChargeService.get_all_loan_charges(filters)
return jsonify(response)
@api.route('/repayment-schedules', methods=['GET'])
# @token_required
def get_all_repayment_schedules():
# Extract query parameters for filtering
filters = {
'loan_id': request.args.get('loan_id'),
'product_id': request.args.get('product_id'),
'paid': request.args.get('paid'),
'due_before': request.args.get('due_before'),
'due_after': request.args.get('due_after'),
'installment_number': request.args.get('installment_number'),
'page': request.args.get('page', 1),
'limit': request.args.get('limit', 20)
}
# logger.info(f"Get repayment schedules request received with filters: {filters}")
response = LoanRepaymentScheduleService.get_all_repayment_schedules(filters)
return jsonify(response)
+1
View File
@@ -7,3 +7,4 @@ from app.api.services.auth_service import AuthService
from app.api.services.dashboard_service import DashboardService
from app.api.services.repayment_service import RepaymentService
from app.api.services.loan_charge_service import LoanChargeService
from app.api.services.loan_repayment_schedule_service import LoanRepaymentScheduleService
@@ -0,0 +1,108 @@
import logging
from datetime import datetime
from flask import jsonify
from app.models.loan_repayment_schedule import LoanRepaymentSchedule
# Configure logging
logger = logging.getLogger(__name__)
class LoanRepaymentScheduleService:
"""
Service class for handling loan repayment schedule-related operations.
"""
@staticmethod
def get_all_repayment_schedules(filters=None):
"""
Get all loan repayment schedules with optional filtering.
Args:
filters (dict, optional): Filters for the loan repayment schedules query.
Returns:
dict: A standardized response with loan repayment schedules data.
"""
try:
if filters is None:
filters = {}
# Extract filters
loan_id = filters.get('loan_id')
product_id = filters.get('product_id')
paid = filters.get('paid')
due_before = filters.get('due_before')
due_after = filters.get('due_after')
installment_number = filters.get('installment_number')
# Extract pagination parameters
page = int(filters.get('page', 1))
limit = int(filters.get('limit', 20))
# Ensure page and limit are valid
if page < 1:
page = 1
if limit < 1 or limit > 100:
limit = 20
# Convert string dates to datetime objects if provided
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'))
# Convert paid string to boolean if provided
if paid is not None and isinstance(paid, str):
paid = paid.lower() == 'true'
# Get loan repayment schedules with optional filters and pagination
schedules, total_count = LoanRepaymentSchedule.get_all_repayment_schedules(
loan_id=loan_id,
product_id=product_id,
paid=paid,
due_before=due_before,
due_after=due_after,
installment_number=installment_number,
page=page,
limit=limit
)
# Convert schedules to dictionary format
schedules_data = []
for schedule in schedules:
schedules_data.append({
'id': schedule.id,
'loan_id': schedule.loan_id,
'product_id': schedule.product_id,
'installment_number': schedule.installment_number,
'due_date': schedule.due_date.isoformat() if schedule.due_date else None,
'installment_amount': schedule.installment_amount,
'total_repayment_amount': schedule.total_repayment_amount,
'paid': schedule.paid,
'paid_at': schedule.paid_at.isoformat() if schedule.paid_at else None,
'created_at': schedule.created_at.isoformat() if schedule.created_at else None,
'updated_at': schedule.updated_at.isoformat() if schedule.updated_at else None
})
# Calculate total pages
total_pages = (total_count + limit - 1) // limit
response_data = {
'repayment_schedules': schedules_data,
'count': len(schedules_data),
'pagination': {
'total_count': total_count,
'total_pages': total_pages,
'current_page': page,
'limit': limit,
'has_next': page < total_pages,
'has_prev': page > 1
}
}
return response_data
except Exception as e:
logger.error(f"An error occurred: {str(e)}", exc_info=True)
return jsonify({
"message": "Internal Server Error"
}), 500
+6
View File
@@ -28,11 +28,14 @@ class LoanService:
filters = {}
# Extract filters
id = filters.get('id')
customer_id = filters.get('customer_id')
account_id = filters.get('account_id')
status = filters.get('status')
offer_id = filters.get('offer_id')
product_id = filters.get('product_id')
transaction_id = filters.get('transaction_id')
original_transaction = filters.get('original_transaction')
start_date = filters.get('start_date')
end_date = filters.get('end_date')
due_before = filters.get('due_before')
@@ -60,11 +63,14 @@ class LoanService:
# Get loans with optional filters and pagination
loans, total_count = Loan.get_all_loans(
id=id,
customer_id=customer_id,
account_id=account_id,
status=status,
offer_id=offer_id,
product_id=product_id,
transaction_id=transaction_id,
original_transaction=original_transaction,
start_date=start_date,
end_date=end_date,
due_before=due_before,