Some checks failed
Deploy to Cloudflare Workers / deploy (push) Has been cancelled
126 lines
3.2 KiB
TypeScript
126 lines
3.2 KiB
TypeScript
import type { SupabaseClient } from "@supabase/supabase-js";
|
|
|
|
export async function createTeam(
|
|
supabase: SupabaseClient,
|
|
competitionId: string,
|
|
input: { name: string; nickname?: string; icon?: string }
|
|
) {
|
|
const { data, error } = await supabase
|
|
.from("teams")
|
|
.insert({
|
|
competition_id: competitionId,
|
|
name: input.name,
|
|
nickname: input.nickname || null,
|
|
icon: input.icon || "shield",
|
|
})
|
|
.select()
|
|
.single();
|
|
if (error) throw new Error(error.message);
|
|
return data;
|
|
}
|
|
|
|
export async function deleteTeam(supabase: SupabaseClient, teamId: string) {
|
|
const { error } = await supabase.from("teams").delete().eq("id", teamId);
|
|
if (error) throw new Error(error.message);
|
|
}
|
|
|
|
export async function listTeams(
|
|
supabase: SupabaseClient,
|
|
competitionId: string
|
|
) {
|
|
const { data, error } = await supabase
|
|
.from("teams")
|
|
.select("*")
|
|
.eq("competition_id", competitionId)
|
|
.order("name");
|
|
if (error) throw new Error(error.message);
|
|
return data;
|
|
}
|
|
|
|
export async function updateTeam(
|
|
supabase: SupabaseClient,
|
|
teamId: string,
|
|
updates: {
|
|
home_stadium_name?: string;
|
|
logo_path?: string;
|
|
nickname?: string;
|
|
icon?: string;
|
|
}
|
|
) {
|
|
const { data, error } = await supabase
|
|
.from("teams")
|
|
.update(updates)
|
|
.eq("id", teamId)
|
|
.select()
|
|
.single();
|
|
if (error) throw new Error(error.message);
|
|
return data;
|
|
}
|
|
|
|
export async function setAvailability(
|
|
supabase: SupabaseClient,
|
|
teamId: string,
|
|
windows: { day_of_week: number; start_time?: string; end_time?: string }[]
|
|
) {
|
|
await supabase.from("team_availability").delete().eq("team_id", teamId);
|
|
if (windows.length === 0) return;
|
|
const { error } = await supabase.from("team_availability").insert(
|
|
windows.map((w) => ({
|
|
team_id: teamId,
|
|
day_of_week: w.day_of_week,
|
|
start_time: w.start_time || null,
|
|
end_time: w.end_time || null,
|
|
}))
|
|
);
|
|
if (error) throw new Error(error.message);
|
|
}
|
|
|
|
export async function getManagerTeam(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
competitionId: string
|
|
) {
|
|
const { data: teams } = await supabase
|
|
.from("teams")
|
|
.select("id")
|
|
.eq("competition_id", competitionId);
|
|
const teamIds = teams?.map((t) => t.id) ?? [];
|
|
if (teamIds.length === 0) return null;
|
|
|
|
const { data } = await supabase
|
|
.from("team_members")
|
|
.select("team_id, teams(*)")
|
|
.eq("user_id", userId)
|
|
.eq("role", "manager")
|
|
.in("team_id", teamIds)
|
|
.limit(1)
|
|
.maybeSingle();
|
|
|
|
return data;
|
|
}
|
|
|
|
export async function getTeamDashboard(
|
|
supabase: SupabaseClient,
|
|
teamId: string,
|
|
competitionId: string
|
|
) {
|
|
const [results, playerStats] = await Promise.all([
|
|
supabase
|
|
.from("team_match_results")
|
|
.select("*")
|
|
.eq("team_id", teamId)
|
|
.order("matchday", { ascending: true }),
|
|
supabase
|
|
.from("player_competition_stats")
|
|
.select("*")
|
|
.eq("team_id", teamId)
|
|
.eq("competition_id", competitionId)
|
|
.order("goals", { ascending: false }),
|
|
]);
|
|
|
|
return {
|
|
results: results.data ?? [],
|
|
playerStats: playerStats.data ?? [],
|
|
};
|
|
}
|