brackt/app/models/participant-result.ts
Chris Parsons a3b8fa6309 feat: Implement bracket expansion plan to support various tournament structures
- Added a comprehensive plan for bracket expansion, including support for 4, 8, 16, 32, and 68 team formats.
- Introduced a template-based bracket system with predefined templates for NCAA March Madness, NFL Playoffs, NBA Playoffs, and simple brackets.
- Updated UI flow for bracket creation, allowing admins to select templates and assign participants flexibly.
- Enhanced database schema to accommodate new scoring rules and bracket templates.
- Proposed updates to scoring logic to handle non-scoring rounds and participant placements correctly.
- Documented implementation phases for gradual rollout of new features.
- Addressed critical bugs in the playoff event processing and scoring logic, ensuring proper advancement and scoring rules across multiple leagues.
2025-11-03 09:36:16 -08:00

141 lines
3.7 KiB
TypeScript

import { eq, and } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
export type ParticipantResult = typeof schema.participantResults.$inferSelect;
export type NewParticipantResult = typeof schema.participantResults.$inferInsert;
export async function createParticipantResult(
data: NewParticipantResult
): Promise<ParticipantResult> {
const db = database();
const [result] = await db
.insert(schema.participantResults)
.values(data)
.returning();
return result;
}
export async function createManyParticipantResults(
data: NewParticipantResult[]
): Promise<ParticipantResult[]> {
const db = database();
return await db
.insert(schema.participantResults)
.values(data)
.returning();
}
export async function findParticipantResultById(
id: string
): Promise<ParticipantResult | undefined> {
const db = database();
return await db.query.participantResults.findFirst({
where: eq(schema.participantResults.id, id),
with: {
participant: true,
sportsSeason: {
with: {
sport: true,
},
},
},
});
}
export async function findParticipantResultByParticipantId(
participantId: string
): Promise<ParticipantResult | undefined> {
const db = database();
return await db.query.participantResults.findFirst({
where: eq(schema.participantResults.participantId, participantId),
with: {
participant: true,
sportsSeason: {
with: {
sport: true,
},
},
},
});
}
export async function findParticipantResultsBySportsSeasonId(
sportsSeasonId: string
): Promise<ParticipantResult[]> {
const db = database();
return await db.query.participantResults.findMany({
where: eq(schema.participantResults.sportsSeasonId, sportsSeasonId),
orderBy: (results, { asc }) => [asc(results.finalPosition)],
with: {
participant: true,
},
});
}
export async function updateParticipantResult(
id: string,
data: Partial<NewParticipantResult>
): Promise<ParticipantResult> {
const db = database();
const [result] = await db
.update(schema.participantResults)
.set({ ...data, updatedAt: new Date() })
.where(eq(schema.participantResults.id, id))
.returning();
return result;
}
export async function deleteParticipantResult(id: string): Promise<void> {
const db = database();
await db.delete(schema.participantResults).where(eq(schema.participantResults.id, id));
}
export async function deleteParticipantResultsBySportsSeasonId(
sportsSeasonId: string
): Promise<void> {
const db = database();
await db
.delete(schema.participantResults)
.where(eq(schema.participantResults.sportsSeasonId, sportsSeasonId));
}
/**
* Set result for a participant in a sports season
* Points are calculated on-demand based on each fantasy league's scoring rules
*/
export async function setParticipantResult(
participantId: string,
sportsSeasonId: string,
finalPosition: number,
qualifyingPoints?: number,
notes?: string
): Promise<ParticipantResult> {
const db = database();
// Check if result already exists
const existing = await db.query.participantResults.findFirst({
where: and(
eq(schema.participantResults.participantId, participantId),
eq(schema.participantResults.sportsSeasonId, sportsSeasonId)
),
});
if (existing) {
// Update existing result
return await updateParticipantResult(existing.id, {
finalPosition,
qualifyingPoints: qualifyingPoints?.toString(),
notes,
});
} else {
// Create new result
return await createParticipantResult({
participantId,
sportsSeasonId,
finalPosition,
qualifyingPoints: qualifyingPoints?.toString(),
notes,
});
}
}