brackt/app/models/tournament-group.ts
Chris Parsons e2b178221a
Add oxlint linting setup with zero errors (#194)
* Add oxlint and fix all lint errors

- Install oxlint, add .oxlintrc.json with rules for TypeScript/React
- Add npm run lint / lint:fix scripts
- Add Claude PostToolUse hook to run oxlint on every edited file
- Fix 101 errors: unused vars/imports, eqeqeq, prefer-const, no-new-array
- Fix no-array-index-key (use stable keys or suppress positional cases)
- Fix exhaustive-deps missing dependency in useEffect
- Promote exhaustive-deps and no-array-index-key to errors
- Fix Map.get() !== null bug in $leagueId.server.ts (should be !== undefined)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix no-explicit-any warnings and upgrade tsconfig to ES2023

- Replace all `any` types with proper types or `unknown` across ~20 files
- Add typed socket payload interfaces in draft route and useDraftSocket
- Use any[] with eslint-disable for socket.io callbacks (legitimate escape hatch)
- Bump all tsconfigs from ES2022 → ES2023 to support toSorted/toReversed
- Fix cascading type errors uncovered by removing any: Map.get narrowing,
  participant relation types, ChartDataPoint, Partial<NewSeason> indexing
- Add ParticipantResultWithParticipant type to participant-result model
- Fix test fixtures to match updated interfaces (DraftCell, ParticipantResult)
- Fix duplicate getQPStandings import in sportsSeasonId.server.ts

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Promote no-explicit-any to error

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 09:44:05 -07:00

143 lines
3.6 KiB
TypeScript

import { eq } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
export type TournamentGroup = typeof schema.tournamentGroups.$inferSelect;
export type TournamentGroupMember = typeof schema.tournamentGroupMembers.$inferSelect;
/**
* Create empty tournament groups for a scoring event
*/
export async function createGroupsForEvent(
eventId: string,
groupLabels: string[]
): Promise<TournamentGroup[]> {
const db = database();
const groups = await db
.insert(schema.tournamentGroups)
.values(
groupLabels.map((label) => ({
scoringEventId: eventId,
groupName: label,
}))
)
.returning();
return groups;
}
/**
* Add participants to a tournament group
*/
export async function addMembersToGroup(
groupId: string,
participantIds: string[]
): Promise<TournamentGroupMember[]> {
const db = database();
const members = await db
.insert(schema.tournamentGroupMembers)
.values(
participantIds.map((participantId) => ({
tournamentGroupId: groupId,
participantId,
}))
)
.returning();
return members;
}
/**
* Find all tournament groups for a scoring event with members and participant data
*/
export async function findGroupsByEventId(eventId: string) {
const db = database();
return await db.query.tournamentGroups.findMany({
where: eq(schema.tournamentGroups.scoringEventId, eventId),
orderBy: (groups, { asc }) => [asc(groups.groupName)],
with: {
members: {
with: {
participant: true,
},
orderBy: (members, { asc }) => [asc(members.createdAt)],
},
},
});
}
/**
* Toggle the eliminated status of a group member
*/
export async function toggleMemberEliminated(
memberId: string
): Promise<TournamentGroupMember> {
const db = database();
// Get current state
const member = await db.query.tournamentGroupMembers.findFirst({
where: eq(schema.tournamentGroupMembers.id, memberId),
});
if (!member) {
throw new Error("Tournament group member not found");
}
const [updated] = await db
.update(schema.tournamentGroupMembers)
.set({
eliminated: !member.eliminated,
updatedAt: new Date(),
})
.where(eq(schema.tournamentGroupMembers.id, memberId))
.returning();
return updated;
}
/**
* Get participant IDs of non-eliminated group members (advancing teams)
*/
export async function getAdvancingParticipantIds(
eventId: string
): Promise<string[]> {
const db = database();
const groups = await db.query.tournamentGroups.findMany({
where: eq(schema.tournamentGroups.scoringEventId, eventId),
with: {
members: {
where: eq(schema.tournamentGroupMembers.eliminated, false),
},
},
});
return groups.flatMap((g) => g.members.map((m) => m.participantId));
}
/**
* Get participant IDs of eliminated group members
*/
export async function getEliminatedParticipantIds(
eventId: string
): Promise<string[]> {
const db = database();
const groups = await db.query.tournamentGroups.findMany({
where: eq(schema.tournamentGroups.scoringEventId, eventId),
with: {
members: {
where: eq(schema.tournamentGroupMembers.eliminated, true),
},
},
});
return groups.flatMap((g) => g.members.map((m) => m.participantId));
}
/**
* Delete all tournament groups for a scoring event (for regeneration)
*/
export async function deleteGroupsByEventId(eventId: string): Promise<void> {
const db = database();
// Members cascade-delete when groups are deleted
await db
.delete(schema.tournamentGroups)
.where(eq(schema.tournamentGroups.scoringEventId, eventId));
}