2025-10-12 21:16:00 -07:00
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
|
|
|
|
|
|
|
|
|
export type Sport = typeof schema.sports.$inferSelect;
|
|
|
|
|
export type NewSport = typeof schema.sports.$inferInsert;
|
|
|
|
|
export type SportType = "team" | "individual";
|
|
|
|
|
|
2025-10-13 10:30:47 -07:00
|
|
|
export type SportWithActiveSeasons = Sport & {
|
|
|
|
|
activeSeasons: Array<{ id: string; name: string; year: number }>;
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-12 21:16:00 -07:00
|
|
|
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({
|
Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:14:54 -07:00
|
|
|
orderBy: (s, { asc }) => [asc(s.name)],
|
2025-10-12 21:16:00 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 10:30:47 -07:00
|
|
|
export async function findAllSportsWithActiveSeasons(): Promise<SportWithActiveSeasons[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
const sports = await db.query.sports.findMany({
|
Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:14:54 -07:00
|
|
|
orderBy: (s, { asc }) => [asc(s.name)],
|
2025-10-13 10:30:47 -07:00
|
|
|
with: {
|
|
|
|
|
sportsSeasons: {
|
Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:14:54 -07:00
|
|
|
where: (ss, { or }) =>
|
2025-10-13 10:30:47 -07:00
|
|
|
or(
|
Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:14:54 -07:00
|
|
|
eq(ss.status, "active"),
|
|
|
|
|
eq(ss.status, "upcoming")
|
2025-10-13 10:30:47 -07:00
|
|
|
),
|
Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:14:54 -07:00
|
|
|
orderBy: (ss, { desc }) => [desc(ss.year)],
|
2025-10-13 10:30:47 -07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return sports.map((sport) => ({
|
|
|
|
|
...sport,
|
|
|
|
|
activeSeasons: sport.sportsSeasons.map((season) => ({
|
|
|
|
|
id: season.id,
|
|
|
|
|
name: season.name,
|
|
|
|
|
year: season.year,
|
|
|
|
|
})),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 21:16:00 -07:00
|
|
|
export async function findSportsByType(type: SportType): Promise<Sport[]> {
|
|
|
|
|
const db = database();
|
|
|
|
|
return await db.query.sports.findMany({
|
|
|
|
|
where: eq(schema.sports.type, type),
|
Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:14:54 -07:00
|
|
|
orderBy: (s, { asc }) => [asc(s.name)],
|
2025-10-12 21:16:00 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|