322 lines
13 KiB
JavaScript
322 lines
13 KiB
JavaScript
/**
|
||
* Script de debug pour tester le flux complet d'envoi d'email
|
||
*
|
||
* Ce script teste:
|
||
* 1. Connexion SMTP
|
||
* 2. Envoi d'un email simple
|
||
* 3. Envoi avec le template complet
|
||
*/
|
||
|
||
require('dotenv').config();
|
||
const nodemailer = require('nodemailer');
|
||
|
||
console.log('\n🔍 DEBUG - Flux d\'envoi d\'email transporteur\n');
|
||
console.log('='.repeat(60));
|
||
|
||
// 1. Afficher la configuration
|
||
console.log('\n📋 CONFIGURATION ACTUELLE:');
|
||
console.log('----------------------------');
|
||
console.log('SMTP_HOST:', process.env.SMTP_HOST);
|
||
console.log('SMTP_PORT:', process.env.SMTP_PORT);
|
||
console.log('SMTP_SECURE:', process.env.SMTP_SECURE);
|
||
console.log('SMTP_USER:', process.env.SMTP_USER);
|
||
console.log('SMTP_PASS:', process.env.SMTP_PASS ? '***' + process.env.SMTP_PASS.slice(-4) : 'NON DÉFINI');
|
||
console.log('SMTP_FROM:', process.env.SMTP_FROM);
|
||
console.log('APP_URL:', process.env.APP_URL);
|
||
|
||
// 2. Vérifier les variables requises
|
||
console.log('\n✅ VÉRIFICATION DES VARIABLES:');
|
||
console.log('--------------------------------');
|
||
const requiredVars = ['SMTP_HOST', 'SMTP_PORT', 'SMTP_USER', 'SMTP_PASS'];
|
||
const missing = requiredVars.filter(v => !process.env[v]);
|
||
if (missing.length > 0) {
|
||
console.error('❌ Variables manquantes:', missing.join(', '));
|
||
process.exit(1);
|
||
} else {
|
||
console.log('✅ Toutes les variables requises sont présentes');
|
||
}
|
||
|
||
// 3. Créer le transporter avec la même configuration que le backend
|
||
console.log('\n🔧 CRÉATION DU TRANSPORTER:');
|
||
console.log('----------------------------');
|
||
|
||
const host = process.env.SMTP_HOST;
|
||
const port = parseInt(process.env.SMTP_PORT);
|
||
const user = process.env.SMTP_USER;
|
||
const pass = process.env.SMTP_PASS;
|
||
const secure = process.env.SMTP_SECURE === 'true';
|
||
|
||
// Même logique que dans email.adapter.ts
|
||
const useDirectIP = host.includes('mailtrap.io');
|
||
const actualHost = useDirectIP ? '3.209.246.195' : host;
|
||
const serverName = useDirectIP ? 'smtp.mailtrap.io' : host;
|
||
|
||
console.log('Configuration détectée:');
|
||
console.log(' Host original:', host);
|
||
console.log(' Utilise IP directe:', useDirectIP);
|
||
console.log(' Host réel:', actualHost);
|
||
console.log(' Server name (TLS):', serverName);
|
||
console.log(' Port:', port);
|
||
console.log(' Secure:', secure);
|
||
|
||
const transporter = nodemailer.createTransport({
|
||
host: actualHost,
|
||
port,
|
||
secure,
|
||
auth: {
|
||
user,
|
||
pass,
|
||
},
|
||
tls: {
|
||
rejectUnauthorized: false,
|
||
servername: serverName,
|
||
},
|
||
connectionTimeout: 10000,
|
||
greetingTimeout: 10000,
|
||
socketTimeout: 30000,
|
||
dnsTimeout: 10000,
|
||
});
|
||
|
||
// 4. Tester la connexion
|
||
console.log('\n🔌 TEST DE CONNEXION SMTP:');
|
||
console.log('---------------------------');
|
||
|
||
async function testConnection() {
|
||
try {
|
||
console.log('Vérification de la connexion...');
|
||
await transporter.verify();
|
||
console.log('✅ Connexion SMTP réussie!');
|
||
return true;
|
||
} catch (error) {
|
||
console.error('❌ Échec de la connexion SMTP:');
|
||
console.error(' Message:', error.message);
|
||
console.error(' Code:', error.code);
|
||
console.error(' Command:', error.command);
|
||
if (error.stack) {
|
||
console.error(' Stack:', error.stack.substring(0, 200) + '...');
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 5. Envoyer un email de test simple
|
||
async function sendSimpleEmail() {
|
||
console.log('\n📧 TEST 1: Email simple');
|
||
console.log('------------------------');
|
||
|
||
try {
|
||
const info = await transporter.sendMail({
|
||
from: process.env.SMTP_FROM || 'noreply@xpeditis.com',
|
||
to: 'test@example.com',
|
||
subject: 'Test Simple - ' + new Date().toISOString(),
|
||
text: 'Ceci est un test simple',
|
||
html: '<h1>Test Simple</h1><p>Ceci est un test simple</p>',
|
||
});
|
||
|
||
console.log('✅ Email simple envoyé avec succès!');
|
||
console.log(' Message ID:', info.messageId);
|
||
console.log(' Response:', info.response);
|
||
console.log(' Accepted:', info.accepted);
|
||
console.log(' Rejected:', info.rejected);
|
||
return true;
|
||
} catch (error) {
|
||
console.error('❌ Échec d\'envoi email simple:');
|
||
console.error(' Message:', error.message);
|
||
console.error(' Code:', error.code);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 6. Envoyer un email avec le template transporteur complet
|
||
async function sendCarrierEmail() {
|
||
console.log('\n📧 TEST 2: Email transporteur avec template');
|
||
console.log('--------------------------------------------');
|
||
|
||
const bookingData = {
|
||
bookingId: 'TEST-' + Date.now(),
|
||
origin: 'FRPAR',
|
||
destination: 'USNYC',
|
||
volumeCBM: 15.5,
|
||
weightKG: 1200,
|
||
palletCount: 6,
|
||
priceUSD: 2500,
|
||
priceEUR: 2250,
|
||
primaryCurrency: 'USD',
|
||
transitDays: 18,
|
||
containerType: '40FT',
|
||
documents: [
|
||
{ type: 'Bill of Lading', fileName: 'bol-test.pdf' },
|
||
{ type: 'Packing List', fileName: 'packing-test.pdf' },
|
||
{ type: 'Commercial Invoice', fileName: 'invoice-test.pdf' },
|
||
],
|
||
};
|
||
|
||
const baseUrl = process.env.APP_URL || 'http://localhost:3000';
|
||
const acceptUrl = `${baseUrl}/api/v1/csv-bookings/${bookingData.bookingId}/accept`;
|
||
const rejectUrl = `${baseUrl}/api/v1/csv-bookings/${bookingData.bookingId}/reject`;
|
||
|
||
// Template HTML (version simplifiée pour le test)
|
||
const htmlTemplate = `
|
||
<!DOCTYPE html>
|
||
<html lang="fr">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Nouvelle demande de réservation</title>
|
||
</head>
|
||
<body style="margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f4f6f8;">
|
||
<div style="max-width: 600px; margin: 20px auto; background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);">
|
||
<div style="background: linear-gradient(135deg, #045a8d, #00bcd4); color: #ffffff; padding: 30px 20px; text-align: center;">
|
||
<h1 style="margin: 0; font-size: 28px;">🚢 Nouvelle demande de réservation</h1>
|
||
<p style="margin: 5px 0 0; font-size: 14px;">Xpeditis</p>
|
||
</div>
|
||
<div style="padding: 30px 20px;">
|
||
<p style="font-size: 16px;">Bonjour,</p>
|
||
<p>Vous avez reçu une nouvelle demande de réservation via Xpeditis.</p>
|
||
|
||
<h2 style="color: #045a8d; border-bottom: 2px solid #00bcd4; padding-bottom: 8px;">📋 Détails du transport</h2>
|
||
<table style="width: 100%; border-collapse: collapse;">
|
||
<tr style="border-bottom: 1px solid #e0e0e0;">
|
||
<td style="padding: 12px; font-weight: bold; color: #045a8d;">Route</td>
|
||
<td style="padding: 12px;">${bookingData.origin} → ${bookingData.destination}</td>
|
||
</tr>
|
||
<tr style="border-bottom: 1px solid #e0e0e0;">
|
||
<td style="padding: 12px; font-weight: bold; color: #045a8d;">Volume</td>
|
||
<td style="padding: 12px;">${bookingData.volumeCBM} CBM</td>
|
||
</tr>
|
||
<tr style="border-bottom: 1px solid #e0e0e0;">
|
||
<td style="padding: 12px; font-weight: bold; color: #045a8d;">Poids</td>
|
||
<td style="padding: 12px;">${bookingData.weightKG} kg</td>
|
||
</tr>
|
||
<tr style="border-bottom: 1px solid #e0e0e0;">
|
||
<td style="padding: 12px; font-weight: bold; color: #045a8d;">Prix</td>
|
||
<td style="padding: 12px; font-size: 24px; font-weight: bold; color: #00aa00;">
|
||
${bookingData.priceUSD} USD
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
|
||
<div style="background-color: #f9f9f9; padding: 20px; border-radius: 6px; margin: 20px 0;">
|
||
<h3 style="margin-top: 0; color: #045a8d;">📄 Documents fournis</h3>
|
||
<ul style="list-style: none; padding: 0; margin: 10px 0 0;">
|
||
${bookingData.documents.map(doc => `<li style="padding: 8px 0;">📄 <strong>${doc.type}:</strong> ${doc.fileName}</li>`).join('')}
|
||
</ul>
|
||
</div>
|
||
|
||
<div style="text-align: center; margin: 30px 0;">
|
||
<p style="font-weight: bold; font-size: 16px;">Veuillez confirmer votre décision :</p>
|
||
<div style="margin: 15px 0;">
|
||
<a href="${acceptUrl}" style="display: inline-block; padding: 15px 30px; background-color: #00aa00; color: #ffffff; text-decoration: none; border-radius: 6px; margin: 0 5px; min-width: 200px;">✓ Accepter la demande</a>
|
||
<a href="${rejectUrl}" style="display: inline-block; padding: 15px 30px; background-color: #cc0000; color: #ffffff; text-decoration: none; border-radius: 6px; margin: 0 5px; min-width: 200px;">✗ Refuser la demande</a>
|
||
</div>
|
||
</div>
|
||
|
||
<div style="background-color: #fff8e1; border-left: 4px solid #f57c00; padding: 15px; margin: 20px 0; border-radius: 4px;">
|
||
<p style="margin: 0; font-size: 14px; color: #666;">
|
||
<strong style="color: #f57c00;">⚠️ Important</strong><br>
|
||
Cette demande expire automatiquement dans <strong>7 jours</strong> si aucune action n'est prise.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div style="background-color: #f4f6f8; padding: 20px; text-align: center; font-size: 12px; color: #666;">
|
||
<p style="margin: 5px 0; font-weight: bold; color: #045a8d;">Référence de réservation : ${bookingData.bookingId}</p>
|
||
<p style="margin: 5px 0;">© 2025 Xpeditis. Tous droits réservés.</p>
|
||
<p style="margin: 5px 0;">Cet email a été envoyé automatiquement. Merci de ne pas y répondre directement.</p>
|
||
</div>
|
||
</div>
|
||
</body>
|
||
</html>
|
||
`;
|
||
|
||
try {
|
||
console.log('Données du booking:');
|
||
console.log(' Booking ID:', bookingData.bookingId);
|
||
console.log(' Route:', bookingData.origin, '→', bookingData.destination);
|
||
console.log(' Prix:', bookingData.priceUSD, 'USD');
|
||
console.log(' Accept URL:', acceptUrl);
|
||
console.log(' Reject URL:', rejectUrl);
|
||
console.log('\nEnvoi en cours...');
|
||
|
||
const info = await transporter.sendMail({
|
||
from: process.env.SMTP_FROM || 'noreply@xpeditis.com',
|
||
to: 'carrier@test.com',
|
||
subject: `Nouvelle demande de réservation - ${bookingData.origin} → ${bookingData.destination}`,
|
||
html: htmlTemplate,
|
||
});
|
||
|
||
console.log('\n✅ Email transporteur envoyé avec succès!');
|
||
console.log(' Message ID:', info.messageId);
|
||
console.log(' Response:', info.response);
|
||
console.log(' Accepted:', info.accepted);
|
||
console.log(' Rejected:', info.rejected);
|
||
console.log('\n📬 Vérifiez votre inbox Mailtrap:');
|
||
console.log(' URL: https://mailtrap.io/inboxes');
|
||
console.log(' Sujet: Nouvelle demande de réservation - FRPAR → USNYC');
|
||
return true;
|
||
} catch (error) {
|
||
console.error('\n❌ Échec d\'envoi email transporteur:');
|
||
console.error(' Message:', error.message);
|
||
console.error(' Code:', error.code);
|
||
console.error(' ResponseCode:', error.responseCode);
|
||
console.error(' Response:', error.response);
|
||
if (error.stack) {
|
||
console.error(' Stack:', error.stack.substring(0, 300));
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// Exécuter tous les tests
|
||
async function runAllTests() {
|
||
console.log('\n🚀 DÉMARRAGE DES TESTS');
|
||
console.log('='.repeat(60));
|
||
|
||
// Test 1: Connexion
|
||
const connectionOk = await testConnection();
|
||
if (!connectionOk) {
|
||
console.log('\n❌ ARRÊT: La connexion SMTP a échoué');
|
||
console.log(' Vérifiez vos credentials SMTP dans .env');
|
||
process.exit(1);
|
||
}
|
||
|
||
// Test 2: Email simple
|
||
const simpleEmailOk = await sendSimpleEmail();
|
||
if (!simpleEmailOk) {
|
||
console.log('\n⚠️ L\'email simple a échoué, mais on continue...');
|
||
}
|
||
|
||
// Test 3: Email transporteur
|
||
const carrierEmailOk = await sendCarrierEmail();
|
||
|
||
// Résumé
|
||
console.log('\n' + '='.repeat(60));
|
||
console.log('📊 RÉSUMÉ DES TESTS:');
|
||
console.log('='.repeat(60));
|
||
console.log('Connexion SMTP:', connectionOk ? '✅ OK' : '❌ ÉCHEC');
|
||
console.log('Email simple:', simpleEmailOk ? '✅ OK' : '❌ ÉCHEC');
|
||
console.log('Email transporteur:', carrierEmailOk ? '✅ OK' : '❌ ÉCHEC');
|
||
|
||
if (connectionOk && simpleEmailOk && carrierEmailOk) {
|
||
console.log('\n✅ TOUS LES TESTS ONT RÉUSSI!');
|
||
console.log(' Le système d\'envoi d\'email fonctionne correctement.');
|
||
console.log(' Si vous ne recevez pas les emails dans le backend,');
|
||
console.log(' le problème vient de l\'intégration NestJS.');
|
||
} else {
|
||
console.log('\n❌ CERTAINS TESTS ONT ÉCHOUÉ');
|
||
console.log(' Vérifiez les erreurs ci-dessus pour comprendre le problème.');
|
||
}
|
||
|
||
console.log('\n' + '='.repeat(60));
|
||
}
|
||
|
||
// Lancer les tests
|
||
runAllTests()
|
||
.then(() => {
|
||
console.log('\n✅ Tests terminés\n');
|
||
process.exit(0);
|
||
})
|
||
.catch(error => {
|
||
console.error('\n❌ Erreur fatale:', error);
|
||
process.exit(1);
|
||
});
|