Yaltopia-FIFA/app/api/matches/[matchId]/results/route.ts
Kirubel-Kibru-Yaltopia 89440985f1
Some checks failed
Deploy to Cloudflare Workers / deploy (push) Has been cancelled
x
2026-05-24 21:46:10 +03:00

59 lines
1.7 KiB
TypeScript

import { createClient } from "@/lib/supabase/server";
import { requireUser } from "@/lib/api/auth";
import { ApiError, apiError, apiSuccess, parseJson } from "@/lib/api/errors";
import * as matches from "@/lib/services/matches";
export async function POST(
request: Request,
{ params }: { params: Promise<{ matchId: string }> }
) {
try {
const { matchId } = await params;
const supabase = await createClient();
await requireUser(supabase);
const body = await parseJson<{
action: "submit" | "approve" | "set";
teamId?: string;
homeScore?: number;
awayScore?: number;
note?: string;
}>(request);
switch (body.action) {
case "submit":
if (body.teamId == null || body.homeScore == null || body.awayScore == null) {
throw new ApiError(400, "teamId, homeScore, awayScore required");
}
await matches.submitResult(
supabase,
matchId,
body.teamId,
body.homeScore,
body.awayScore
);
break;
case "approve":
await matches.approveResult(supabase, matchId);
break;
case "set":
if (body.homeScore == null || body.awayScore == null) {
throw new ApiError(400, "homeScore, awayScore required");
}
await matches.setResultByManager(
supabase,
matchId,
body.homeScore,
body.awayScore,
body.note
);
break;
default:
throw new ApiError(400, "action must be submit, approve, or set");
}
return apiSuccess({ ok: true });
} catch (e) {
return apiError(e);
}
}