132 lines
4.0 KiB
Python
132 lines
4.0 KiB
Python
"""
|
|
Controller for SMS notification endpoints.
|
|
"""
|
|
from flask import Blueprint, request, jsonify
|
|
from app.middleware import api_key_required
|
|
from app.models import SMSRequest, SMSResponse
|
|
import logging
|
|
|
|
# Configure logger
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create blueprint
|
|
sms_bp = Blueprint('sms', __name__)
|
|
|
|
@sms_bp.route('/SMS', methods=['POST'])
|
|
@api_key_required
|
|
def send_sms():
|
|
"""
|
|
Endpoint to send SMS notifications.
|
|
|
|
This method handles requests to send SMS messages to customers.
|
|
|
|
Returns:
|
|
JSON response with SMS sending 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 = ['text', 'dest', 'unicode']
|
|
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 = SMSRequest.from_dict(data)
|
|
|
|
# Process SMS request (this would connect to your business logic)
|
|
# For demonstration, we'll return a mock response
|
|
|
|
# Create response
|
|
response = SMSResponse(
|
|
data="",
|
|
statusCode=200,
|
|
IsSuccessful=True,
|
|
errorMessage=None
|
|
)
|
|
|
|
logger.info(f"Processed SMS request to {req.dest}")
|
|
return jsonify(response.to_dict())
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error processing SMS request: {str(e)}")
|
|
return jsonify({
|
|
'resultCode': '500',
|
|
'resultDescription': 'Internal server error'
|
|
}), 500
|
|
|
|
@sms_bp.route('/BulkSMS', methods=['POST'])
|
|
@api_key_required
|
|
def send_bulk_sms():
|
|
"""
|
|
Endpoint to send bulk SMS notifications.
|
|
|
|
This method handles requests to send multiple SMS messages (up to 20)
|
|
in a single request.
|
|
|
|
Returns:
|
|
JSON response with bulk SMS sending status
|
|
"""
|
|
try:
|
|
# Parse and validate request
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({
|
|
'resultCode': '400',
|
|
'resultDescription': 'Invalid JSON payload'
|
|
}), 400
|
|
|
|
# Validate that data is an array
|
|
if not isinstance(data, list):
|
|
return jsonify({
|
|
'resultCode': '422',
|
|
'resultDescription': 'Request must be an array of SMS messages'
|
|
}), 422
|
|
|
|
# Validate array length
|
|
if len(data) > 20:
|
|
return jsonify({
|
|
'resultCode': '422',
|
|
'resultDescription': 'Maximum of 20 SMS messages allowed per request'
|
|
}), 422
|
|
|
|
# Validate each SMS in the array
|
|
for i, sms in enumerate(data):
|
|
required_fields = ['text', 'dest', 'unicode']
|
|
for field in required_fields:
|
|
if field not in sms:
|
|
return jsonify({
|
|
'resultCode': '422',
|
|
'resultDescription': f'Missing required field: {field} in SMS at index {i}'
|
|
}), 422
|
|
|
|
# Process bulk SMS request (this would connect to the business logic)
|
|
# For demonstration, we'll return a mock response
|
|
|
|
# Create response
|
|
response = SMSResponse(
|
|
data="",
|
|
statusCode=200,
|
|
IsSuccessful=True,
|
|
errorMessage=None
|
|
)
|
|
|
|
logger.info(f"Processed bulk SMS request with {len(data)} messages")
|
|
return jsonify(response.to_dict())
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error processing bulk SMS request: {str(e)}")
|
|
return jsonify({
|
|
'resultCode': '500',
|
|
'resultDescription': 'Internal server error'
|
|
}), 500 |