brackt/app/models/tournament.ts
Chris Parsons 5a47300110
feat(models): add canonical tournament, participant, result, surface-elo models
Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).

Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
  createCanonicalParticipant, etc.) to avoid collision with existing
  season-participant.ts exports
- All four models include comprehensive unit tests following the
  audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints

Part of Phase 1b of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:16:53 +00:00

94 lines
2.3 KiB
TypeScript

import { eq, and, asc } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
export type Tournament = typeof schema.tournaments.$inferSelect;
export type NewTournament = typeof schema.tournaments.$inferInsert;
export type TournamentStatus = Tournament["status"];
export async function createTournament(
data: NewTournament
): Promise<Tournament> {
const db = database();
const [tournament] = await db
.insert(schema.tournaments)
.values(data)
.returning();
return tournament;
}
export async function getTournamentById(
id: string
): Promise<Tournament | null> {
const db = database();
const results = await db
.select()
.from(schema.tournaments)
.where(eq(schema.tournaments.id, id))
.limit(1);
return results[0] ?? null;
}
export async function findTournamentsBySport(
sportId: string
): Promise<Tournament[]> {
const db = database();
return await db
.select()
.from(schema.tournaments)
.where(eq(schema.tournaments.sportId, sportId))
.orderBy(asc(schema.tournaments.year), asc(schema.tournaments.startsAt));
}
export async function findTournamentBySportNameYear(
sportId: string,
name: string,
year: number
): Promise<Tournament | null> {
const db = database();
const results = await db
.select()
.from(schema.tournaments)
.where(
and(
eq(schema.tournaments.sportId, sportId),
eq(schema.tournaments.name, name),
eq(schema.tournaments.year, year)
)
)
.limit(1);
return results[0] ?? null;
}
export async function upsertTournament(
data: NewTournament
): Promise<Tournament> {
if (!data.sportId || !data.name || !data.year) {
throw new Error("sportId, name, and year are required for upsert");
}
const existing = await findTournamentBySportNameYear(
data.sportId,
data.name,
data.year
);
if (existing) {
return existing;
}
return await createTournament(data);
}
export async function updateTournamentStatus(
id: string,
status: TournamentStatus
): Promise<Tournament> {
const db = database();
const [tournament] = await db
.update(schema.tournaments)
.set({ status, updatedAt: new Date() })
.where(eq(schema.tournaments.id, id))
.returning();
return tournament;
}