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>
144 lines
4 KiB
TypeScript
144 lines
4 KiB
TypeScript
import { eq, and } from "drizzle-orm";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
|
|
export type ParticipantResult = typeof schema.seasonParticipantResults.$inferSelect;
|
|
export type ParticipantResultWithParticipant = ParticipantResult & {
|
|
participant: { id: string; name: string } | null;
|
|
};
|
|
export type NewParticipantResult = typeof schema.seasonParticipantResults.$inferInsert;
|
|
|
|
export async function createParticipantResult(
|
|
data: NewParticipantResult
|
|
): Promise<ParticipantResult> {
|
|
const db = database();
|
|
const [result] = await db
|
|
.insert(schema.seasonParticipantResults)
|
|
.values(data)
|
|
.returning();
|
|
return result;
|
|
}
|
|
|
|
export async function createManyParticipantResults(
|
|
data: NewParticipantResult[]
|
|
): Promise<ParticipantResult[]> {
|
|
const db = database();
|
|
return await db
|
|
.insert(schema.seasonParticipantResults)
|
|
.values(data)
|
|
.returning();
|
|
}
|
|
|
|
export async function findParticipantResultById(
|
|
id: string
|
|
): Promise<ParticipantResult | undefined> {
|
|
const db = database();
|
|
return await db.query.seasonParticipantResults.findFirst({
|
|
where: eq(schema.seasonParticipantResults.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.seasonParticipantResults.findFirst({
|
|
where: eq(schema.seasonParticipantResults.participantId, participantId),
|
|
with: {
|
|
participant: true,
|
|
sportsSeason: {
|
|
with: {
|
|
sport: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function findParticipantResultsBySportsSeasonId(
|
|
sportsSeasonId: string
|
|
): Promise<ParticipantResultWithParticipant[]> {
|
|
const db = database();
|
|
return await db.query.seasonParticipantResults.findMany({
|
|
where: eq(schema.seasonParticipantResults.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.seasonParticipantResults)
|
|
.set({ ...data, updatedAt: new Date() })
|
|
.where(eq(schema.seasonParticipantResults.id, id))
|
|
.returning();
|
|
return result;
|
|
}
|
|
|
|
export async function deleteParticipantResult(id: string): Promise<void> {
|
|
const db = database();
|
|
await db.delete(schema.seasonParticipantResults).where(eq(schema.seasonParticipantResults.id, id));
|
|
}
|
|
|
|
export async function deleteParticipantResultsBySportsSeasonId(
|
|
sportsSeasonId: string
|
|
): Promise<void> {
|
|
const db = database();
|
|
await db
|
|
.delete(schema.seasonParticipantResults)
|
|
.where(eq(schema.seasonParticipantResults.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.seasonParticipantResults.findFirst({
|
|
where: and(
|
|
eq(schema.seasonParticipantResults.participantId, participantId),
|
|
eq(schema.seasonParticipantResults.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,
|
|
});
|
|
}
|
|
}
|