162 lines
6.1 KiB
Python
162 lines
6.1 KiB
Python
import logging
|
|
from datetime import datetime
|
|
from flask import jsonify
|
|
|
|
# Configure logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Loan: # Mock Loan class for demonstration
|
|
def __init__(self, id, customer_id, account_id, offer_id, initial_loan_amount, current_loan_amount, status,
|
|
product_id, default_penalty_fee, continuous_fee, due_date, created_at, updated_at):
|
|
self.id = id
|
|
self.customer_id = customer_id
|
|
self.account_id = account_id
|
|
self.offer_id = offer_id
|
|
self.initial_loan_amount = initial_loan_amount
|
|
self.current_loan_amount = current_loan_amount
|
|
self.status = status
|
|
self.product_id = product_id
|
|
self.default_penalty_fee = default_penalty_fee
|
|
self.continuous_fee = continuous_fee
|
|
self.due_date = due_date
|
|
self.created_at = created_at
|
|
self.updated_at = updated_at
|
|
|
|
@staticmethod
|
|
def get_all_loans(customer_id=None, account_id=None, status=None, offer_id=None, product_id=None, start_date=None,
|
|
end_date=None, due_before=None, due_after=None, page=1, limit=20):
|
|
# This is a mock implementation. In a real application, this would query a database.
|
|
loans = []
|
|
# Create some dummy loans for testing
|
|
for i in range(limit):
|
|
loan = Loan(
|
|
id=i + (page - 1) * limit,
|
|
customer_id=customer_id or "customer123",
|
|
account_id=account_id or "account456",
|
|
offer_id=offer_id or "offer789",
|
|
initial_loan_amount=1000.00,
|
|
current_loan_amount=900.00,
|
|
status=status or "active",
|
|
product_id=product_id or "product101",
|
|
default_penalty_fee=50.00,
|
|
continuous_fee=10.00,
|
|
due_date=datetime.now(),
|
|
created_at=datetime.now(),
|
|
updated_at=datetime.now()
|
|
)
|
|
loans.append(loan)
|
|
total_count = 100 # Example total count
|
|
return loans, total_count
|
|
|
|
|
|
class LoanService:
|
|
"""
|
|
Service class for handling loan-related operations.
|
|
"""
|
|
|
|
@staticmethod
|
|
def process_request(filters=None):
|
|
"""
|
|
Process the get loans request.
|
|
|
|
Args:
|
|
filters (dict, optional): Filters for the loans query.
|
|
|
|
Returns:
|
|
dict: A standardized response with loans data.
|
|
"""
|
|
try:
|
|
if filters is None:
|
|
filters = {}
|
|
|
|
# Extract filters
|
|
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')
|
|
start_date = filters.get('start_date')
|
|
end_date = filters.get('end_date')
|
|
due_before = filters.get('due_before')
|
|
due_after = filters.get('due_after')
|
|
|
|
# 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 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 optional filters and pagination
|
|
loans, total_count = 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,
|
|
page=page,
|
|
limit=limit
|
|
)
|
|
|
|
# Convert loans to dictionary format
|
|
loans_data = []
|
|
for loan in loans:
|
|
loans_data.append({
|
|
'id': loan.id,
|
|
'customer_id': loan.customer_id,
|
|
'account_id': loan.account_id,
|
|
'offer_id': loan.offer_id,
|
|
'initial_loan_amount': loan.initial_loan_amount,
|
|
'current_loan_amount': loan.current_loan_amount,
|
|
'status': loan.status,
|
|
'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
|
|
})
|
|
|
|
# Calculate total pages
|
|
total_pages = (total_count + limit - 1) // limit
|
|
|
|
response_data = {
|
|
'loans': loans_data,
|
|
'count': len(loans_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"
|
|
# "message": f"Internal Server Error: {str(e)}"
|
|
}), 500
|