import 'dotenv/config'; import { pool } from '../db.js'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function applyMigration() { console.log('🔧 Application de la migration: add-email-campaigns\n'); try { // Lire le fichier SQL const migrationPath = path.join(__dirname, '../database/migrations/add-email-campaigns.sql'); const migrationSQL = fs.readFileSync(migrationPath, 'utf8'); // VĂ©rifier l'Ă©tat actuel console.log('📋 VĂ©rification de l\'Ă©tat actuel de la base de donnĂ©es...\n'); const tablesCheck = await pool.query(` SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('email_campaigns', 'email_campaign_recipients', 'email_templates') `); console.log('📊 État actuel:'); console.log(` ‱ Tables existantes: ${tablesCheck.rows.map(r => r.table_name).join(', ') || 'Aucune'}`); // Appliquer la migration console.log('\n🚀 Application de la migration...\n'); await pool.query(migrationSQL); // VĂ©rifier le nouvel Ă©tat const newTablesCheck = await pool.query(` SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('email_campaigns', 'email_campaign_recipients', 'email_templates') ORDER BY table_name `); console.log('✅ Migration appliquĂ©e avec succĂšs!\n'); console.log('📊 Nouvel Ă©tat:'); newTablesCheck.rows.forEach(table => { console.log(` ‱ ${table.table_name}: ✅ Créée`); }); // Ajouter quelques templates par dĂ©faut console.log('\n📧 CrĂ©ation de templates par dĂ©faut...\n'); const templates = [ { name: 'Bienvenue', description: 'Email de bienvenue pour les nouveaux inscrits', subject: 'Bienvenue chez ThĂ© Tip Top!', html: `

Bienvenue {{firstName}}!

Merci de vous ĂȘtre inscrit au jeu-concours ThĂ© Tip Top.

Bonne chance pour gagner de magnifiques lots!

`, text: 'Bienvenue {{firstName}}! Merci de vous ĂȘtre inscrit au jeu-concours ThĂ© Tip Top. Bonne chance!', category: 'welcome', }, { name: 'Gagnant notifiĂ©', description: 'Notification de gain', subject: 'FĂ©licitations, vous avez gagnĂ©!', html: `

Félicitations {{firstName}}!

Vous avez gagné: {{prizeName}}

Rendez-vous en boutique pour récupérer votre lot.

`, text: 'Félicitations {{firstName}}! Vous avez gagné: {{prizeName}}. Rendez-vous en boutique pour récupérer votre lot.', category: 'notification', }, { name: 'Promotion', description: 'Email promotionnel générique', subject: 'Nouvelle promotion chez Thé Tip Top!', html: `

Nouvelle promotion!

Bonjour {{firstName}},

Ne ratez pas notre promotion exceptionnelle!

Découvrir

`, text: 'Nouvelle promotion! Bonjour {{firstName}}, Ne ratez pas notre promotion exceptionnelle sur https://thetiptop.fr', category: 'promotion', }, ]; for (const template of templates) { await pool.query( `INSERT INTO email_templates (name, description, subject, html_content, text_content, category, is_active) VALUES ($1, $2, $3, $4, $5, $6, TRUE)`, [template.name, template.description, template.subject, template.html, template.text, template.category] ); console.log(` ✓ Template "${template.name}" créé`); } console.log('\n✹ Migration et templates créés!'); } catch (error) { console.error('❌ Erreur lors de l\'application de la migration:', error); throw error; } finally { await pool.end(); } } applyMigration();