105 lines
3.9 KiB
Python
105 lines
3.9 KiB
Python
import logging
|
|
from datetime import datetime
|
|
from flask import jsonify
|
|
from app.models.repayment import Repayment
|
|
|
|
# Configure logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class RepaymentService:
|
|
"""
|
|
Service class for handling repayment-related operations.
|
|
"""
|
|
|
|
@staticmethod
|
|
def get_all_repayments(filters=None):
|
|
"""
|
|
Get all repayments with optional filtering.
|
|
|
|
Args:
|
|
filters (dict, optional): Filters for the repayments query.
|
|
|
|
Returns:
|
|
dict: A standardized response with repayments data.
|
|
"""
|
|
try:
|
|
if filters is None:
|
|
filters = {}
|
|
|
|
# Extract filters
|
|
loan_id = filters.get('loan_id')
|
|
customer_id = filters.get('customer_id')
|
|
product_id = filters.get('product_id')
|
|
start_date = filters.get('start_date')
|
|
end_date = filters.get('end_date')
|
|
|
|
# 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'))
|
|
|
|
# Get repayments with optional filters and pagination
|
|
repayments, total_count = Repayment.get_all_repayments(
|
|
loan_id=loan_id,
|
|
customer_id=customer_id,
|
|
product_id=product_id,
|
|
start_date=start_date,
|
|
end_date=end_date,
|
|
page=page,
|
|
limit=limit
|
|
)
|
|
|
|
# Convert repayments to dictionary format
|
|
repayments_data = []
|
|
for repayment in repayments:
|
|
repayments_data.append({
|
|
'loan_id': repayment.loan_id,
|
|
'customer_id': repayment.customer_id,
|
|
'product_id': repayment.product_id,
|
|
'transaction_id': repayment.transaction_id,
|
|
'created_at': repayment.created_at.isoformat() if repayment.created_at else None,
|
|
'updated_at': repayment.updated_at.isoformat() if repayment.updated_at else None,
|
|
'repay_date': repayment.repay_date.isoformat() if repayment.repay_date else None,
|
|
'initiated_by': repayment.initiated_by,
|
|
'salary_amount': repayment.salary_amount,
|
|
'verify_date': repayment.verify_date.isoformat() if repayment.verify_date else None,
|
|
'repay_result': repayment.repay_result,
|
|
'repay_description': repayment.repay_description,
|
|
'verify_result': repayment.verify_result,
|
|
'verify_description': repayment.verify_description
|
|
})
|
|
|
|
# Calculate total pages
|
|
total_pages = (total_count + limit - 1) // limit
|
|
|
|
response_data = {
|
|
'repayments': repayments_data,
|
|
'count': len(repayments_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 |