espace-paie-odentas/DEBUG_EMAIL_LOGS.md
2025-10-12 17:05:46 +02:00

2.4 KiB

🔍 Diagnostic Email SES - Logs de Débogage Activés

Logs Ajoutés pour Diagnostic

J'ai ajouté des logs détaillés dans 3 fichiers pour identifier exactement d'où vient l'email invalide :

1. Dans app/api/contrats/[id]/route.ts

  • 🔍 [ROUTE DEBUG] Organization data retrieved for non-staff user
  • 🔍 [ROUTE DEBUG] About to send email notifications with organizationData

2. Dans lib/emailService.ts

  • 🔍 [EMAIL DEBUG] sendContractUpdateNotifications called with
  • 🔍 [EMAIL DEBUG] Email addresses found
  • 🔍 [EMAIL DEBUG] Sending email with validated addresses

3. Dans lib/emailTemplateService.ts

  • 🔍 [SES DEBUG] sendUniversalEmailV2 called with
  • 🚨 [SES DEBUG] Invalid toEmail detected (si erreur)
  • 🔍 [SES DEBUG] Emails validated, proceeding with send

Corrections Appliquées

Fix Potentiel : Array vs Object

Si organization_details est retourné comme un tableau au lieu d'un objet :

// Fix: Si organization_details est un tableau, prendre le premier élément
let orgDetails = organizationData?.organization_details;
if (Array.isArray(orgDetails)) {
  console.log('🔍 [EMAIL DEBUG] organization_details is array, taking first element');
  orgDetails = orgDetails[0] || {};
}

Comment Tester

  1. Modifiez un contrat en tant qu'utilisateur non-staff
  2. Regardez les logs Vercel pour voir les messages de debug
  3. Identifiez exactement quelle valeur d'email cause le problème

Attendu dans les Logs

🔍 [ROUTE DEBUG] Organization data retrieved for non-staff user: {
  orgId: "...",
  hasOrgDetails: true,
  emailNotifs: "...", // ← La valeur problématique sera ici
  emailNotifsCC: "..."
}

🔍 [EMAIL DEBUG] Email addresses found: {
  emailNotifs: "...", // ← Valeur exacte
  emailNotifsType: "string" // ← Type de données
}

🚨 [SES DEBUG] Invalid toEmail detected: {
  toEmail: "...", // ← Email invalide détecté
  isEmpty: true/false
}

Actions Possibles

Si organization_details est un tableau vide :

"organization_details": []

→ Le fix array est appliqué

Si emailNotifs est une chaîne vide :

"emailNotifs": ""

→ Validation should catch this

Si la structure de données est inattendue :

Les logs détaillés révéleront la structure exacte.

Testez maintenant et regardez les logs Vercel pour identifier le problème ! 🔍