import { notifyOwner } from "./_core/notification"; export interface ImportNotificationData { source: "email" | "folder"; fileName?: string; totalFiles?: number; totalInvoices: number; imported: number; duplicates: number; errors: number; } /** * Send notification after automatic import */ export async function sendImportNotification( userId: number, data: ImportNotificationData ): Promise { try { const sourceLabel = data.source === "email" ? "email" : "dossier"; let title: string; let content: string; if (data.source === "email" && data.fileName) { // Single file from email title = `📧 Import automatique email - ${data.fileName}`; content = ` **Fichier traité :** ${data.fileName} **Résultats :** - ✅ **${data.imported}** facture(s) importée(s) - 🔄 **${data.duplicates}** doublon(s) ignoré(s) - ❌ **${data.errors}** erreur(s) **Total détecté :** ${data.totalInvoices} facture(s) --- *Import automatique depuis email* `.trim(); } else if (data.source === "folder") { // Folder scan (potentially multiple files) title = `📁 Import automatique dossier - ${data.totalFiles || 0} fichier(s)`; content = ` **Fichiers traités :** ${data.totalFiles || 0} fichier(s) PDF **Résultats :** - ✅ **${data.imported}** facture(s) importée(s) - 🔄 **${data.duplicates}** doublon(s) ignoré(s) - ❌ **${data.errors}** erreur(s) **Total détecté :** ${data.totalInvoices} facture(s) --- *Import automatique depuis dossier* `.trim(); } else { // Fallback title = `📥 Import automatique - ${sourceLabel}`; content = ` **Résultats :** - ✅ **${data.imported}** facture(s) importée(s) - 🔄 **${data.duplicates}** doublon(s) ignoré(s) - ❌ **${data.errors}** erreur(s) **Total détecté :** ${data.totalInvoices} facture(s) `.trim(); } console.log(`[Notification] Sending import notification for user ${userId}:`, title); const success = await notifyOwner({ title, content, }); if (success) { console.log(`[Notification] Import notification sent successfully for user ${userId}`); } else { console.error(`[Notification] Failed to send import notification for user ${userId}`); } return success; } catch (error) { console.error(`[Notification] Error sending import notification for user ${userId}:`, error); return false; } }