Updated all model files to use the renamed schema exports from Task 1: - participants → seasonParticipants - participantExpectedValues → seasonParticipantExpectedValues - participantQualifyingTotals → seasonParticipantQualifyingTotals - participantResults → seasonParticipantResults - participantSurfaceElos → seasonParticipantSurfaceElos - eventResults.participantId → eventResults.seasonParticipantId - db.query relation accessors updated - Relation field .participant → .seasonParticipant where applicable - Import paths updated: ./participant → ./season-participant Files updated (14 model files + 3 test files): - draft-pick.ts - draft-utils.ts - event-result.ts - group-stage-match.ts - participant-result.ts - qualifying-points.ts - scoring-calculator.ts - scoring-event.ts - sports-season.ts - surface-elo.ts - team-score-events.ts - cs2-major-stage.ts - golf-skills.ts - participant-expected-value.ts - __tests__/sports-season.clone.test.ts - __tests__/auto-pick.test.ts - __tests__/executeAutoPick.timer.test.ts Typecheck errors decreased: 779 → 499 (280 fewer) All model file errors related to renamed schemas resolved. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
243 lines
5.6 KiB
TypeScript
243 lines
5.6 KiB
TypeScript
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
import { eq, and, inArray } from "drizzle-orm";
|
|
|
|
export interface CreateEventResultData {
|
|
scoringEventId: string;
|
|
participantId: string;
|
|
placement?: number;
|
|
qualifyingPointsAwarded?: number;
|
|
eliminated?: boolean;
|
|
rawScore?: number;
|
|
}
|
|
|
|
export interface UpdateEventResultData {
|
|
placement?: number;
|
|
qualifyingPointsAwarded?: number;
|
|
eliminated?: boolean;
|
|
rawScore?: number;
|
|
}
|
|
|
|
/**
|
|
* Create an event result for a participant
|
|
*/
|
|
export async function createEventResult(
|
|
data: CreateEventResultData,
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
const [result] = await db
|
|
.insert(schema.eventResults)
|
|
.values({
|
|
scoringEventId: data.scoringEventId,
|
|
seasonParticipantId: data.participantId,
|
|
placement: data.placement,
|
|
qualifyingPointsAwarded: data.qualifyingPointsAwarded?.toString(),
|
|
eliminated: data.eliminated,
|
|
rawScore: data.rawScore?.toString(),
|
|
})
|
|
.returning();
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Create multiple event results in bulk
|
|
* Useful for entering all results for a tournament at once
|
|
*/
|
|
export async function createEventResultsBulk(
|
|
results: CreateEventResultData[],
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
const inserted = await db
|
|
.insert(schema.eventResults)
|
|
.values(
|
|
results.map((r) => ({
|
|
scoringEventId: r.scoringEventId,
|
|
seasonParticipantId: r.participantId,
|
|
placement: r.placement,
|
|
qualifyingPointsAwarded: r.qualifyingPointsAwarded?.toString(),
|
|
eliminated: r.eliminated,
|
|
rawScore: r.rawScore?.toString(),
|
|
}))
|
|
)
|
|
.returning();
|
|
|
|
return inserted;
|
|
}
|
|
|
|
/**
|
|
* Get an event result by ID
|
|
*/
|
|
export async function getEventResultById(
|
|
resultId: string,
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
return await db.query.eventResults.findFirst({
|
|
where: eq(schema.eventResults.id, resultId),
|
|
with: {
|
|
seasonParticipant: {
|
|
with: {
|
|
sportsSeason: {
|
|
with: {
|
|
sport: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
scoringEvent: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get all results for a scoring event
|
|
*/
|
|
export async function getEventResults(
|
|
eventId: string,
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
return await db.query.eventResults.findMany({
|
|
where: eq(schema.eventResults.scoringEventId, eventId),
|
|
orderBy: schema.eventResults.placement,
|
|
with: {
|
|
seasonParticipant: {
|
|
with: {
|
|
sportsSeason: {
|
|
with: {
|
|
sport: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get all event results for a participant across all events
|
|
*/
|
|
export async function getParticipantEventResults(
|
|
participantId: string,
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
return await db.query.eventResults.findMany({
|
|
where: eq(schema.eventResults.seasonParticipantId, participantId),
|
|
with: {
|
|
scoringEvent: true,
|
|
},
|
|
orderBy: schema.eventResults.createdAt,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update an event result
|
|
*/
|
|
export async function updateEventResult(
|
|
resultId: string,
|
|
data: UpdateEventResultData,
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
const [updated] = await db
|
|
.update(schema.eventResults)
|
|
.set({
|
|
placement: data.placement,
|
|
qualifyingPointsAwarded: data.qualifyingPointsAwarded?.toString(),
|
|
eliminated: data.eliminated,
|
|
rawScore: data.rawScore?.toString(),
|
|
updatedAt: new Date(),
|
|
})
|
|
.where(eq(schema.eventResults.id, resultId))
|
|
.returning();
|
|
|
|
return updated;
|
|
}
|
|
|
|
/**
|
|
* Delete an event result
|
|
*/
|
|
export async function deleteEventResult(
|
|
resultId: string,
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
await db.delete(schema.eventResults).where(eq(schema.eventResults.id, resultId));
|
|
}
|
|
|
|
/**
|
|
* Delete all results for a scoring event
|
|
*/
|
|
export async function deleteEventResults(
|
|
eventId: string,
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
await db
|
|
.delete(schema.eventResults)
|
|
.where(eq(schema.eventResults.scoringEventId, eventId));
|
|
}
|
|
|
|
/**
|
|
* Check if a participant has a result for a specific event
|
|
*/
|
|
export async function hasParticipantResult(
|
|
eventId: string,
|
|
participantId: string,
|
|
providedDb?: ReturnType<typeof database>
|
|
): Promise<boolean> {
|
|
const db = providedDb || database();
|
|
|
|
const result = await db.query.eventResults.findFirst({
|
|
where: and(
|
|
eq(schema.eventResults.scoringEventId, eventId),
|
|
eq(schema.eventResults.seasonParticipantId, participantId)
|
|
),
|
|
});
|
|
|
|
return !!result;
|
|
}
|
|
|
|
/**
|
|
* Get results for multiple participants in a specific event
|
|
* Useful for checking drafted participants' performance
|
|
*/
|
|
export async function getEventResultsForParticipants(
|
|
eventId: string,
|
|
participantIds: string[],
|
|
providedDb?: ReturnType<typeof database>
|
|
) {
|
|
const db = providedDb || database();
|
|
|
|
if (participantIds.length === 0) return [];
|
|
|
|
return await db.query.eventResults.findMany({
|
|
where: and(
|
|
eq(schema.eventResults.scoringEventId, eventId),
|
|
inArray(schema.eventResults.seasonParticipantId, participantIds)
|
|
),
|
|
with: {
|
|
seasonParticipant: {
|
|
with: {
|
|
sportsSeason: {
|
|
with: {
|
|
sport: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|