the-tip-top-backend/scripts/apply-email-campaigns-migration.js
2025-11-17 23:47:54 +01:00

114 lines
4.0 KiB
JavaScript

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: `
<h1>Bienvenue {{firstName}}!</h1>
<p>Merci de vous être inscrit au jeu-concours Thé Tip Top.</p>
<p>Bonne chance pour gagner de magnifiques lots!</p>
`,
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: `
<h1>Félicitations {{firstName}}!</h1>
<p>Vous avez gagné: <strong>{{prizeName}}</strong></p>
<p>Rendez-vous en boutique pour récupérer votre lot.</p>
`,
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: `
<h1>Nouvelle promotion!</h1>
<p>Bonjour {{firstName}},</p>
<p>Ne ratez pas notre promotion exceptionnelle!</p>
<p><a href="https://thetiptop.fr">Découvrir</a></p>
`,
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();