feat: warningMessage quota IA dans rapports d'import + redémarrage auto services

This commit is contained in:
Manus
2026-06-09 12:15:26 -04:00
parent 595f5dded1
commit f49eadda2b
7 changed files with 2224 additions and 3 deletions

View File

@@ -40,7 +40,7 @@ async function processEmailAttachment(
userId: number,
attachment: Attachment,
emailSubject: string
): Promise<{ success: boolean; totalInvoices: number; imported: number; duplicates: number; errors: number }> {
): Promise<{ success: boolean; totalInvoices: number; imported: number; duplicates: number; errors: number; quotaError?: boolean }> {
const fileName = attachment.filename || `email-attachment-${Date.now()}.pdf`;
console.log(`[EmailImport] Processing attachment: ${fileName} from email: ${emailSubject}`);
@@ -206,6 +206,20 @@ async function processEmailAttachment(
processingProgress: `Terminé: ${importedCount} importée(s), ${duplicatesCount} doublon(s)`,
});
// Détecter si des erreurs de quota ont eu lieu
const quotaErrors = errorDetails.filter(e =>
e.error && (
e.error.includes('usage exhausted') ||
e.error.includes('quota') ||
e.error.includes('rate limit') ||
e.error.includes('Precondition Failed') ||
e.error.includes('429')
)
);
const warningMessage = quotaErrors.length > 0
? `Quota IA épuisé : ${quotaErrors.length} facture(s) non extraite(s). Vérifiez votre quota Manus ou configurez une clé API externe dans Paramétrage → IA.`
: null;
// Create import log
await createImportLog({
userId,
@@ -217,6 +231,7 @@ async function processEmailAttachment(
errors: errorsCount,
duplicateDetails: duplicateDetails.length > 0 ? JSON.stringify(duplicateDetails) : null,
errorDetails: errorDetails.length > 0 ? JSON.stringify(errorDetails) : null,
warningMessage,
});
console.log(`[EmailImport] Successfully processed attachment: ${fileName}`);
@@ -229,14 +244,23 @@ async function processEmailAttachment(
duplicates: duplicatesCount,
errors: errorsCount,
};
} catch (error) {
} catch (error: any) {
console.error(`[EmailImport] Error processing attachment ${attachment.filename}:`, error);
// Détecter l'erreur de quota IA
const isQuotaError = error?.message && (
error.message.includes('usage exhausted') ||
error.message.includes('quota') ||
error.message.includes('429') ||
error.message.includes('rate limit') ||
error.message.includes('Precondition Failed')
);
return {
success: false,
totalInvoices: 0,
imported: 0,
duplicates: 0,
errors: 1,
quotaError: isQuotaError as boolean,
};
}
}