Fuxi/supabase/functions/fresh-challenges/index.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-12-13 15:56:01 +00:00
import { createClient } from "https://esm.sh/@supabase/supabase-js";
// http://127.0.0.1:54321/functions/v1/fresh-challenges
// Auto get judging status from remote lawyers
Deno.serve(async (req) => {
try {
const client = createClient(
Deno.env.get("SUPABASE_URL") ?? "",
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? ""
);
const body = await req.json();
const { data } = await client
.from("challenges")
.select<any, any>("*")
.eq("status", "judging")
.eq("id", body.id)
.single();
if (data == null || data?.details?.submissions == null) {
return new Response(String("Unable to find the challenge with the id in the request."), { status: 400 });
}
const tokens = data?.details?.submissions
.map((item: { token: string }) => item.token)
.join(",");
const resp = await fetch(
Deno.env.get("JUDGE0_ENDPOINT") + `/submissions/batch?tokens=${tokens}`,
{ method: "GET" }
);
const result = await resp.json();
return new Response(JSON.stringify({ result }), {
headers: { "Content-Type": "application/json" },
status: 200
});
} catch (err) {
return new Response(String(err?.message ?? err), { status: 500 });
}
});