Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
f905abb05b
commit
c2ebdf2fe7
@@ -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" },
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user