Yaltopia-FIFA/lib/services/matches.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

133 lines
3.4 KiB
TypeScript

import type { SupabaseClient } from "@supabase/supabase-js";
export async function listMatches(
supabase: SupabaseClient,
competitionId: string
) {
const { data, error } = await supabase
.from("matches")
.select(
`*, home:home_team_id(id, name, logo_path), away:away_team_id(id, name, logo_path)`
)
.eq("competition_id", competitionId)
.order("matchday")
.order("round");
if (error) throw new Error(error.message);
return data;
}
export async function getMatch(supabase: SupabaseClient, matchId: string) {
const { data, error } = await supabase
.from("matches")
.select(
`*, home:home_team_id(id, name, logo_path), away:away_team_id(id, name, logo_path)`
)
.eq("id", matchId)
.single();
if (error) throw new Error(error.message);
return data;
}
export async function getMatchDetails(
supabase: SupabaseClient,
matchId: string
) {
const [match, submissions, signatures] = await Promise.all([
getMatch(supabase, matchId),
supabase
.from("match_result_submissions")
.select("*, teams(name)")
.eq("match_id", matchId),
supabase
.from("match_signatures")
.select("team_id, signed_at, teams(name)")
.eq("match_id", matchId),
]);
return {
match,
submissions: submissions.data ?? [],
signatures: signatures.data ?? [],
};
}
export async function proposeSchedule(
supabase: SupabaseClient,
matchId: string,
scheduledAt: string
) {
const { error } = await supabase.rpc("propose_match_schedule", {
p_match_id: matchId,
p_scheduled_at: scheduledAt,
});
if (error) throw new Error(error.message);
}
export async function signSchedule(
supabase: SupabaseClient,
matchId: string,
teamId: string
) {
const { error } = await supabase.rpc("sign_match_schedule", {
p_match_id: matchId,
p_team_id: teamId,
});
if (error) throw new Error(error.message);
}
export async function submitResult(
supabase: SupabaseClient,
matchId: string,
teamId: string,
homeScore: number,
awayScore: number
) {
const { error } = await supabase.rpc("submit_match_result", {
p_match_id: matchId,
p_team_id: teamId,
p_home_score: homeScore,
p_away_score: awayScore,
});
if (error) throw new Error(error.message);
}
export async function approveResult(
supabase: SupabaseClient,
matchId: string
) {
const { error } = await supabase.rpc("approve_match_result", {
p_match_id: matchId,
});
if (error) throw new Error(error.message);
}
export async function setResultByManager(
supabase: SupabaseClient,
matchId: string,
homeScore: number,
awayScore: number,
note?: string
) {
const { error } = await supabase.rpc("set_match_result_by_manager", {
p_match_id: matchId,
p_home_score: homeScore,
p_away_score: awayScore,
p_note: note ?? null,
});
if (error) throw new Error(error.message);
}
export async function listPendingResults(
supabase: SupabaseClient,
competitionId: string
) {
const { data, error } = await supabase
.from("matches")
.select(
`*, home:home_team_id(name, logo_path), away:away_team_id(name, logo_path)`
)
.eq("competition_id", competitionId)
.in("result_status", ["pending_approval", "disputed"]);
if (error) throw new Error(error.message);
return data;
}