Yaltopia-FIFA/app/api/leagues/[leagueId]/masters/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

45 lines
1.3 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 masters from "@/lib/services/masters";
export async function GET(
_request: Request,
{ params }: { params: Promise<{ leagueId: string }> }
) {
try {
const { leagueId } = await params;
const supabase = await createClient();
await requireUser(supabase);
const data = await masters.listLeagueMasters(supabase, leagueId);
return apiSuccess(data);
} catch (e) {
return apiError(e);
}
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ leagueId: string }> }
) {
try {
const { leagueId } = await params;
const supabase = await createClient();
const user = await requireUser(supabase);
const body = await parseJson<{ email: string }>(request);
const targetId = await masters.getUserIdByEmail(supabase, body.email);
if (!targetId) {
throw new ApiError(404, "No user found with that email");
}
await masters.assignLeagueMaster(
supabase,
leagueId,
targetId,
user.id
);
return apiSuccess({ assigned: true });
} catch (e) {
return apiError(e);
}
}