Files
Azeez Muibi ba4d878daf Major update
2025-03-27 08:21:20 +01:00

78 lines
2.7 KiB
Python

from flask import request, jsonify
from marshmallow import ValidationError
from app.utils.logger import logger
from app.api.schemas.bulk_sms import BulkSMSSchema, BulkSMSResponseSchema
class BulkSMSService:
@staticmethod
def process_request(data):
"""
Process the BulkSMS request.
Args:
data (list): The request data as a list of SMS items.
Returns:
dict: A standardized response.
"""
try:
logger.info("Processing BulkSMS request")
# Check if the data is a list and not too large
if not isinstance(data, list):
return jsonify({
"data": "",
"statusCode": 400,
"IsSuccessful": False,
"errorMessage": "Request must be a list of SMS items"
}), 400
if len(data) > 20:
return jsonify({
"data": "",
"statusCode": 400,
"IsSuccessful": False,
"errorMessage": "Maximum of 20 SMS items allowed"
}), 400
# Validate each item in the list
schema = BulkSMSSchema()
# We need to wrap the list in a dictionary with a key that matches the schema field name
validated_data = schema.load({"_schema": data}) # Raises ValidationError if invalid
# Simulated processing logic
# In a real implementation, this would interact with your business logic
# to send bulk SMS messages to customers
# For demonstration, we'll simulate a successful bulk SMS send
response_data = {
"data": "",
"statusCode": 200,
"IsSuccessful": True,
"errorMessage": None
}
# Validate the response using the response schema
response_schema = BulkSMSResponseSchema()
validated_response = response_schema.dump(response_data)
return jsonify(validated_response)
except ValidationError as err:
logger.error(f"Validation Error: {err.messages}")
return jsonify({
"data": "",
"statusCode": 400,
"IsSuccessful": False,
"errorMessage": f"Validation error: {err.messages}"
}), 400
except Exception as e:
logger.error(f"An error occurred: {str(e)}", exc_info=True)
return jsonify({
"data": "",
"statusCode": 500,
"IsSuccessful": False,
"errorMessage": f"Error occurred: {str(e)}"
}), 500