""" Controller for repayment endpoints. """ from flask import Blueprint, request, jsonify from api.middleware import basic_auth_required from api.models import RepaymentRequest, RepaymentResponse import logging # Configure logger logger = logging.getLogger(__name__) # Create blueprint repayment_bp = Blueprint('repayment', __name__) @repayment_bp.route('/Repayment', methods=['POST']) @basic_auth_required def repayment(): """ Endpoint to process loan repayment requests. This method handles customer repayment of loans. Returns: JSON response with repayment 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 = ['$type', 'transactionId', 'customerId', 'debtId', 'productId', 'channel'] 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 = RepaymentRequest.from_dict(data) # Process repayment request (this would connect to the business logic) # For demonstration, we'll return a mock response # Create response response = RepaymentResponse( customerId=req.customerId, productId=req.productId, debtId=req.debtId, resultCode="00", resultDescription="Repayment processed successfully" ) logger.info(f"Processed repayment for customer {req.customerId}, debt {req.debtId}") return jsonify(response.to_dict()) except Exception as e: logger.error(f"Error processing repayment: {str(e)}") return jsonify({ 'resultCode': '500', 'resultDescription': 'Internal server error' }), 500