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

59 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2023-12-13 13:18:14 +00:00
import { createClient } from "https://esm.sh/@supabase/supabase-js";
2023-12-13 15:56:01 +00:00
import { runChallenge } from "./runners/index.ts";
2023-12-13 13:18:14 +00:00
2023-12-13 15:56:01 +00:00
// http://127.0.0.1:54321/functions/v1/judge-challenges
// Judge all status is submitted challenges
Deno.serve(async (_) => {
2023-12-13 13:18:14 +00:00
try {
const client = createClient(
2023-12-13 15:56:01 +00:00
Deno.env.get("SUPABASE_URL") ?? "",
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? ""
);
2023-12-13 13:18:14 +00:00
const { data, error } = await client
2023-12-13 15:56:01 +00:00
.from("challenges")
.select<any, any>("*")
.eq("status", "submitted")
.limit(20);
2023-12-13 13:18:14 +00:00
if (error) {
2023-12-13 15:56:01 +00:00
throw error;
2023-12-13 13:18:14 +00:00
}
2023-12-13 15:56:01 +00:00
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.");
}
2023-12-13 13:18:14 +00:00
2023-12-13 15:56:01 +00:00
const { data: cases } = await client
.from("problem_cases")
.select<any, any>("*")
.eq("problem", problem.id);
2023-12-13 13:18:14 +00:00
2023-12-13 15:56:01 +00:00
const result = await runChallenge(item, problem, cases);
2023-12-13 13:18:14 +00:00
2023-12-13 15:56:01 +00:00
await client
.from("challenges")
.update<any>({ status: result.status === "skipped" ? "finished" : "judging", details: result })
.eq("id", item.id);
2023-12-13 13:18:14 +00:00
2023-12-13 15:56:01 +00:00
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 });
}
});