Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/sql/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/sql/sms_management.sql

-- SMS Management System Tables

-- SMS Gateway Settings
CREATE TABLE IF NOT EXISTS sms_settings (
    id INT PRIMARY KEY AUTO_INCREMENT,
    gateway_provider ENUM('africas_talking', 'twilio', 'bulksms_nigeria', 'hubtel', 'clickatell', 'smsportal', 'arkesel', 'custom') NOT NULL DEFAULT 'africas_talking',
    is_enabled TINYINT(1) DEFAULT 0,
    
    -- Africa's Talking Settings
    at_api_key VARCHAR(255) DEFAULT NULL,
    at_username VARCHAR(255) DEFAULT NULL,
    at_sender_id VARCHAR(20) DEFAULT NULL,
    
    -- Twilio Settings
    twilio_account_sid VARCHAR(255) DEFAULT NULL,
    twilio_auth_token VARCHAR(255) DEFAULT NULL,
    twilio_from_number VARCHAR(20) DEFAULT NULL,
    
    -- BulkSMS Nigeria Settings
    bulksms_api_token VARCHAR(255) DEFAULT NULL,
    bulksms_sender_id VARCHAR(20) DEFAULT NULL,
    
    -- Hubtel Settings
    hubtel_client_id VARCHAR(255) DEFAULT NULL,
    hubtel_client_secret VARCHAR(255) DEFAULT NULL,
    hubtel_sender_id VARCHAR(20) DEFAULT NULL,
    
    -- Clickatell Settings
    clickatell_api_key VARCHAR(255) DEFAULT NULL,
    
    -- SMS Portal Settings (South Africa)
    smsportal_client_id VARCHAR(255) DEFAULT NULL,
    smsportal_client_secret VARCHAR(255) DEFAULT NULL,
    
    -- Arkesel Settings (Ghana)
    arkesel_api_key VARCHAR(255) DEFAULT NULL,
    arkesel_sender_id VARCHAR(20) DEFAULT NULL,
    
    -- Custom Gateway Settings
    custom_api_url VARCHAR(500) DEFAULT NULL,
    custom_api_key VARCHAR(255) DEFAULT NULL,
    custom_method ENUM('GET', 'POST') DEFAULT 'POST',
    custom_headers TEXT DEFAULT NULL,
    
    -- General Settings
    default_sender_name VARCHAR(20) DEFAULT NULL,
    test_mode TINYINT(1) DEFAULT 1,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- SMS Queue (for sending SMS)
CREATE TABLE IF NOT EXISTS sms_queue (
    id INT PRIMARY KEY AUTO_INCREMENT,
    recipient_phone VARCHAR(20) NOT NULL,
    recipient_name VARCHAR(255) DEFAULT NULL,
    message TEXT NOT NULL,
    sender_id VARCHAR(20) DEFAULT NULL,
    
    status ENUM('pending', 'sent', 'failed', 'cancelled') DEFAULT 'pending',
    gateway_used VARCHAR(50) DEFAULT NULL,
    gateway_response TEXT DEFAULT NULL,
    gateway_message_id VARCHAR(255) DEFAULT NULL,
    
    attempts INT DEFAULT 0,
    max_attempts INT DEFAULT 3,
    
    scheduled_at DATETIME DEFAULT NULL,
    sent_at DATETIME DEFAULT NULL,
    failed_at DATETIME DEFAULT NULL,
    
    error_message TEXT DEFAULT NULL,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    INDEX idx_status (status),
    INDEX idx_scheduled (scheduled_at),
    INDEX idx_phone (recipient_phone)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- SMS Logs (for tracking all SMS)
CREATE TABLE IF NOT EXISTS sms_logs (
    id INT PRIMARY KEY AUTO_INCREMENT,
    queue_id INT DEFAULT NULL,
    
    recipient_phone VARCHAR(20) NOT NULL,
    recipient_name VARCHAR(255) DEFAULT NULL,
    message TEXT NOT NULL,
    sender_id VARCHAR(20) DEFAULT NULL,
    
    gateway_provider VARCHAR(50) NOT NULL,
    status ENUM('sent', 'failed', 'delivered', 'undelivered') NOT NULL,
    
    gateway_response TEXT DEFAULT NULL,
    gateway_message_id VARCHAR(255) DEFAULT NULL,
    
    cost DECIMAL(10, 4) DEFAULT NULL,
    currency VARCHAR(3) DEFAULT 'USD',
    
    delivery_status VARCHAR(50) DEFAULT NULL,
    delivery_time DATETIME DEFAULT NULL,
    
    error_message TEXT DEFAULT NULL,
    
    sent_at DATETIME NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    INDEX idx_status (status),
    INDEX idx_phone (recipient_phone),
    INDEX idx_sent_at (sent_at),
    INDEX idx_gateway (gateway_provider),
    FOREIGN KEY (queue_id) REFERENCES sms_queue(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- SMS Templates
CREATE TABLE IF NOT EXISTS sms_templates (
    id INT PRIMARY KEY AUTO_INCREMENT,
    template_name VARCHAR(100) NOT NULL,
    template_code VARCHAR(50) NOT NULL UNIQUE,
    message_template TEXT NOT NULL,
    description TEXT DEFAULT NULL,
    variables TEXT DEFAULT NULL COMMENT 'JSON array of available variables',
    
    is_active TINYINT(1) DEFAULT 1,
    
    created_by INT DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Insert default SMS templates
INSERT INTO sms_templates (template_name, template_code, message_template, description, variables) VALUES
('Welcome Message', 'welcome', 'Welcome to {church_name}, {name}! Your membership card number is {card_number}. God bless you!', 'Sent when a new member is registered', '["name", "church_name", "card_number"]'),
('Event Reminder', 'event_reminder', 'Hi {name}, reminder: {event_name} on {event_date} at {event_time}. Location: {event_location}', 'Event reminder notification', '["name", "event_name", "event_date", "event_time", "event_location"]'),
('Password Reset', 'password_reset', 'Your password reset code is: {code}. Valid for 15 minutes. If you did not request this, please ignore.', 'Password reset OTP', '["code"]'),
('2FA Code', '2fa_code', 'Your verification code is: {code}. Valid for 10 minutes. Do not share this code.', 'Two-factor authentication code', '["code"]'),
('Custom Message', 'custom', '', 'Custom message template', '[]');

-- Insert default SMS settings
INSERT INTO sms_settings (gateway_provider, is_enabled, test_mode) VALUES ('africas_talking', 0, 1);

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists