import pkg from 'pg'; const { Pool } = pkg; const preprodPool = new Pool({ host: '51.75.24.29', port: 5434, user: 'postgres', password: 'postgres', database: 'thetiptop_preprod' }); console.log('=== CORRECTION DU SCHÉMA PREPROD ===\n'); // 1. TICKETS - ajouter colonnes de livraison console.log('📦 Correction de TICKETS...'); await preprodPool.query(` ALTER TABLE tickets ADD COLUMN IF NOT EXISTS delivered_at TIMESTAMP, ADD COLUMN IF NOT EXISTS delivered_by UUID REFERENCES users(id), ADD COLUMN IF NOT EXISTS delivery_notes TEXT `); console.log('✅ tickets: delivered_at, delivered_by, delivery_notes ajoutées\n'); // 2. NEWSLETTERS - corriger is_active console.log('📦 Correction de NEWSLETTERS...'); await preprodPool.query(`ALTER TABLE newsletters RENAME COLUMN is_subscribed TO is_active`); console.log('✅ newsletters: is_subscribed renommée en is_active\n'); // 3. EMAIL_TEMPLATES - recréer avec bonnes colonnes console.log('📦 Correction de EMAIL_TEMPLATES...'); await preprodPool.query(`DROP TABLE IF EXISTS email_templates CASCADE`); await preprodPool.query(` CREATE TABLE email_templates ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name VARCHAR(255) NOT NULL, description TEXT, subject VARCHAR(500), html_content TEXT NOT NULL, text_content TEXT, category VARCHAR(100), is_active BOOLEAN DEFAULT TRUE, created_by UUID REFERENCES users(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) `); console.log('✅ email_templates recréée avec toutes les colonnes\n'); // 4. EMAIL_CAMPAIGNS - recréer avec bonnes colonnes console.log('📦 Correction de EMAIL_CAMPAIGNS...'); await preprodPool.query(`DROP TABLE IF EXISTS email_campaign_recipients CASCADE`); await preprodPool.query(`DROP TABLE IF EXISTS email_campaigns CASCADE`); await preprodPool.query(` CREATE TABLE email_campaigns ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name VARCHAR(255) NOT NULL, subject VARCHAR(500) NOT NULL, template_html TEXT NOT NULL, template_text TEXT, created_by UUID NOT NULL REFERENCES users(id), status VARCHAR(50) DEFAULT 'DRAFT', scheduled_at TIMESTAMP, sent_at TIMESTAMP, recipient_count INTEGER DEFAULT 0, opened_count INTEGER DEFAULT 0, clicked_count INTEGER DEFAULT 0, criteria JSONB, notes TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) `); console.log('✅ email_campaigns recréée avec toutes les colonnes\n'); // 5. EMAIL_CAMPAIGN_RECIPIENTS - recréer avec bonnes colonnes console.log('📦 Correction de EMAIL_CAMPAIGN_RECIPIENTS...'); await preprodPool.query(` CREATE TABLE email_campaign_recipients ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), campaign_id UUID NOT NULL REFERENCES email_campaigns(id) ON DELETE CASCADE, user_id UUID NOT NULL REFERENCES users(id), email VARCHAR(255) NOT NULL, status VARCHAR(50) DEFAULT 'PENDING', sent_at TIMESTAMP, opened_at TIMESTAMP, clicked_at TIMESTAMP, unsubscribed_at TIMESTAMP, error_message TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) `); console.log('✅ email_campaign_recipients recréée avec toutes les colonnes\n'); // 6. GRAND_PRIZE_DRAWS - recréer avec bonnes colonnes console.log('📦 Correction de GRAND_PRIZE_DRAWS...'); await preprodPool.query(`DROP TABLE IF EXISTS grand_prize_draws CASCADE`); await preprodPool.query(` CREATE TABLE grand_prize_draws ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), draw_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, conducted_by UUID NOT NULL REFERENCES users(id), winner_id UUID NOT NULL REFERENCES users(id), winner_email VARCHAR(255) NOT NULL, winner_name VARCHAR(255) NOT NULL, prize_name VARCHAR(255) NOT NULL, prize_value VARCHAR(100), total_participants INTEGER NOT NULL, eligible_participants INTEGER NOT NULL, criteria JSONB, status VARCHAR(50) DEFAULT 'COMPLETED', notified_at TIMESTAMP, claimed_at TIMESTAMP, notes TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, CONSTRAINT unique_grand_prize_draw UNIQUE (draw_date) ) `); console.log('✅ grand_prize_draws recréée avec toutes les colonnes\n'); // Créer les index console.log('📦 Création des index...'); await preprodPool.query(` CREATE INDEX IF NOT EXISTS idx_email_campaigns_status ON email_campaigns(status); CREATE INDEX IF NOT EXISTS idx_email_campaigns_created_by ON email_campaigns(created_by); CREATE INDEX IF NOT EXISTS idx_email_campaigns_scheduled_at ON email_campaigns(scheduled_at); CREATE INDEX IF NOT EXISTS idx_email_campaign_recipients_campaign ON email_campaign_recipients(campaign_id); CREATE INDEX IF NOT EXISTS idx_email_campaign_recipients_user ON email_campaign_recipients(user_id); CREATE INDEX IF NOT EXISTS idx_email_campaign_recipients_status ON email_campaign_recipients(status); CREATE INDEX IF NOT EXISTS idx_email_templates_category ON email_templates(category); CREATE INDEX IF NOT EXISTS idx_email_templates_is_active ON email_templates(is_active); CREATE INDEX IF NOT EXISTS idx_grand_prize_draws_winner ON grand_prize_draws(winner_id); CREATE INDEX IF NOT EXISTS idx_grand_prize_draws_date ON grand_prize_draws(draw_date); CREATE INDEX IF NOT EXISTS idx_grand_prize_draws_status ON grand_prize_draws(status); `); console.log('✅ Index créés\n'); console.log('=== CORRECTION TERMINÉE ==='); await preprodPool.end(); process.exit(0);