Fuxi/supabase/functions/judge-challenges/index.ts
2023-12-13 23:56:01 +08:00

59 lines
1.5 KiB
TypeScript

import { createClient } from "https://esm.sh/@supabase/supabase-js";
import { runChallenge } from "./runners/index.ts";
// http://127.0.0.1:54321/functions/v1/judge-challenges
// Judge all status is submitted challenges
Deno.serve(async (_) => {
try {
const client = createClient(
Deno.env.get("SUPABASE_URL") ?? "",
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? ""
);
const { data, error } = await client
.from("challenges")
.select<any, any>("*")
.eq("status", "submitted")
.limit(20);
if (error) {
throw error;
}
let counter = 0;
for (const item of data) {
const { data: problem } = await client
.from("problems")
.select<any, any>("*")
.eq("id", item.problem)
.single();
if (problem == null) {
throw new Error("Problem was not found.");
}
const { data: cases } = await client
.from("problem_cases")
.select<any, any>("*")
.eq("problem", problem.id);
const result = await runChallenge(item, problem, cases);
await client
.from("challenges")
.update<any>({ status: result.status === "skipped" ? "finished" : "judging", details: result })
.eq("id", item.id);
counter++;
}
return new Response(JSON.stringify({ judged: counter }), {
headers: { "Content-Type": "application/json" },
status: 200
});
} catch (err) {
return new Response(String(err?.message ?? err), { status: 500 });
}
});