2025-10-16 00:32:48 -07:00
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
2026-02-22 17:27:16 -08:00
|
|
|
import { eq, and } from "drizzle-orm";
|
2025-10-16 00:32:48 -07:00
|
|
|
|
|
|
|
|
export async function initializeDraftTimers(seasonId: string, teams: { id: string }[], initialTime: number) {
|
|
|
|
|
const timerData = teams.map((team) => ({
|
|
|
|
|
seasonId,
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
timeRemaining: initialTime,
|
|
|
|
|
}));
|
2026-02-22 17:27:16 -08:00
|
|
|
|
2025-10-16 00:32:48 -07:00
|
|
|
const db = database();
|
|
|
|
|
await db.insert(schema.draftTimers).values(timerData);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:27:16 -08:00
|
|
|
export async function getTeamTimer(seasonId: string, teamId: string) {
|
2025-10-16 00:32:48 -07:00
|
|
|
const db = database();
|
|
|
|
|
const [timer] = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(schema.draftTimers)
|
2026-02-22 17:27:16 -08:00
|
|
|
.where(and(eq(schema.draftTimers.seasonId, seasonId), eq(schema.draftTimers.teamId, teamId)));
|
2025-10-16 00:32:48 -07:00
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getSeasonTimers(seasonId: string) {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(schema.draftTimers)
|
|
|
|
|
.where(eq(schema.draftTimers.seasonId, seasonId));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:27:16 -08:00
|
|
|
export async function updateTeamTimer(seasonId: string, teamId: string, timeRemaining: number) {
|
2025-10-16 00:32:48 -07:00
|
|
|
const db = database();
|
|
|
|
|
const [timer] = await db
|
|
|
|
|
.update(schema.draftTimers)
|
|
|
|
|
.set({ timeRemaining, updatedAt: new Date() })
|
2026-02-22 17:27:16 -08:00
|
|
|
.where(and(eq(schema.draftTimers.seasonId, seasonId), eq(schema.draftTimers.teamId, teamId)))
|
2025-10-16 00:32:48 -07:00
|
|
|
.returning();
|
|
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteSeasonTimers(seasonId: string) {
|
|
|
|
|
const db = database();
|
|
|
|
|
await db.delete(schema.draftTimers).where(eq(schema.draftTimers.seasonId, seasonId));
|
|
|
|
|
}
|