""" Controller for customer consent endpoints. """ from flask import Blueprint, request, jsonify from api.middleware import basic_auth_required, api_key_required from api.models import ( CustomerConsentRequest, CustomerConsentResponse, RevokeEnableConsentRequest, RevokeEnableConsentResponse ) import logging # Configure logger logger = logging.getLogger(__name__) # Create blueprint consent_bp = Blueprint('consent', __name__) @consent_bp.route('/CustomerConsent', methods=['POST']) @basic_auth_required def customer_consent(): """ Endpoint to process customer consent requests. This method handles customer consent for loan services. Returns: JSON response with consent 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', 'accountId', 'requestTime', 'consentType', '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 = CustomerConsentRequest.from_dict(data) # Process consent request (this would connect to the business logic) # For demonstration, we'll return a mock response # Create response response = CustomerConsentResponse( resultCode="00", resultDescription="Request is received" ) logger.info(f"Processed consent request for customer {req.customerId}, type {req.consentType}") return jsonify(response.to_dict()) except Exception as e: logger.error(f"Error processing consent request: {str(e)}") return jsonify({ 'resultCode': '500', 'resultDescription': 'Internal server error' }), 500 @consent_bp.route('/RevokeEnableConsent', methods=['POST']) @api_key_required def revoke_enable_consent(): """ Endpoint to process consent revocation or enablement. This method handles requests from Simbrella to revoke or enable customer consent. Returns: JSON response with operation 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', 'customerId', 'accountId', 'processTime', 'consentType', '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 = RevokeEnableConsentRequest.from_dict(data) # Process revoke/enable consent request (this would connect to the business logic) # For demonstration, we'll return a mock response # Create response response = RevokeEnableConsentResponse( type="RevokeEnableConsentResponse", customerId=req.customerId, accountId=req.accountId, resultCode="00", resultDescription="Success" ) logger.info(f"Processed revoke/enable consent for customer {req.customerId}, type {req.consentType}") return jsonify(response.to_dict()) except Exception as e: logger.error(f"Error processing revoke/enable consent: {str(e)}") return jsonify({ 'resultCode': '500', 'resultDescription': 'Internal server error' }), 500