* Improve admin dashboard stats and fix scoring event date bugs, fixes #92 - Add "Seasons by Status" breakdown card (pre-draft / drafting / active / completed) - Replace full-table fetches with COUNT queries for sports, sports seasons, and templates - Fix bracket events with null eventDate disappearing from the Games to Score tabs by computing a displayDate from matched playoffMatchGames.scheduledAt in getEventsForDates - Fix Today tab badge showing stale count when all events are scored - Hide "Getting Started" checklist once the system has sports configured - Use explicit "en-US" locale in formatEventTime for deterministic output Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Fix scoring-event-dashboard tests after select/sql changes - Rename mockSelectDistinct → mockSelect to match implementation change from .selectDistinct() to .select() - Add sql to drizzle-orm mock - Update mock row shapes to include eventDate and gameDate fields - Add two tests covering displayDate derivation from eventDate and gameDate Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
96 lines
2.8 KiB
TypeScript
96 lines
2.8 KiB
TypeScript
import { eq, sql } from "drizzle-orm";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
|
|
export async function countSeasonTemplates(): Promise<number> {
|
|
const db = database();
|
|
const [{ count }] = await db
|
|
.select({ count: sql<number>`count(*)::int` })
|
|
.from(schema.seasonTemplates);
|
|
return count;
|
|
}
|
|
|
|
export type SeasonTemplate = typeof schema.seasonTemplates.$inferSelect;
|
|
export type NewSeasonTemplate = typeof schema.seasonTemplates.$inferInsert;
|
|
|
|
export async function createSeasonTemplate(data: NewSeasonTemplate): Promise<SeasonTemplate> {
|
|
const db = database();
|
|
const [template] = await db
|
|
.insert(schema.seasonTemplates)
|
|
.values(data)
|
|
.returning();
|
|
return template;
|
|
}
|
|
|
|
export async function findSeasonTemplateById(id: string): Promise<SeasonTemplate | undefined> {
|
|
const db = database();
|
|
return await db.query.seasonTemplates.findFirst({
|
|
where: eq(schema.seasonTemplates.id, id),
|
|
});
|
|
}
|
|
|
|
export async function findSeasonTemplateWithSportsSeasons(id: string) {
|
|
const db = database();
|
|
return await db.query.seasonTemplates.findFirst({
|
|
where: eq(schema.seasonTemplates.id, id),
|
|
with: {
|
|
seasonTemplateSports: {
|
|
with: {
|
|
sportsSeason: {
|
|
with: {
|
|
sport: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function findActiveSeasonTemplates(): Promise<SeasonTemplate[]> {
|
|
const db = database();
|
|
return await db.query.seasonTemplates.findMany({
|
|
where: eq(schema.seasonTemplates.isActive, true),
|
|
orderBy: (templates, { desc }) => [desc(templates.year)],
|
|
});
|
|
}
|
|
|
|
export async function findSeasonTemplatesByYear(year: number): Promise<SeasonTemplate[]> {
|
|
const db = database();
|
|
return await db.query.seasonTemplates.findMany({
|
|
where: eq(schema.seasonTemplates.year, year),
|
|
orderBy: (templates, { asc }) => [asc(templates.name)],
|
|
});
|
|
}
|
|
|
|
export async function findAllSeasonTemplates(): Promise<SeasonTemplate[]> {
|
|
const db = database();
|
|
return await db.query.seasonTemplates.findMany({
|
|
orderBy: (templates, { desc }) => [desc(templates.year)],
|
|
});
|
|
}
|
|
|
|
export async function updateSeasonTemplate(
|
|
id: string,
|
|
data: Partial<NewSeasonTemplate>
|
|
): Promise<SeasonTemplate> {
|
|
const db = database();
|
|
const [template] = await db
|
|
.update(schema.seasonTemplates)
|
|
.set({ ...data, updatedAt: new Date() })
|
|
.where(eq(schema.seasonTemplates.id, id))
|
|
.returning();
|
|
return template;
|
|
}
|
|
|
|
export async function setSeasonTemplateActive(
|
|
id: string,
|
|
isActive: boolean
|
|
): Promise<SeasonTemplate> {
|
|
return await updateSeasonTemplate(id, { isActive });
|
|
}
|
|
|
|
export async function deleteSeasonTemplate(id: string): Promise<void> {
|
|
const db = database();
|
|
await db.delete(schema.seasonTemplates).where(eq(schema.seasonTemplates.id, id));
|
|
}
|