""" Controller for offer selection endpoints. """ from flask import Blueprint, request, jsonify from app.middleware import basic_auth_required from app.models import SelectOffersRequest, SelectOffersResponse, Offer import logging # Configure logger logger = logging.getLogger(__name__) # Create blueprint offers_bp = Blueprint('offers', __name__) @offers_bp.route('/SelectOffer', methods=['POST']) @basic_auth_required def select_offer(): """ Endpoint to send the offer the customer selected to Simbrella. This method processes the customer's selected offer and returns detailed offer information. Returns: JSON response with detailed offer information """ 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 = ['requestId', 'transactionId', 'customerId', 'accountId', 'msisdn', 'requestedAmount', '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 = SelectOffersRequest.from_dict(data) # Process offer selection (this would connect to the business logic) # For demonstration, we'll return a mock response # Create sample offers offers = [ Offer( offerId="14451", productId=req.productid, amount=req.requestedAmount, upfrontPayment=req.requestedAmount * 0.1, # 10% upfront interestRate=3.0, Interest=req.requestedAmount * 0.03, # 3% interest ManagementRate=1.0, ManagementFee=req.requestedAmount * 0.01, # 1% management fee InsuranceRate=1.0, InsuranceFee=req.requestedAmount * 0.01, # 1% insurance VATRate=7.5, VATamount=(req.requestedAmount * 0.01) * 0.075, # VAT on management fee recommendedRepaymentDates=["2023-04-30", "2023-05-30", "2023-06-29"], installmentAmount=req.requestedAmount * 1.05 / 3, # Split into 3 payments with 5% total fees totalRepaymentAmount=req.requestedAmount * 1.05 # Total with 5% fees ) ] # Create response response = SelectOffersResponse( requestId=req.requestId, transactionId=req.transactionId, customerId=req.customerId, accountId=req.accountId, offers=[offer.to_dict() for offer in offers], resultCode="00", resultDescription="Successful", outstandingDebtAmount=0.0 ) logger.info(f"Processed offer selection for customer {req.customerId}") return jsonify(response.to_dict()) except Exception as e: logger.error(f"Error processing offer selection: {str(e)}") return jsonify({ 'resultCode': '500', 'resultDescription': 'Internal server error' }), 500