from flask import session, jsonify from app.utils.logger import logger from app.api.services.base_service import BaseService from app.api.schemas.eligibility_check import EligibilityCheckSchema from marshmallow import ValidationError from app.api.enums import TransactionType from app.api.integrations import SimbrellaIntegration from app.extensions import db class EligibilityCheckService(BaseService): TRANSACTION_TYPE = TransactionType.ELIGIBILITY_CHECK @staticmethod def process_request(data): """ Process the EligibilityCheck request. Args: data (dict): The request data. Returns: dict: A standardized response. """ try: with db.session.begin(): validated_data = EligibilityCheckService.validate_data(data, EligibilityCheckSchema()) account_id = validated_data.get('accountId') customer_id = validated_data.get('customerId') transactionId = validated_data.get('transactionId') msisdn = validated_data.get('msisdn') customer = EligibilityCheckService.get_or_create_customer(validated_data = validated_data) if (EligibilityCheckService.validate_account_ownership(account_id = account_id, customer_id = customer_id)): transaction = EligibilityCheckService.log_transaction(validated_data = validated_data) if not transaction: logger.error(f"Failed to log transaction") return jsonify({ "message": "Failed to log transaction." }), 400 else: return jsonify({ "message": "Invalid Customer or Account" }), 400 # Call RACCheck response = SimbrellaIntegration.rac_check( customer_id = customer_id, account_id = account_id, transaction_id = transaction.id, ) logger.error(f"This is Response Returned ****** : {str(response)}") # this chck for error is not valid logger.error(f"Check for ERROR is not valid ****** FIX THIS !!!!!") #if "error" in response or response.get("status") != 200: # return jsonify({"message": "RACCheck failed"}), 400 offers = [ { "offerId": "SAL90", "productId": "2030", "minAmount": 5000, "maxAmount": 100000, "tenor": 30 }, { "offerId": "SAL30", "productId": "2090", "minAmount": 3000, "maxAmount": 500000, "tenor": 90 } ] # Simulate processing response_data = { "customerId": customer_id, "transactionId": transactionId, "countryCode": "NG", "msisdn": msisdn, "eligibleOffers": offers, "resultDescription": "Successful", "resultCode": "00", "accountId": account_id } return response_data except ValidationError as err: logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}") return jsonify({ "message": "Validation exception" }) , 422 except ValueError as err: logger.error(f"{getattr(err, 'messages', str(err))}") return jsonify({ "message": str(err) }) , 400 except Exception as e: logger.error(f"An error occurred: {str(e)}", exc_info=True) return jsonify({ "message": "Internal Server Error" }) , 500