Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/database/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/database/proofreading_schema.sql

-- Proofreading System Extensions for COP News Portal
-- Add to existing schema.sql or run as migration

-- 1. Extend users table account_type enum to include 'editor'
ALTER TABLE users MODIFY COLUMN account_type ENUM('superuser', 'admin', 'editor', 'user') DEFAULT 'user';

-- 2. Add editorial permissions field to users table
ALTER TABLE users ADD COLUMN can_edit_others BOOLEAN DEFAULT FALSE AFTER status;
ALTER TABLE users ADD COLUMN editorial_scope ENUM('assembly', 'district', 'area', 'global') NULL AFTER can_edit_others;

-- 3. Extend news table status enum to include review statuses
ALTER TABLE news MODIFY COLUMN status ENUM('draft', 'pending_review', 'approved', 'rejected', 'published', 'archived') DEFAULT 'draft';

-- 4. Add editorial fields to news table
ALTER TABLE news ADD COLUMN assigned_editor_id INT NULL AFTER user_id;
ALTER TABLE news ADD COLUMN submitted_for_review_at TIMESTAMP NULL AFTER created_at;
ALTER TABLE news ADD COLUMN reviewed_at TIMESTAMP NULL AFTER submitted_for_review_at;
ALTER TABLE news ADD COLUMN review_comments TEXT NULL AFTER reviewed_at;

-- 5. Add foreign key constraint for assigned editor
ALTER TABLE news ADD FOREIGN KEY (assigned_editor_id) REFERENCES users(id) ON DELETE SET NULL;

-- 6. Create news_reviews table for review history and comments
CREATE TABLE news_reviews (
    id INT PRIMARY KEY AUTO_INCREMENT,
    news_id INT NOT NULL,
    reviewer_id INT NOT NULL,
    previous_status ENUM('draft', 'pending_review', 'approved', 'rejected', 'published', 'archived') NOT NULL,
    new_status ENUM('draft', 'pending_review', 'approved', 'rejected', 'published', 'archived') NOT NULL,
    review_comments TEXT,
    internal_notes TEXT,
    reviewed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (news_id) REFERENCES news(id) ON DELETE CASCADE,
    FOREIGN KEY (reviewer_id) REFERENCES users(id) ON DELETE CASCADE,
    INDEX idx_news_reviews_news_id (news_id),
    INDEX idx_news_reviews_reviewer (reviewer_id),
    INDEX idx_news_reviews_status (new_status)
);

-- 7. Create editor_assignments table for location-based assignments
CREATE TABLE editor_assignments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    editor_id INT NOT NULL,
    location_id INT NOT NULL,
    assignment_type ENUM('primary', 'secondary', 'backup') DEFAULT 'primary',
    is_active BOOLEAN DEFAULT TRUE,
    assigned_by INT NOT NULL,
    assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (editor_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE CASCADE,
    FOREIGN KEY (assigned_by) REFERENCES users(id) ON DELETE SET NULL,
    UNIQUE KEY unique_primary_assignment (location_id, assignment_type),
    INDEX idx_editor_assignments_editor (editor_id),
    INDEX idx_editor_assignments_location (location_id)
);

-- 8. Create review_notifications table for email notifications
CREATE TABLE review_notifications (
    id INT PRIMARY KEY AUTO_INCREMENT,
    news_id INT NOT NULL,
    recipient_id INT NOT NULL,
    notification_type ENUM('review_requested', 'review_completed', 'article_approved', 'article_rejected') NOT NULL,
    is_read BOOLEAN DEFAULT FALSE,
    sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (news_id) REFERENCES news(id) ON DELETE CASCADE,
    FOREIGN KEY (recipient_id) REFERENCES users(id) ON DELETE CASCADE,
    INDEX idx_review_notifications_recipient (recipient_id),
    INDEX idx_review_notifications_unread (is_read)
);

-- 9. Insert sample editor users (update with real data)
INSERT INTO users (name, email, username, telephone, password, account_type, location_id, can_edit_others, editorial_scope, status) VALUES
('District Editor - Tema', 'tema.editor@cop.org', 'tema_editor', '+233244567890', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'editor', 4, TRUE, 'district', 'active'),
('Area Editor - Greater Accra', 'accra.editor@cop.org', 'accra_editor', '+233244567891', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'editor', 1, TRUE, 'area', 'active');

-- 10. Insert sample editor assignments
INSERT INTO editor_assignments (editor_id, location_id, assignment_type, assigned_by) VALUES
((SELECT id FROM users WHERE username = 'tema_editor'), 4, 'primary', 1),  -- Tema District Editor
((SELECT id FROM users WHERE username = 'accra_editor'), 1, 'primary', 1); -- Greater Accra Area Editor

-- 11. Update settings for editorial workflow
INSERT INTO settings (setting_key, setting_value, setting_type, description) VALUES
('auto_assign_editors', 'true', 'boolean', 'Automatically assign editors based on location hierarchy'),
('require_review_for_publication', 'true', 'boolean', 'Require editorial review before publication'),
('email_notifications_enabled', 'true', 'boolean', 'Send email notifications for review status changes'),
('review_reminder_days', '3', 'number', 'Days before sending review reminder emails');

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