81 lines
2.9 KiB
Python
81 lines
2.9 KiB
Python
"""
|
|
Controller for loan collection endpoints.
|
|
"""
|
|
from flask import Blueprint, request, jsonify
|
|
from api.middleware import api_key_required
|
|
from api.models import CollectLoanRequest, CollectLoanResponse
|
|
import logging
|
|
|
|
# Configure logger
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create blueprint
|
|
collection_bp = Blueprint('collection', __name__)
|
|
|
|
@collection_bp.route('/CollectLoan', methods=['POST'])
|
|
@api_key_required
|
|
def collect_loan():
|
|
"""
|
|
Endpoint to process loan collection requests from Simbrella.
|
|
|
|
This method handles requests to collect money from user accounts.
|
|
When a request is received, FirstBank should check all user accounts
|
|
and collect as much money as possible to cover the existing loan
|
|
either partially or fully.
|
|
|
|
Returns:
|
|
JSON response with collection status
|
|
"""
|
|
try:
|
|
# Parse and validate request
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({
|
|
'resultCode': '400',
|
|
'resultDescription': 'Invalid JSON payload'
|
|
}), 400
|
|
|
|
# Validate required fields
|
|
required_fields = ['transactionId', 'fbnTransactionId', 'debtId', 'customerId',
|
|
'accountId', 'productId', 'collectAmount', 'collectionMethod',
|
|
'lienAmount', 'countryId']
|
|
for field in required_fields:
|
|
if field not in data:
|
|
return jsonify({
|
|
'resultCode': '422',
|
|
'resultDescription': f'Missing required field: {field}'
|
|
}), 422
|
|
|
|
# Create request model
|
|
req = CollectLoanRequest.from_dict(data)
|
|
|
|
# Process collection request (this would connect to the business logic)
|
|
# For demonstration, we'll return a mock response with partial collection
|
|
|
|
# Assume we collected 75% of the requested amount
|
|
collected_amount = req.collectAmount * 0.75
|
|
remaining_lien = req.lienAmount - collected_amount
|
|
|
|
# Create response
|
|
response = CollectLoanResponse(
|
|
transactionId=req.transactionId,
|
|
debtId=req.debtId,
|
|
customerId=req.customerId,
|
|
accountId=req.accountId,
|
|
productId=req.productId,
|
|
collectAmount=collected_amount,
|
|
lienAmount=remaining_lien,
|
|
resultCode="00",
|
|
resultDescription="Loan Collection Successful",
|
|
penalCharge=req.penalCharge if hasattr(req, 'penalCharge') else 0.0
|
|
)
|
|
|
|
logger.info(f"Processed collection for customer {req.customerId}, collected {collected_amount}")
|
|
return jsonify(response.to_dict())
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error processing collection: {str(e)}")
|
|
return jsonify({
|
|
'resultCode': '500',
|
|
'resultDescription': 'Internal server error'
|
|
}), 500 |