brackt/app/models/sport.ts
Chris Parsons eca1508b3f
Improve admin dashboard stats and fix scoring event bugs (#230)
* 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>
2026-03-25 20:46:54 -07:00

101 lines
2.7 KiB
TypeScript

import { eq, sql } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
export async function countSports(): Promise<number> {
const db = database();
const [{ count }] = await db
.select({ count: sql<number>`count(*)::int` })
.from(schema.sports);
return count;
}
export type Sport = typeof schema.sports.$inferSelect;
export type NewSport = typeof schema.sports.$inferInsert;
export type SportType = "team" | "individual";
export type SportWithActiveSeasons = Sport & {
activeSeasons: Array<{ id: string; name: string; year: number }>;
};
export async function createSport(data: NewSport): Promise<Sport> {
const db = database();
const [sport] = await db
.insert(schema.sports)
.values(data)
.returning();
return sport;
}
export async function findSportById(id: string): Promise<Sport | undefined> {
const db = database();
return await db.query.sports.findFirst({
where: eq(schema.sports.id, id),
});
}
export async function findSportBySlug(slug: string): Promise<Sport | undefined> {
const db = database();
return await db.query.sports.findFirst({
where: eq(schema.sports.slug, slug),
});
}
export async function findAllSports(): Promise<Sport[]> {
const db = database();
return await db.query.sports.findMany({
orderBy: (s, { asc }) => [asc(s.name)],
});
}
export async function findAllSportsWithActiveSeasons(): Promise<SportWithActiveSeasons[]> {
const db = database();
const sports = await db.query.sports.findMany({
orderBy: (s, { asc }) => [asc(s.name)],
with: {
sportsSeasons: {
where: (ss, { or }) =>
or(
eq(ss.status, "active"),
eq(ss.status, "upcoming")
),
orderBy: (ss, { desc }) => [desc(ss.year)],
},
},
});
return sports.map((sport) => ({
...sport,
activeSeasons: sport.sportsSeasons.map((season) => ({
id: season.id,
name: season.name,
year: season.year,
})),
}));
}
export async function findSportsByType(type: SportType): Promise<Sport[]> {
const db = database();
return await db.query.sports.findMany({
where: eq(schema.sports.type, type),
orderBy: (s, { asc }) => [asc(s.name)],
});
}
export async function updateSport(
id: string,
data: Partial<NewSport>
): Promise<Sport> {
const db = database();
const [sport] = await db
.update(schema.sports)
.set({ ...data, updatedAt: new Date() })
.where(eq(schema.sports.id, id))
.returning();
return sport;
}
export async function deleteSport(id: string): Promise<void> {
const db = database();
await db.delete(schema.sports).where(eq(schema.sports.id, id));
}