Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-24 03:35:05 +00:00
co-authored by renee-png
parent f905abb05b
commit c2ebdf2fe7
2 changed files with 366 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
// Re-runs case matching against incoming emails.
// POST { ids?: string[] } -> rematch the listed email ids
// POST {} -> rematch all currently unmatched, non-archived emails
import { createFileRoute } from "@tanstack/react-router";
import { supabaseAdmin } from "@/integrations/supabase/client.server";
import { matchAndUpdateEmails } from "@/lib/email-matching.server";
export const Route = createFileRoute("/hooks/rematch-emails")({
server: {
handlers: {
POST: async ({ request }) => {
let ids: string[] = [];
try {
const body = (await request.json()) as { ids?: string[] };
if (Array.isArray(body?.ids)) ids = body.ids.filter((x) => typeof x === "string");
} catch {
/* no body is fine */
}
if (ids.length === 0) {
const { data, error } = await supabaseAdmin
.from("incoming_emails")
.select("id")
.is("case_id", null)
.eq("is_archived", false)
.order("received_at", { ascending: false })
.limit(500);
if (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
ids = (data ?? []).map((r) => r.id);
}
try {
const stats = await matchAndUpdateEmails(ids);
return new Response(
JSON.stringify({ ok: true, scanned: ids.length, ...stats }),
{ status: 200, headers: { "Content-Type": "application/json" } },
);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
return new Response(JSON.stringify({ error: msg }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
},
},
},
});