brackt/app/models/season-template-sport.ts
Claude 5cea6fd9b5
Convert season templates from season-based to sport-based for evergreen use
Templates previously referenced specific sports seasons (e.g. "NFL 2026"), requiring
admins to create a new template every year. Templates now reference sports directly
(e.g. "NFL") and resolve to the currently-draftable season at league-creation time.

- Remove `year` field from season_templates table
- Replace `sportsSeasonId` FK with `sportId` FK in season_template_sports
- applySportsFromTemplate now resolves each sport to its active draftable season
- Admin template UI updated to manage sports (not specific seasons)
- sports-data-sync export/import updated to the new sport-centric format

https://claude.ai/code/session_01CoRLHERakMncbPs877izvw
2026-06-04 17:54:16 +00:00

75 lines
2 KiB
TypeScript

import { eq, and } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
export type SeasonTemplateSport = typeof schema.seasonTemplateSports.$inferSelect;
export type NewSeasonTemplateSport = typeof schema.seasonTemplateSports.$inferInsert;
export async function addSportToTemplate(
templateId: string,
sportId: string
): Promise<SeasonTemplateSport> {
const db = database();
const [link] = await db
.insert(schema.seasonTemplateSports)
.values({
templateId,
sportId,
})
.returning();
return link;
}
export async function addMultipleSportsToTemplate(
data: NewSeasonTemplateSport[]
): Promise<SeasonTemplateSport[]> {
const db = database();
return await db
.insert(schema.seasonTemplateSports)
.values(data)
.returning();
}
export async function removeSportFromTemplate(
templateId: string,
sportId: string
): Promise<void> {
const db = database();
await db
.delete(schema.seasonTemplateSports)
.where(
and(
eq(schema.seasonTemplateSports.templateId, templateId),
eq(schema.seasonTemplateSports.sportId, sportId)
)
);
}
export async function findSeasonTemplateSportsByTemplateId(
templateId: string
): Promise<SeasonTemplateSport[]> {
const db = database();
return await db.query.seasonTemplateSports.findMany({
where: eq(schema.seasonTemplateSports.templateId, templateId),
with: {
sport: true,
},
});
}
export async function findSeasonTemplateSportsBySportId(
sportId: string
): Promise<SeasonTemplateSport[]> {
const db = database();
return await db.query.seasonTemplateSports.findMany({
where: eq(schema.seasonTemplateSports.sportId, sportId),
with: {
template: true,
},
});
}
export async function deleteSeasonTemplateSport(id: string): Promise<void> {
const db = database();
await db.delete(schema.seasonTemplateSports).where(eq(schema.seasonTemplateSports.id, id));
}