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: `
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: `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: `Bonjour {{firstName}},
Ne ratez pas notre promotion exceptionnelle!
`, 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();