diff --git a/src/routes/hooks/poll-imap.ts b/src/routes/hooks/poll-imap.ts index a51733c..fbbde7f 100644 --- a/src/routes/hooks/poll-imap.ts +++ b/src/routes/hooks/poll-imap.ts @@ -41,6 +41,7 @@ export const Route = createFileRoute("/hooks/poll-imap")({ let imported = 0; let highestUid = settings.last_uid ?? 0; let errorMessage: string | null = null; + const newEmailIds: string[] = []; try { await client.connect(); @@ -191,7 +192,24 @@ export const Route = createFileRoute("/hooks/poll-imap")({ if (errorMessage) { return json({ ok: false, imported, error: errorMessage }, 500); } - return json({ ok: true, imported, last_uid: highestUid }); + + // Run case-matching on freshly imported emails. + let matchStats = { matched: 0, suggested: 0, unmatched: 0 }; + if (newEmailIds.length > 0) { + try { + matchStats = await matchAndUpdateEmails(newEmailIds); + } catch (e) { + console.error("Case matching failed", e); + } + } + + return json({ + ok: true, + imported, + last_uid: highestUid, + matched: matchStats.matched, + suggested: matchStats.suggested, + }); } catch (e) { const message = e instanceof Error ? e.message : String(e); console.error("poll-imap fatal", message);