refactor(models): update model layer to use renamed schema exports
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>
This commit is contained in:
parent
fdf5fe8976
commit
fd99cab61b
17 changed files with 217 additions and 217 deletions
|
|
@ -34,7 +34,7 @@ import {
|
|||
getTeamDraftPicksWithSports,
|
||||
isParticipantDrafted,
|
||||
} from "~/models/draft-pick";
|
||||
import { getParticipantsForSeasonWithSports } from "~/models/participant";
|
||||
import { getParticipantsForSeasonWithSports } from "~/models/season-participant";
|
||||
import { getSeasonSportsSimple } from "~/models/season-sport";
|
||||
import { getTeamQueue, getAllQueuesForSeason } from "~/models/draft-queue";
|
||||
import { calculateDraftEligibility } from "~/lib/draft-eligibility";
|
||||
|
|
@ -49,7 +49,7 @@ const ALL_TEAM_IDS = [TEAM_ID, "team-2"];
|
|||
function makeMockDb(overrides: Record<string, unknown> = {}) {
|
||||
const mockDb: Record<string, unknown> = {
|
||||
query: {
|
||||
participants: {
|
||||
seasonParticipants: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
},
|
||||
|
|
@ -71,7 +71,7 @@ function makeMockDb(overrides: Record<string, unknown> = {}) {
|
|||
// 3. select().from().where().orderBy() → top participant → [{id, ...}]
|
||||
function makeMockDbWithEvParticipant(participantId: string) {
|
||||
return {
|
||||
query: { participants: { findMany: vi.fn().mockResolvedValue([]) } },
|
||||
query: { seasonParticipants: { findMany: vi.fn().mockResolvedValue([]) } },
|
||||
select: vi.fn().mockReturnThis(),
|
||||
from: vi.fn().mockReturnThis(),
|
||||
innerJoin: vi.fn().mockReturnThis(),
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ vi.mock("~/lib/draft-eligibility", () => ({
|
|||
vi.mock("~/database/context");
|
||||
|
||||
import { getDraftPicksWithSports, getTeamDraftPicksWithSports, isParticipantDrafted } from "~/models/draft-pick";
|
||||
import { getParticipantsForSeasonWithSports } from "~/models/participant";
|
||||
import { getParticipantsForSeasonWithSports } from "~/models/season-participant";
|
||||
import { getSeasonSportsSimple } from "~/models/season-sport";
|
||||
import { getTeamQueue, getAllQueuesForSeason } from "~/models/draft-queue";
|
||||
import { calculateDraftEligibility } from "~/lib/draft-eligibility";
|
||||
|
|
@ -127,7 +127,7 @@ function makeMockDb(seasonOverrides: Record<string, unknown> = {}) {
|
|||
draftPicks: { findFirst: vi.fn().mockResolvedValue(null) },
|
||||
seasons: { findFirst: vi.fn().mockResolvedValue(makeSeason(seasonOverrides)) },
|
||||
draftSlots: { findMany: vi.fn().mockResolvedValue(mockDraftSlots) },
|
||||
participants: {
|
||||
seasonParticipants: {
|
||||
// findMany: used by autoPickForTeam queue path
|
||||
findMany: vi.fn().mockResolvedValue([mockParticipantForQueue]),
|
||||
// findFirst: used by executeAutoPick to fetch full participant details
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ vi.mock("~/database/context", () => ({
|
|||
|
||||
vi.mock("~/database/schema", () => ({
|
||||
sportsSeasons: { id: "ss.id", sportId: "ss.sport_id" },
|
||||
participants: { sportsSeasonId: "p.sports_season_id" },
|
||||
seasonParticipants: { sportsSeasonId: "p.sports_season_id" },
|
||||
scoringEvents: { sportsSeasonId: "se.sports_season_id" },
|
||||
participantExpectedValues: { sportsSeasonId: "pev.sports_season_id" },
|
||||
seasonParticipantExpectedValues: { sportsSeasonId: "pev.sports_season_id" },
|
||||
}));
|
||||
|
||||
vi.mock("drizzle-orm", () => ({
|
||||
|
|
@ -91,16 +91,16 @@ function makeMockDb({
|
|||
const db: any = {
|
||||
query: {
|
||||
sportsSeasons: { findFirst: vi.fn().mockResolvedValue(ss) },
|
||||
participants: { findMany: vi.fn().mockResolvedValue(participants) },
|
||||
seasonParticipants: { findMany: vi.fn().mockResolvedValue(participants) },
|
||||
scoringEvents: { findMany: vi.fn().mockResolvedValue(scoringEvents) },
|
||||
participantExpectedValues: { findMany: vi.fn().mockResolvedValue(sourceEvRows) },
|
||||
seasonParticipantExpectedValues: { findMany: vi.fn().mockResolvedValue(sourceEvRows) },
|
||||
},
|
||||
insert: vi.fn().mockImplementation((table: object) => {
|
||||
let key: string;
|
||||
let returnRows: object[];
|
||||
if (table === schema.sportsSeasons) {
|
||||
key = "sportsSeasons"; returnRows = [insertedSeason];
|
||||
} else if (table === schema.participants) {
|
||||
} else if (table === schema.seasonParticipants) {
|
||||
key = "participants"; returnRows = newParticipantRows;
|
||||
} else if (table === schema.scoringEvents) {
|
||||
key = "scoringEvents"; returnRows = [];
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
*/
|
||||
|
||||
import { database } from "~/database/context";
|
||||
import { cs2MajorStageResults, participants } from "~/database/schema";
|
||||
import { cs2MajorStageResults, seasonParticipants } from "~/database/schema";
|
||||
import { eq, and, sql } from "drizzle-orm";
|
||||
|
||||
export interface Cs2StageResult {
|
||||
|
|
@ -56,12 +56,12 @@ export async function getCs2StageResultsForEvent(
|
|||
finalPlacement: cs2MajorStageResults.finalPlacement,
|
||||
createdAt: cs2MajorStageResults.createdAt,
|
||||
updatedAt: cs2MajorStageResults.updatedAt,
|
||||
participantName: participants.name,
|
||||
participantName: seasonParticipants.name,
|
||||
})
|
||||
.from(cs2MajorStageResults)
|
||||
.innerJoin(participants, eq(cs2MajorStageResults.participantId, participants.id))
|
||||
.innerJoin(seasonParticipants, eq(cs2MajorStageResults.participantId, seasonParticipants.id))
|
||||
.where(eq(cs2MajorStageResults.scoringEventId, scoringEventId))
|
||||
.orderBy(cs2MajorStageResults.stageEntry, participants.name);
|
||||
.orderBy(cs2MajorStageResults.stageEntry, seasonParticipants.name);
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
|
@ -135,7 +135,7 @@ export async function clearCs2StageAssignments(
|
|||
|
||||
/**
|
||||
* Mark teams as eliminated from a specific stage.
|
||||
* Records stageEliminated and stageEliminatedWins for the specified participants.
|
||||
* Records stageEliminated and stageEliminatedWins for the specified seasonParticipants.
|
||||
* Teams NOT in eliminatedEntries that are in this stage are implicitly considered
|
||||
* to have advanced (stageEliminated remains null).
|
||||
*/
|
||||
|
|
@ -166,7 +166,7 @@ export async function markCs2StageEliminations(
|
|||
}
|
||||
|
||||
/**
|
||||
* Set final placements for all participants in a CS2 Major event.
|
||||
* Set final placements for all seasonParticipants in a CS2 Major event.
|
||||
* Called after the Champions Stage is complete.
|
||||
* placements: Map from participantId to final placement (1–32)
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -78,14 +78,14 @@ export async function getDraftedParticipantsBySportsSeason(
|
|||
|
||||
const results = await db
|
||||
.select({
|
||||
sportsSeasonId: schema.participants.sportsSeasonId,
|
||||
participantId: schema.participants.id,
|
||||
participantName: schema.participants.name,
|
||||
sportsSeasonId: schema.seasonParticipants.sportsSeasonId,
|
||||
participantId: schema.seasonParticipants.id,
|
||||
participantName: schema.seasonParticipants.name,
|
||||
})
|
||||
.from(schema.draftPicks)
|
||||
.innerJoin(
|
||||
schema.participants,
|
||||
eq(schema.draftPicks.participantId, schema.participants.id)
|
||||
schema.seasonParticipants,
|
||||
eq(schema.draftPicks.participantId, schema.seasonParticipants.id)
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
|
|
@ -182,10 +182,10 @@ export async function getDraftedParticipantsWithPoints(
|
|||
// Batch-fetch QP totals for qualifying_points participants
|
||||
const qpMap = new Map<string, number>(); // participantId → totalQP
|
||||
if (qpParticipantIds.size > 0 && qpSeasonIds.size > 0) {
|
||||
const totals = await db.query.participantQualifyingTotals.findMany({
|
||||
const totals = await db.query.seasonParticipantQualifyingTotals.findMany({
|
||||
where: and(
|
||||
inArray(schema.participantQualifyingTotals.participantId, [...qpParticipantIds]),
|
||||
inArray(schema.participantQualifyingTotals.sportsSeasonId, [...qpSeasonIds])
|
||||
inArray(schema.seasonParticipantQualifyingTotals.participantId, [...qpParticipantIds]),
|
||||
inArray(schema.seasonParticipantQualifyingTotals.sportsSeasonId, [...qpSeasonIds])
|
||||
),
|
||||
columns: { participantId: true, totalQualifyingPoints: true },
|
||||
});
|
||||
|
|
@ -245,19 +245,19 @@ export async function getDraftPicksWithSports(seasonId: string, providedDb?: Ret
|
|||
id: schema.draftPicks.id,
|
||||
teamId: schema.draftPicks.teamId,
|
||||
pickNumber: schema.draftPicks.pickNumber,
|
||||
participantId: schema.participants.id,
|
||||
participantName: schema.participants.name,
|
||||
participantId: schema.seasonParticipants.id,
|
||||
participantName: schema.seasonParticipants.name,
|
||||
sportId: schema.sports.id,
|
||||
sportName: schema.sports.name,
|
||||
})
|
||||
.from(schema.draftPicks)
|
||||
.innerJoin(
|
||||
schema.participants,
|
||||
eq(schema.draftPicks.participantId, schema.participants.id)
|
||||
schema.seasonParticipants,
|
||||
eq(schema.draftPicks.participantId, schema.seasonParticipants.id)
|
||||
)
|
||||
.innerJoin(
|
||||
schema.sportsSeasons,
|
||||
eq(schema.participants.sportsSeasonId, schema.sportsSeasons.id)
|
||||
eq(schema.seasonParticipants.sportsSeasonId, schema.sportsSeasons.id)
|
||||
)
|
||||
.innerJoin(
|
||||
schema.sports,
|
||||
|
|
@ -289,19 +289,19 @@ export async function getTeamDraftPicksWithSports(teamId: string, seasonId: stri
|
|||
id: schema.draftPicks.id,
|
||||
teamId: schema.draftPicks.teamId,
|
||||
pickNumber: schema.draftPicks.pickNumber,
|
||||
participantId: schema.participants.id,
|
||||
participantName: schema.participants.name,
|
||||
participantId: schema.seasonParticipants.id,
|
||||
participantName: schema.seasonParticipants.name,
|
||||
sportId: schema.sports.id,
|
||||
sportName: schema.sports.name,
|
||||
})
|
||||
.from(schema.draftPicks)
|
||||
.innerJoin(
|
||||
schema.participants,
|
||||
eq(schema.draftPicks.participantId, schema.participants.id)
|
||||
schema.seasonParticipants,
|
||||
eq(schema.draftPicks.participantId, schema.seasonParticipants.id)
|
||||
)
|
||||
.innerJoin(
|
||||
schema.sportsSeasons,
|
||||
eq(schema.participants.sportsSeasonId, schema.sportsSeasons.id)
|
||||
eq(schema.seasonParticipants.sportsSeasonId, schema.sportsSeasons.id)
|
||||
)
|
||||
.innerJoin(
|
||||
schema.sports,
|
||||
|
|
@ -338,13 +338,13 @@ export async function getDraftPicksForSeason(seasonId: string) {
|
|||
pickInRound: schema.draftPicks.pickInRound,
|
||||
timeUsed: schema.draftPicks.timeUsed,
|
||||
team: schema.teams,
|
||||
participant: schema.participants,
|
||||
participant: schema.seasonParticipants,
|
||||
sport: schema.sports,
|
||||
})
|
||||
.from(schema.draftPicks)
|
||||
.innerJoin(schema.teams, eq(schema.draftPicks.teamId, schema.teams.id))
|
||||
.innerJoin(schema.participants, eq(schema.draftPicks.participantId, schema.participants.id))
|
||||
.innerJoin(schema.sportsSeasons, eq(schema.participants.sportsSeasonId, schema.sportsSeasons.id))
|
||||
.innerJoin(schema.seasonParticipants, eq(schema.draftPicks.participantId, schema.seasonParticipants.id))
|
||||
.innerJoin(schema.sportsSeasons, eq(schema.seasonParticipants.sportsSeasonId, schema.sportsSeasons.id))
|
||||
.innerJoin(schema.sports, eq(schema.sportsSeasons.sportId, schema.sports.id))
|
||||
.where(eq(schema.draftPicks.seasonId, seasonId))
|
||||
.orderBy(asc(schema.draftPicks.pickNumber));
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { logger } from "~/lib/logger";
|
|||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import { getTeamQueue, getAllQueuesForSeason } from "./draft-queue";
|
||||
import { isParticipantDrafted, getDraftPicksWithSports, getTeamDraftPicksWithSports } from "./draft-pick";
|
||||
import { getParticipantsForSeasonWithSports } from "./participant";
|
||||
import { getParticipantsForSeasonWithSports } from "./season-participant";
|
||||
import { getSeasonSportsSimple } from "./season-sport";
|
||||
import { calculateDraftEligibility } from "~/lib/draft-eligibility";
|
||||
import { getSocketIO, scheduleDraftRoomClosure } from "../../server/socket";
|
||||
|
|
@ -118,8 +118,8 @@ export async function autoPickForTeam(
|
|||
|
||||
// Get participant details for queue items to check eligibility
|
||||
const queueParticipantIds = queue.map((item) => item.participantId);
|
||||
const queueParticipants = await db.query.participants.findMany({
|
||||
where: inArray(schema.participants.id, queueParticipantIds),
|
||||
const queueParticipants = await db.query.seasonParticipants.findMany({
|
||||
where: inArray(schema.seasonParticipants.id, queueParticipantIds),
|
||||
with: {
|
||||
sportsSeason: {
|
||||
with: {
|
||||
|
|
@ -264,17 +264,17 @@ export async function getTopAvailableParticipant(
|
|||
for (const sportsSeasonId of sportsSeasonIds) {
|
||||
let participantQuery = db
|
||||
.select()
|
||||
.from(schema.participants)
|
||||
.where(eq(schema.participants.sportsSeasonId, sportsSeasonId));
|
||||
.from(schema.seasonParticipants)
|
||||
.where(eq(schema.seasonParticipants.sportsSeasonId, sportsSeasonId));
|
||||
|
||||
if (draftedIds.length > 0) {
|
||||
participantQuery = db
|
||||
.select()
|
||||
.from(schema.participants)
|
||||
.from(schema.seasonParticipants)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.participants.sportsSeasonId, sportsSeasonId),
|
||||
notInArray(schema.participants.id, draftedIds)
|
||||
eq(schema.seasonParticipants.sportsSeasonId, sportsSeasonId),
|
||||
notInArray(schema.seasonParticipants.id, draftedIds)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
@ -299,21 +299,21 @@ export async function getTopAvailableParticipant(
|
|||
// Single sport season
|
||||
let query = db
|
||||
.select()
|
||||
.from(schema.participants)
|
||||
.where(eq(schema.participants.sportsSeasonId, sportsSeasonIds[0]))
|
||||
.orderBy(desc(schema.participants.vorpValue), schema.participants.name);
|
||||
.from(schema.seasonParticipants)
|
||||
.where(eq(schema.seasonParticipants.sportsSeasonId, sportsSeasonIds[0]))
|
||||
.orderBy(desc(schema.seasonParticipants.vorpValue), schema.seasonParticipants.name);
|
||||
|
||||
if (draftedIds.length > 0) {
|
||||
query = db
|
||||
.select()
|
||||
.from(schema.participants)
|
||||
.from(schema.seasonParticipants)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.participants.sportsSeasonId, sportsSeasonIds[0]),
|
||||
notInArray(schema.participants.id, draftedIds)
|
||||
eq(schema.seasonParticipants.sportsSeasonId, sportsSeasonIds[0]),
|
||||
notInArray(schema.seasonParticipants.id, draftedIds)
|
||||
)
|
||||
)
|
||||
.orderBy(desc(schema.participants.vorpValue), schema.participants.name);
|
||||
.orderBy(desc(schema.seasonParticipants.vorpValue), schema.seasonParticipants.name);
|
||||
}
|
||||
|
||||
const [topParticipant] = await query;
|
||||
|
|
@ -471,7 +471,7 @@ export async function executeAutoPick(params: {
|
|||
success: boolean;
|
||||
error?: string;
|
||||
pick?: InferSelectModel<typeof schema.draftPicks>;
|
||||
participant?: InferSelectModel<typeof schema.participants> & {
|
||||
participant?: InferSelectModel<typeof schema.seasonParticipants> & {
|
||||
sportsSeason: InferSelectModel<typeof schema.sportsSeasons> & {
|
||||
sport: InferSelectModel<typeof schema.sports>;
|
||||
};
|
||||
|
|
@ -581,8 +581,8 @@ export async function executeAutoPick(params: {
|
|||
}
|
||||
|
||||
// Get participant details
|
||||
const participantToPick = await db.query.participants.findFirst({
|
||||
where: eq(schema.participants.id, participantId),
|
||||
const participantToPick = await db.query.seasonParticipants.findFirst({
|
||||
where: eq(schema.seasonParticipants.id, participantId),
|
||||
with: {
|
||||
sportsSeason: {
|
||||
with: {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ export async function createEventResult(
|
|||
.insert(schema.eventResults)
|
||||
.values({
|
||||
scoringEventId: data.scoringEventId,
|
||||
participantId: data.participantId,
|
||||
seasonParticipantId: data.participantId,
|
||||
placement: data.placement,
|
||||
qualifyingPointsAwarded: data.qualifyingPointsAwarded?.toString(),
|
||||
eliminated: data.eliminated,
|
||||
|
|
@ -57,7 +57,7 @@ export async function createEventResultsBulk(
|
|||
.values(
|
||||
results.map((r) => ({
|
||||
scoringEventId: r.scoringEventId,
|
||||
participantId: r.participantId,
|
||||
seasonParticipantId: r.participantId,
|
||||
placement: r.placement,
|
||||
qualifyingPointsAwarded: r.qualifyingPointsAwarded?.toString(),
|
||||
eliminated: r.eliminated,
|
||||
|
|
@ -81,7 +81,7 @@ export async function getEventResultById(
|
|||
return await db.query.eventResults.findFirst({
|
||||
where: eq(schema.eventResults.id, resultId),
|
||||
with: {
|
||||
participant: {
|
||||
seasonParticipant: {
|
||||
with: {
|
||||
sportsSeason: {
|
||||
with: {
|
||||
|
|
@ -108,7 +108,7 @@ export async function getEventResults(
|
|||
where: eq(schema.eventResults.scoringEventId, eventId),
|
||||
orderBy: schema.eventResults.placement,
|
||||
with: {
|
||||
participant: {
|
||||
seasonParticipant: {
|
||||
with: {
|
||||
sportsSeason: {
|
||||
with: {
|
||||
|
|
@ -131,7 +131,7 @@ export async function getParticipantEventResults(
|
|||
const db = providedDb || database();
|
||||
|
||||
return await db.query.eventResults.findMany({
|
||||
where: eq(schema.eventResults.participantId, participantId),
|
||||
where: eq(schema.eventResults.seasonParticipantId, participantId),
|
||||
with: {
|
||||
scoringEvent: true,
|
||||
},
|
||||
|
|
@ -203,7 +203,7 @@ export async function hasParticipantResult(
|
|||
const result = await db.query.eventResults.findFirst({
|
||||
where: and(
|
||||
eq(schema.eventResults.scoringEventId, eventId),
|
||||
eq(schema.eventResults.participantId, participantId)
|
||||
eq(schema.eventResults.seasonParticipantId, participantId)
|
||||
),
|
||||
});
|
||||
|
||||
|
|
@ -226,10 +226,10 @@ export async function getEventResultsForParticipants(
|
|||
return await db.query.eventResults.findMany({
|
||||
where: and(
|
||||
eq(schema.eventResults.scoringEventId, eventId),
|
||||
inArray(schema.eventResults.participantId, participantIds)
|
||||
inArray(schema.eventResults.seasonParticipantId, participantIds)
|
||||
),
|
||||
with: {
|
||||
participant: {
|
||||
seasonParticipant: {
|
||||
with: {
|
||||
sportsSeason: {
|
||||
with: {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import { database } from "~/database/context";
|
||||
import { participantGolfSkills, participants } from "~/database/schema";
|
||||
import { participantGolfSkills, seasonParticipants } from "~/database/schema";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
|
||||
export interface GolfSkillsRecord {
|
||||
|
|
@ -40,7 +40,7 @@ export interface GolfSkillsInput {
|
|||
|
||||
/**
|
||||
* Get all golf skill records for a sports season, joined with participant names.
|
||||
* Returns one record per participant (participants with no record are excluded).
|
||||
* Returns one record per participant (seasonParticipants with no record are excluded).
|
||||
*/
|
||||
export async function getGolfSkillsForSeason(
|
||||
sportsSeasonId: string
|
||||
|
|
@ -58,12 +58,12 @@ export async function getGolfSkillsForSeason(
|
|||
openChampionshipOdds: participantGolfSkills.openChampionshipOdds,
|
||||
pgaChampionshipOdds: participantGolfSkills.pgaChampionshipOdds,
|
||||
updatedAt: participantGolfSkills.updatedAt,
|
||||
participantName: participants.name,
|
||||
participantName: seasonParticipants.name,
|
||||
})
|
||||
.from(participantGolfSkills)
|
||||
.innerJoin(participants, eq(participantGolfSkills.participantId, participants.id))
|
||||
.innerJoin(seasonParticipants, eq(participantGolfSkills.participantId, seasonParticipants.id))
|
||||
.where(eq(participantGolfSkills.sportsSeasonId, sportsSeasonId))
|
||||
.orderBy(participants.name);
|
||||
.orderBy(seasonParticipants.name);
|
||||
|
||||
return rows.map((r) => ({
|
||||
...r,
|
||||
|
|
@ -94,7 +94,7 @@ export async function getGolfSkillsMap(
|
|||
}
|
||||
|
||||
/**
|
||||
* Upsert golf skill ratings for a batch of participants.
|
||||
* Upsert golf skill ratings for a batch of seasonParticipants.
|
||||
* Uses INSERT … ON CONFLICT DO UPDATE so all columns are overwritten atomically.
|
||||
*/
|
||||
export async function batchUpsertGolfSkills(
|
||||
|
|
|
|||
|
|
@ -334,8 +334,8 @@ export async function getUpcomingGroupStageMatchesForParticipants(
|
|||
const allParticipantIds = [
|
||||
...new Set(rows.flatMap((r) => [r.participant1Id, r.participant2Id])),
|
||||
];
|
||||
const participantRows = await db.query.participants.findMany({
|
||||
where: inArray(schema.participants.id, allParticipantIds),
|
||||
const participantRows = await db.query.seasonParticipants.findMany({
|
||||
where: inArray(schema.seasonParticipants.id, allParticipantIds),
|
||||
});
|
||||
const participantMap = new Map(participantRows.map((p) => [p.id, p]));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
/**
|
||||
* Model for Participant Expected Values
|
||||
*
|
||||
* Manages probability distributions and calculated EVs for participants
|
||||
* Manages probability distributions and calculated EVs for seasonParticipants
|
||||
* in sports seasons.
|
||||
*/
|
||||
|
||||
import { database } from "~/database/context";
|
||||
import { participantExpectedValues, participants } from "~/database/schema";
|
||||
import { seasonParticipantExpectedValues, seasonParticipants } from "~/database/schema";
|
||||
import { eq, and, count, sql } from "drizzle-orm";
|
||||
import type { ProbabilityDistribution, ScoringRules } from "~/services/ev-calculator";
|
||||
import { calculateEV, normalizeProbabilities, calculateReplacementLevel, calculateVORP } from "~/services/ev-calculator";
|
||||
|
|
@ -53,9 +53,9 @@ export interface UpdateProbabilityInput {
|
|||
* Recalculate and persist VORP for every participant in a sports season.
|
||||
*
|
||||
* VORP = participant EV − replacement level EV
|
||||
* Replacement level = average EV of participants ranked 12th–14th in this season.
|
||||
* Replacement level = average EV of seasonParticipants ranked 12th–14th in this season.
|
||||
*
|
||||
* Call this after any operation that changes EVs for participants in the season.
|
||||
* Call this after any operation that changes EVs for seasonParticipants in the season.
|
||||
*/
|
||||
export async function syncVorpForSeason(sportsSeasonId: string): Promise<void> {
|
||||
const db = database();
|
||||
|
|
@ -72,12 +72,12 @@ export async function syncVorpForSeason(sportsSeasonId: string): Promise<void> {
|
|||
|
||||
const now = new Date();
|
||||
|
||||
// Build a single CASE expression to update all participants in one query
|
||||
// Build a single CASE expression to update all seasonParticipants in one query
|
||||
// instead of N individual UPDATE statements.
|
||||
const vorpCaseExpr = sql`CASE ${sql.join(
|
||||
sorted.map((ev) => {
|
||||
const vorp = calculateVORP(parseFloat(ev.expectedValue), replacementLevel);
|
||||
return sql`WHEN ${participants.id} = ${ev.participantId} THEN ${vorp.toFixed(4)}::numeric`;
|
||||
return sql`WHEN ${seasonParticipants.id} = ${ev.participantId} THEN ${vorp.toFixed(4)}::numeric`;
|
||||
}),
|
||||
sql` `
|
||||
)} END`;
|
||||
|
|
@ -85,9 +85,9 @@ export async function syncVorpForSeason(sportsSeasonId: string): Promise<void> {
|
|||
const ids = sorted.map((ev) => ev.participantId);
|
||||
|
||||
await db
|
||||
.update(participants)
|
||||
.update(seasonParticipants)
|
||||
.set({ vorpValue: vorpCaseExpr, updatedAt: now })
|
||||
.where(sql`${participants.id} = ANY(${sql`ARRAY[${sql.join(ids.map((id) => sql`${id}`), sql`, `)}]::uuid[]`})`);
|
||||
.where(sql`${seasonParticipants.id} = ANY(${sql`ARRAY[${sql.join(ids.map((id) => sql`${id}`), sql`, `)}]::uuid[]`})`);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -118,11 +118,11 @@ export async function upsertParticipantEV(
|
|||
// Check if record exists
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(participantExpectedValues)
|
||||
.from(seasonParticipantExpectedValues)
|
||||
.where(
|
||||
and(
|
||||
eq(participantExpectedValues.participantId, participantId),
|
||||
eq(participantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
eq(seasonParticipantExpectedValues.participantId, participantId),
|
||||
eq(seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
|
@ -134,7 +134,7 @@ export async function upsertParticipantEV(
|
|||
if (existing.length > 0) {
|
||||
// Update existing
|
||||
const updated = await db
|
||||
.update(participantExpectedValues)
|
||||
.update(seasonParticipantExpectedValues)
|
||||
.set({
|
||||
probFirst: probabilities.probFirst.toString(),
|
||||
probSecond: probabilities.probSecond.toString(),
|
||||
|
|
@ -150,14 +150,14 @@ export async function upsertParticipantEV(
|
|||
calculatedAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(participantExpectedValues.id, existing[0].id))
|
||||
.where(eq(seasonParticipantExpectedValues.id, existing[0].id))
|
||||
.returning();
|
||||
|
||||
result = updated[0];
|
||||
} else {
|
||||
// Create new
|
||||
const created = await db
|
||||
.insert(participantExpectedValues)
|
||||
.insert(seasonParticipantExpectedValues)
|
||||
.values({
|
||||
participantId,
|
||||
sportsSeasonId,
|
||||
|
|
@ -180,13 +180,13 @@ export async function upsertParticipantEV(
|
|||
result = created[0];
|
||||
}
|
||||
|
||||
// Sync calculated EV to participants table for draft room ranking
|
||||
// Sync calculated EV to seasonParticipants table for draft room ranking
|
||||
await db
|
||||
.update(participants)
|
||||
.update(seasonParticipants)
|
||||
.set({ expectedValue: expectedValue.toString(), updatedAt: now })
|
||||
.where(eq(participants.id, participantId));
|
||||
.where(eq(seasonParticipants.id, participantId));
|
||||
|
||||
// Recalculate VORP for all participants in this season (replacement level may have shifted)
|
||||
// Recalculate VORP for all seasonParticipants in this season (replacement level may have shifted)
|
||||
await syncVorpForSeason(sportsSeasonId);
|
||||
|
||||
return result;
|
||||
|
|
@ -209,7 +209,7 @@ export async function upsertParticipantEVWithNormalization(
|
|||
|
||||
export async function countAllParticipantEVs(): Promise<number> {
|
||||
const db = database();
|
||||
const result = await db.select({ value: count() }).from(participantExpectedValues);
|
||||
const result = await db.select({ value: count() }).from(seasonParticipantExpectedValues);
|
||||
return result[0].value;
|
||||
}
|
||||
|
||||
|
|
@ -223,11 +223,11 @@ export async function getParticipantEV(
|
|||
const db = database();
|
||||
const result = await db
|
||||
.select()
|
||||
.from(participantExpectedValues)
|
||||
.from(seasonParticipantExpectedValues)
|
||||
.where(
|
||||
and(
|
||||
eq(participantExpectedValues.participantId, participantId),
|
||||
eq(participantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
eq(seasonParticipantExpectedValues.participantId, participantId),
|
||||
eq(seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
|
@ -244,8 +244,8 @@ export async function getAllParticipantEVsForSeason(
|
|||
const db = database();
|
||||
return db
|
||||
.select()
|
||||
.from(participantExpectedValues)
|
||||
.where(eq(participantExpectedValues.sportsSeasonId, sportsSeasonId));
|
||||
.from(seasonParticipantExpectedValues)
|
||||
.where(eq(seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -257,21 +257,21 @@ export async function deleteParticipantEV(
|
|||
): Promise<void> {
|
||||
const db = database();
|
||||
await db
|
||||
.delete(participantExpectedValues)
|
||||
.delete(seasonParticipantExpectedValues)
|
||||
.where(
|
||||
and(
|
||||
eq(participantExpectedValues.participantId, participantId),
|
||||
eq(participantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
eq(seasonParticipantExpectedValues.participantId, participantId),
|
||||
eq(seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
)
|
||||
);
|
||||
|
||||
// Reset this participant's EV and VORP to 0 (no longer ranked)
|
||||
await db
|
||||
.update(participants)
|
||||
.update(seasonParticipants)
|
||||
.set({ expectedValue: "0", vorpValue: "0", updatedAt: new Date() })
|
||||
.where(eq(participants.id, participantId));
|
||||
.where(eq(seasonParticipants.id, participantId));
|
||||
|
||||
// Recalculate VORP for remaining participants — replacement level may have shifted
|
||||
// Recalculate VORP for remaining seasonParticipants — replacement level may have shifted
|
||||
await syncVorpForSeason(sportsSeasonId);
|
||||
}
|
||||
|
||||
|
|
@ -301,12 +301,12 @@ export async function batchUpsertParticipantEVs(
|
|||
const expectedValue = calculateEV(probabilities, scoringRules);
|
||||
|
||||
const existing = await tx
|
||||
.select({ id: participantExpectedValues.id })
|
||||
.from(participantExpectedValues)
|
||||
.select({ id: seasonParticipantExpectedValues.id })
|
||||
.from(seasonParticipantExpectedValues)
|
||||
.where(
|
||||
and(
|
||||
eq(participantExpectedValues.participantId, participantId),
|
||||
eq(participantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
eq(seasonParticipantExpectedValues.participantId, participantId),
|
||||
eq(seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
|
@ -330,24 +330,24 @@ export async function batchUpsertParticipantEVs(
|
|||
|
||||
if (existing.length > 0) {
|
||||
const [updated] = await tx
|
||||
.update(participantExpectedValues)
|
||||
.update(seasonParticipantExpectedValues)
|
||||
// Only overwrite sourceOdds if explicitly provided; preserve existing value otherwise
|
||||
.set(sourceOdds !== undefined ? { ...baseValues, sourceOdds } : baseValues)
|
||||
.where(eq(participantExpectedValues.id, existing[0].id))
|
||||
.where(eq(seasonParticipantExpectedValues.id, existing[0].id))
|
||||
.returning();
|
||||
result = updated;
|
||||
} else {
|
||||
const [created] = await tx
|
||||
.insert(participantExpectedValues)
|
||||
.insert(seasonParticipantExpectedValues)
|
||||
.values({ participantId, sportsSeasonId, ...baseValues, sourceOdds: sourceOdds ?? null })
|
||||
.returning();
|
||||
result = created;
|
||||
}
|
||||
|
||||
await tx
|
||||
.update(participants)
|
||||
.update(seasonParticipants)
|
||||
.set({ expectedValue: expectedValue.toString(), updatedAt: now })
|
||||
.where(eq(participants.id, participantId));
|
||||
.where(eq(seasonParticipants.id, participantId));
|
||||
|
||||
results.push(result);
|
||||
}
|
||||
|
|
@ -361,7 +361,7 @@ export async function batchUpsertParticipantEVs(
|
|||
}
|
||||
|
||||
/**
|
||||
* Save American odds for a batch of participants without touching probabilities or EV.
|
||||
* Save American odds for a batch of seasonParticipants without touching probabilities or EV.
|
||||
* Used by the futures-odds admin page to persist odds before running the full simulation.
|
||||
*/
|
||||
export async function batchSaveSourceOdds(
|
||||
|
|
@ -374,24 +374,24 @@ export async function batchSaveSourceOdds(
|
|||
|
||||
for (const { participantId, sportsSeasonId, sourceOdds } of inputs) {
|
||||
const existing = await tx
|
||||
.select({ id: participantExpectedValues.id })
|
||||
.from(participantExpectedValues)
|
||||
.select({ id: seasonParticipantExpectedValues.id })
|
||||
.from(seasonParticipantExpectedValues)
|
||||
.where(
|
||||
and(
|
||||
eq(participantExpectedValues.participantId, participantId),
|
||||
eq(participantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
eq(seasonParticipantExpectedValues.participantId, participantId),
|
||||
eq(seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
await tx
|
||||
.update(participantExpectedValues)
|
||||
.update(seasonParticipantExpectedValues)
|
||||
.set({ sourceOdds, updatedAt: now })
|
||||
.where(eq(participantExpectedValues.id, existing[0].id));
|
||||
.where(eq(seasonParticipantExpectedValues.id, existing[0].id));
|
||||
} else {
|
||||
// Insert a stub record — probabilities/EV will be filled in by the simulator
|
||||
await tx.insert(participantExpectedValues).values({
|
||||
await tx.insert(seasonParticipantExpectedValues).values({
|
||||
participantId,
|
||||
sportsSeasonId,
|
||||
probFirst: "0",
|
||||
|
|
@ -414,7 +414,7 @@ export async function batchSaveSourceOdds(
|
|||
}
|
||||
|
||||
/**
|
||||
* Persist raw Elo ratings (and optional world rankings) for a batch of participants.
|
||||
* Persist raw Elo ratings (and optional world rankings) for a batch of seasonParticipants.
|
||||
* Used by Elo-based simulators like snooker_bracket and darts_bracket.
|
||||
* Creates stub records if none exist; does not touch probabilities or EV.
|
||||
* Uses INSERT ... ON CONFLICT DO UPDATE to avoid N+1 queries.
|
||||
|
|
@ -427,7 +427,7 @@ export async function batchSaveSourceElos(
|
|||
const now = new Date();
|
||||
|
||||
await db
|
||||
.insert(participantExpectedValues)
|
||||
.insert(seasonParticipantExpectedValues)
|
||||
.values(
|
||||
inputs.map(({ participantId, sportsSeasonId, sourceElo, worldRanking }) => ({
|
||||
participantId,
|
||||
|
|
@ -449,7 +449,7 @@ export async function batchSaveSourceElos(
|
|||
}))
|
||||
)
|
||||
.onConflictDoUpdate({
|
||||
target: [participantExpectedValues.participantId, participantExpectedValues.sportsSeasonId],
|
||||
target: [seasonParticipantExpectedValues.participantId, seasonParticipantExpectedValues.sportsSeasonId],
|
||||
set: {
|
||||
sourceElo: sql`excluded.source_elo`,
|
||||
worldRanking: sql`excluded.world_ranking`,
|
||||
|
|
@ -499,26 +499,26 @@ export async function recalculateEV(
|
|||
const db = database();
|
||||
const now = new Date();
|
||||
const updated = await db
|
||||
.update(participantExpectedValues)
|
||||
.update(seasonParticipantExpectedValues)
|
||||
.set({
|
||||
expectedValue: newEV.toString(),
|
||||
calculatedAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(participantExpectedValues.id, existing.id))
|
||||
.where(eq(seasonParticipantExpectedValues.id, existing.id))
|
||||
.returning();
|
||||
|
||||
// Sync recalculated EV to participants table
|
||||
// Sync recalculated EV to seasonParticipants table
|
||||
await db
|
||||
.update(participants)
|
||||
.update(seasonParticipants)
|
||||
.set({ expectedValue: newEV.toString(), updatedAt: now })
|
||||
.where(eq(participants.id, participantId));
|
||||
.where(eq(seasonParticipants.id, participantId));
|
||||
|
||||
return updated[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate EVs for all participants in a sports season
|
||||
* Recalculate EVs for all seasonParticipants in a sports season
|
||||
* Used when scoring rules change
|
||||
*/
|
||||
export async function recalculateAllEVsForSeason(
|
||||
|
|
|
|||
|
|
@ -2,18 +2,18 @@ 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 ParticipantResult = typeof schema.seasonParticipantResults.$inferSelect;
|
||||
export type ParticipantResultWithParticipant = ParticipantResult & {
|
||||
participant: { id: string; name: string } | null;
|
||||
};
|
||||
export type NewParticipantResult = typeof schema.participantResults.$inferInsert;
|
||||
export type NewParticipantResult = typeof schema.seasonParticipantResults.$inferInsert;
|
||||
|
||||
export async function createParticipantResult(
|
||||
data: NewParticipantResult
|
||||
): Promise<ParticipantResult> {
|
||||
const db = database();
|
||||
const [result] = await db
|
||||
.insert(schema.participantResults)
|
||||
.insert(schema.seasonParticipantResults)
|
||||
.values(data)
|
||||
.returning();
|
||||
return result;
|
||||
|
|
@ -24,7 +24,7 @@ export async function createManyParticipantResults(
|
|||
): Promise<ParticipantResult[]> {
|
||||
const db = database();
|
||||
return await db
|
||||
.insert(schema.participantResults)
|
||||
.insert(schema.seasonParticipantResults)
|
||||
.values(data)
|
||||
.returning();
|
||||
}
|
||||
|
|
@ -33,8 +33,8 @@ export async function findParticipantResultById(
|
|||
id: string
|
||||
): Promise<ParticipantResult | undefined> {
|
||||
const db = database();
|
||||
return await db.query.participantResults.findFirst({
|
||||
where: eq(schema.participantResults.id, id),
|
||||
return await db.query.seasonParticipantResults.findFirst({
|
||||
where: eq(schema.seasonParticipantResults.id, id),
|
||||
with: {
|
||||
participant: true,
|
||||
sportsSeason: {
|
||||
|
|
@ -50,8 +50,8 @@ export async function findParticipantResultByParticipantId(
|
|||
participantId: string
|
||||
): Promise<ParticipantResult | undefined> {
|
||||
const db = database();
|
||||
return await db.query.participantResults.findFirst({
|
||||
where: eq(schema.participantResults.participantId, participantId),
|
||||
return await db.query.seasonParticipantResults.findFirst({
|
||||
where: eq(schema.seasonParticipantResults.participantId, participantId),
|
||||
with: {
|
||||
participant: true,
|
||||
sportsSeason: {
|
||||
|
|
@ -67,8 +67,8 @@ export async function findParticipantResultsBySportsSeasonId(
|
|||
sportsSeasonId: string
|
||||
): Promise<ParticipantResultWithParticipant[]> {
|
||||
const db = database();
|
||||
return await db.query.participantResults.findMany({
|
||||
where: eq(schema.participantResults.sportsSeasonId, sportsSeasonId),
|
||||
return await db.query.seasonParticipantResults.findMany({
|
||||
where: eq(schema.seasonParticipantResults.sportsSeasonId, sportsSeasonId),
|
||||
orderBy: (results, { asc }) => [asc(results.finalPosition)],
|
||||
with: {
|
||||
participant: true,
|
||||
|
|
@ -82,16 +82,16 @@ export async function updateParticipantResult(
|
|||
): Promise<ParticipantResult> {
|
||||
const db = database();
|
||||
const [result] = await db
|
||||
.update(schema.participantResults)
|
||||
.update(schema.seasonParticipantResults)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(schema.participantResults.id, id))
|
||||
.where(eq(schema.seasonParticipantResults.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));
|
||||
await db.delete(schema.seasonParticipantResults).where(eq(schema.seasonParticipantResults.id, id));
|
||||
}
|
||||
|
||||
export async function deleteParticipantResultsBySportsSeasonId(
|
||||
|
|
@ -99,8 +99,8 @@ export async function deleteParticipantResultsBySportsSeasonId(
|
|||
): Promise<void> {
|
||||
const db = database();
|
||||
await db
|
||||
.delete(schema.participantResults)
|
||||
.where(eq(schema.participantResults.sportsSeasonId, sportsSeasonId));
|
||||
.delete(schema.seasonParticipantResults)
|
||||
.where(eq(schema.seasonParticipantResults.sportsSeasonId, sportsSeasonId));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -117,10 +117,10 @@ export async function setParticipantResult(
|
|||
const db = database();
|
||||
|
||||
// Check if result already exists
|
||||
const existing = await db.query.participantResults.findFirst({
|
||||
const existing = await db.query.seasonParticipantResults.findFirst({
|
||||
where: and(
|
||||
eq(schema.participantResults.participantId, participantId),
|
||||
eq(schema.participantResults.sportsSeasonId, sportsSeasonId)
|
||||
eq(schema.seasonParticipantResults.participantId, participantId),
|
||||
eq(schema.seasonParticipantResults.sportsSeasonId, sportsSeasonId)
|
||||
),
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -144,16 +144,16 @@ export async function getOrCreateParticipantQPTotal(
|
|||
) {
|
||||
const db = providedDb || database();
|
||||
|
||||
let total = await db.query.participantQualifyingTotals.findFirst({
|
||||
let total = await db.query.seasonParticipantQualifyingTotals.findFirst({
|
||||
where: and(
|
||||
eq(schema.participantQualifyingTotals.participantId, participantId),
|
||||
eq(schema.participantQualifyingTotals.sportsSeasonId, sportsSeasonId)
|
||||
eq(schema.seasonParticipantQualifyingTotals.participantId, participantId),
|
||||
eq(schema.seasonParticipantQualifyingTotals.sportsSeasonId, sportsSeasonId)
|
||||
),
|
||||
});
|
||||
|
||||
if (!total) {
|
||||
const [created] = await db
|
||||
.insert(schema.participantQualifyingTotals)
|
||||
.insert(schema.seasonParticipantQualifyingTotals)
|
||||
.values({
|
||||
participantId,
|
||||
sportsSeasonId,
|
||||
|
|
@ -184,12 +184,12 @@ export async function addQualifyingPoints(
|
|||
|
||||
// Update with new points (do NOT increment eventsScored here)
|
||||
const [updated] = await db
|
||||
.update(schema.participantQualifyingTotals)
|
||||
.update(schema.seasonParticipantQualifyingTotals)
|
||||
.set({
|
||||
totalQualifyingPoints: (parseFloat(total.totalQualifyingPoints) + pointsToAdd).toString(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(schema.participantQualifyingTotals.id, total.id))
|
||||
.where(eq(schema.seasonParticipantQualifyingTotals.id, total.id))
|
||||
.returning();
|
||||
|
||||
return updated;
|
||||
|
|
@ -208,7 +208,7 @@ export async function recalculateParticipantQP(
|
|||
|
||||
// Get all event results for this participant in this sports season
|
||||
const eventResults = await db.query.eventResults.findMany({
|
||||
where: eq(schema.eventResults.participantId, participantId),
|
||||
where: eq(schema.eventResults.seasonParticipantId, participantId),
|
||||
with: {
|
||||
scoringEvent: true,
|
||||
},
|
||||
|
|
@ -238,13 +238,13 @@ export async function recalculateParticipantQP(
|
|||
|
||||
// Update with recalculated values
|
||||
await db
|
||||
.update(schema.participantQualifyingTotals)
|
||||
.update(schema.seasonParticipantQualifyingTotals)
|
||||
.set({
|
||||
totalQualifyingPoints: totalQP.toString(),
|
||||
eventsScored,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(schema.participantQualifyingTotals.id, total.id));
|
||||
.where(eq(schema.seasonParticipantQualifyingTotals.id, total.id));
|
||||
|
||||
return { totalQP, eventsScored };
|
||||
}
|
||||
|
|
@ -271,9 +271,9 @@ export async function getQPStandings(
|
|||
) {
|
||||
const db = providedDb || database();
|
||||
|
||||
return await db.query.participantQualifyingTotals.findMany({
|
||||
where: eq(schema.participantQualifyingTotals.sportsSeasonId, sportsSeasonId),
|
||||
orderBy: [desc(sql`CAST(${schema.participantQualifyingTotals.totalQualifyingPoints} AS DECIMAL)`)],
|
||||
return await db.query.seasonParticipantQualifyingTotals.findMany({
|
||||
where: eq(schema.seasonParticipantQualifyingTotals.sportsSeasonId, sportsSeasonId),
|
||||
orderBy: [desc(sql`CAST(${schema.seasonParticipantQualifyingTotals.totalQualifyingPoints} AS DECIMAL)`)],
|
||||
with: {
|
||||
participant: true,
|
||||
},
|
||||
|
|
@ -325,9 +325,9 @@ export async function updateFinalRankings(
|
|||
// Update all rankings
|
||||
for (const update of updates) {
|
||||
await db
|
||||
.update(schema.participantQualifyingTotals)
|
||||
.update(schema.seasonParticipantQualifyingTotals)
|
||||
.set({ finalRanking: update.finalRanking, updatedAt: new Date() })
|
||||
.where(eq(schema.participantQualifyingTotals.id, update.id));
|
||||
.where(eq(schema.seasonParticipantQualifyingTotals.id, update.id));
|
||||
}
|
||||
|
||||
return updates;
|
||||
|
|
@ -343,6 +343,6 @@ export async function resetQualifyingPoints(
|
|||
const db = providedDb || database();
|
||||
|
||||
await db
|
||||
.delete(schema.participantQualifyingTotals)
|
||||
.where(eq(schema.participantQualifyingTotals.sportsSeasonId, sportsSeasonId));
|
||||
.delete(schema.seasonParticipantQualifyingTotals)
|
||||
.where(eq(schema.seasonParticipantQualifyingTotals.sportsSeasonId, sportsSeasonId));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -201,8 +201,8 @@ export async function processPlayoffEvent(
|
|||
|
||||
// PHASE 5.3: Handle teams that didn't make the playoffs
|
||||
// Get all participants in the sports season
|
||||
const allParticipants = await db.query.participants.findMany({
|
||||
where: eq(schema.participants.sportsSeasonId, event.sportsSeasonId),
|
||||
const allParticipants = await db.query.seasonParticipants.findMany({
|
||||
where: eq(schema.seasonParticipants.sportsSeasonId, event.sportsSeasonId),
|
||||
});
|
||||
|
||||
// Get all participant IDs that are in ANY playoff match (winners or losers)
|
||||
|
|
@ -222,10 +222,10 @@ export async function processPlayoffEvent(
|
|||
for (const participant of allParticipants) {
|
||||
if (!participantsInBracket.has(participant.id)) {
|
||||
// Check if they already have a result (don't overwrite)
|
||||
const existingResult = await db.query.participantResults.findFirst({
|
||||
const existingResult = await db.query.seasonParticipantResults.findFirst({
|
||||
where: and(
|
||||
eq(schema.participantResults.participantId, participant.id),
|
||||
eq(schema.participantResults.sportsSeasonId, event.sportsSeasonId)
|
||||
eq(schema.seasonParticipantResults.participantId, participant.id),
|
||||
eq(schema.seasonParticipantResults.sportsSeasonId, event.sportsSeasonId)
|
||||
),
|
||||
});
|
||||
|
||||
|
|
@ -506,10 +506,10 @@ async function upsertParticipantResult(
|
|||
db: ReturnType<typeof database>,
|
||||
isPartialScore = false
|
||||
): Promise<number | null> {
|
||||
const existing = await db.query.participantResults.findFirst({
|
||||
const existing = await db.query.seasonParticipantResults.findFirst({
|
||||
where: and(
|
||||
eq(schema.participantResults.participantId, participantId),
|
||||
eq(schema.participantResults.sportsSeasonId, sportsSeasonId)
|
||||
eq(schema.seasonParticipantResults.participantId, participantId),
|
||||
eq(schema.seasonParticipantResults.sportsSeasonId, sportsSeasonId)
|
||||
),
|
||||
});
|
||||
|
||||
|
|
@ -520,16 +520,16 @@ async function upsertParticipantResult(
|
|||
if (!existing.isPartialScore && isPartialScore) return null;
|
||||
|
||||
await db
|
||||
.update(schema.participantResults)
|
||||
.update(schema.seasonParticipantResults)
|
||||
.set({
|
||||
finalPosition,
|
||||
isPartialScore,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(schema.participantResults.id, existing.id));
|
||||
.where(eq(schema.seasonParticipantResults.id, existing.id));
|
||||
return existing.finalPosition ?? 0;
|
||||
} else {
|
||||
await db.insert(schema.participantResults).values({
|
||||
await db.insert(schema.seasonParticipantResults).values({
|
||||
participantId,
|
||||
sportsSeasonId,
|
||||
finalPosition,
|
||||
|
|
@ -651,7 +651,7 @@ export async function processQualifyingEvent(
|
|||
|
||||
// Recalculate totals for all participants from scratch
|
||||
// This is the source of truth - sums all their event_results
|
||||
const participantIds = new Set(results.map(r => r.participantId));
|
||||
const participantIds = new Set(results.map(r => r.seasonParticipantId));
|
||||
for (const participantId of participantIds) {
|
||||
await recalculateParticipantQP(participantId, event.sportsSeasonId, db);
|
||||
}
|
||||
|
|
@ -752,10 +752,10 @@ export async function finalizeQualifyingPoints(
|
|||
if (currentPlacement <= standings.length) {
|
||||
for (let i = currentPlacement - 1; i < standings.length; i++) {
|
||||
const standing = standings[i];
|
||||
const hasResult = await db.query.participantResults.findFirst({
|
||||
const hasResult = await db.query.seasonParticipantResults.findFirst({
|
||||
where: and(
|
||||
eq(schema.participantResults.participantId, standing.participantId),
|
||||
eq(schema.participantResults.sportsSeasonId, sportsSeasonId)
|
||||
eq(schema.seasonParticipantResults.participantId, standing.participantId),
|
||||
eq(schema.seasonParticipantResults.sportsSeasonId, sportsSeasonId)
|
||||
),
|
||||
});
|
||||
|
||||
|
|
@ -1388,11 +1388,11 @@ export async function recalculateAffectedLeagues(
|
|||
.filter((id): id is string => id !== null);
|
||||
const finalizedLoserIds = new Set<string>();
|
||||
if (loserParticipantIds.length > 0) {
|
||||
const loserResults = await db.query.participantResults.findMany({
|
||||
const loserResults = await db.query.seasonParticipantResults.findMany({
|
||||
where: and(
|
||||
inArray(schema.participantResults.participantId, loserParticipantIds),
|
||||
eq(schema.participantResults.sportsSeasonId, sportsSeasonId),
|
||||
eq(schema.participantResults.isPartialScore, false)
|
||||
inArray(schema.seasonParticipantResults.participantId, loserParticipantIds),
|
||||
eq(schema.seasonParticipantResults.sportsSeasonId, sportsSeasonId),
|
||||
eq(schema.seasonParticipantResults.isPartialScore, false)
|
||||
),
|
||||
});
|
||||
for (const r of loserResults) finalizedLoserIds.add(r.participantId);
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ export async function getScoringEventsForSportsSeason(
|
|||
with: {
|
||||
eventResults: {
|
||||
with: {
|
||||
participant: true,
|
||||
seasonParticipant: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -224,7 +224,7 @@ export async function deleteScoringEvent(
|
|||
const results = await db.query.eventResults.findMany({
|
||||
where: eq(schema.eventResults.scoringEventId, eventId),
|
||||
});
|
||||
affectedParticipantIds = results.map((r) => r.participantId);
|
||||
affectedParticipantIds = results.map((r) => r.seasonParticipantId);
|
||||
wasQPProcessed = results.some(
|
||||
(r) => r.qualifyingPointsAwarded && parseFloat(r.qualifyingPointsAwarded) > 0
|
||||
);
|
||||
|
|
@ -233,8 +233,8 @@ export async function deleteScoringEvent(
|
|||
await db.transaction(async (tx) => {
|
||||
if (event.eventType === "playoff_game") {
|
||||
await tx
|
||||
.delete(schema.participantResults)
|
||||
.where(eq(schema.participantResults.sportsSeasonId, event.sportsSeasonId));
|
||||
.delete(schema.seasonParticipantResults)
|
||||
.where(eq(schema.seasonParticipantResults.sportsSeasonId, event.sportsSeasonId));
|
||||
}
|
||||
// Cascades: playoffMatches, eventResults, tournamentGroups/Members
|
||||
await tx.delete(schema.scoringEvents).where(eq(schema.scoringEvents.id, eventId));
|
||||
|
|
|
|||
|
|
@ -211,8 +211,8 @@ export async function cloneSportsSeason(
|
|||
};
|
||||
|
||||
// Read all source data before writing anything
|
||||
const sourceParticipants = await db.query.participants.findMany({
|
||||
where: eq(schema.participants.sportsSeasonId, sourceId),
|
||||
const sourceParticipants = await db.query.seasonParticipants.findMany({
|
||||
where: eq(schema.seasonParticipants.sportsSeasonId, sourceId),
|
||||
});
|
||||
|
||||
const sourceEvents = await db.query.scoringEvents.findMany({
|
||||
|
|
@ -225,8 +225,8 @@ export async function cloneSportsSeason(
|
|||
: null;
|
||||
|
||||
// Read source futures odds and Elo ratings
|
||||
const sourceEvRows = await db.query.participantExpectedValues.findMany({
|
||||
where: eq(schema.participantExpectedValues.sportsSeasonId, sourceId),
|
||||
const sourceEvRows = await db.query.seasonParticipantExpectedValues.findMany({
|
||||
where: eq(schema.seasonParticipantExpectedValues.sportsSeasonId, sourceId),
|
||||
});
|
||||
|
||||
// Build lookup: (externalId ?? name) → source EV, only for rows that have
|
||||
|
|
@ -251,7 +251,7 @@ export async function cloneSportsSeason(
|
|||
const newParticipants =
|
||||
sourceParticipants.length > 0
|
||||
? await tx
|
||||
.insert(schema.participants)
|
||||
.insert(schema.seasonParticipants)
|
||||
.values(
|
||||
sourceParticipants.map((p: typeof sourceParticipants[number]) => ({
|
||||
sportsSeasonId: newSeason.id,
|
||||
|
|
@ -288,7 +288,7 @@ export async function cloneSportsSeason(
|
|||
.filter((r): r is NonNullable<typeof r> => r !== null);
|
||||
|
||||
if (evRows.length > 0) {
|
||||
await tx.insert(schema.participantExpectedValues).values(evRows);
|
||||
await tx.insert(schema.seasonParticipantExpectedValues).values(evRows);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import { database } from "~/database/context";
|
||||
import { participantSurfaceElos, participants } from "~/database/schema";
|
||||
import { seasonParticipantSurfaceElos, seasonParticipants } from "~/database/schema";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
|
||||
export type CourtSurface = "hard" | "clay" | "grass";
|
||||
|
|
@ -38,7 +38,7 @@ export interface SurfaceEloInput {
|
|||
|
||||
/**
|
||||
* Get all surface Elo records for a sports season, joined with participant names.
|
||||
* Returns one record per participant (participants with no Elo record are excluded).
|
||||
* Returns one record per participant (seasonParticipants with no Elo record are excluded).
|
||||
*/
|
||||
export async function getSurfaceElosForSeason(
|
||||
sportsSeasonId: string
|
||||
|
|
@ -46,26 +46,26 @@ export async function getSurfaceElosForSeason(
|
|||
const db = database();
|
||||
const rows = await db
|
||||
.select({
|
||||
id: participantSurfaceElos.id,
|
||||
participantId: participantSurfaceElos.participantId,
|
||||
sportsSeasonId: participantSurfaceElos.sportsSeasonId,
|
||||
worldRanking: participantSurfaceElos.worldRanking,
|
||||
eloHard: participantSurfaceElos.eloHard,
|
||||
eloClay: participantSurfaceElos.eloClay,
|
||||
eloGrass: participantSurfaceElos.eloGrass,
|
||||
updatedAt: participantSurfaceElos.updatedAt,
|
||||
participantName: participants.name,
|
||||
id: seasonParticipantSurfaceElos.id,
|
||||
participantId: seasonParticipantSurfaceElos.participantId,
|
||||
sportsSeasonId: seasonParticipantSurfaceElos.sportsSeasonId,
|
||||
worldRanking: seasonParticipantSurfaceElos.worldRanking,
|
||||
eloHard: seasonParticipantSurfaceElos.eloHard,
|
||||
eloClay: seasonParticipantSurfaceElos.eloClay,
|
||||
eloGrass: seasonParticipantSurfaceElos.eloGrass,
|
||||
updatedAt: seasonParticipantSurfaceElos.updatedAt,
|
||||
participantName: seasonParticipants.name,
|
||||
})
|
||||
.from(participantSurfaceElos)
|
||||
.innerJoin(participants, eq(participantSurfaceElos.participantId, participants.id))
|
||||
.where(eq(participantSurfaceElos.sportsSeasonId, sportsSeasonId))
|
||||
.orderBy(participants.name);
|
||||
.from(seasonParticipantSurfaceElos)
|
||||
.innerJoin(seasonParticipants, eq(seasonParticipantSurfaceElos.participantId, seasonParticipants.id))
|
||||
.where(eq(seasonParticipantSurfaceElos.sportsSeasonId, sportsSeasonId))
|
||||
.orderBy(seasonParticipants.name);
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert surface Elo ratings for a batch of participants.
|
||||
* Upsert surface Elo ratings for a batch of seasonParticipants.
|
||||
* Uses INSERT ... ON CONFLICT DO UPDATE so all three surface columns are
|
||||
* overwritten atomically — the admin always submits all three values.
|
||||
*/
|
||||
|
|
@ -77,7 +77,7 @@ export async function batchUpsertSurfaceElos(
|
|||
const now = new Date();
|
||||
|
||||
await db
|
||||
.insert(participantSurfaceElos)
|
||||
.insert(seasonParticipantSurfaceElos)
|
||||
.values(
|
||||
inputs.map(({ participantId, sportsSeasonId, worldRanking, eloHard, eloClay, eloGrass }) => ({
|
||||
participantId,
|
||||
|
|
@ -90,7 +90,7 @@ export async function batchUpsertSurfaceElos(
|
|||
}))
|
||||
)
|
||||
.onConflictDoUpdate({
|
||||
target: [participantSurfaceElos.participantId, participantSurfaceElos.sportsSeasonId],
|
||||
target: [seasonParticipantSurfaceElos.participantId, seasonParticipantSurfaceElos.sportsSeasonId],
|
||||
set: {
|
||||
worldRanking: sql`excluded.world_ranking`,
|
||||
eloHard: sql`excluded.elo_hard`,
|
||||
|
|
@ -111,14 +111,14 @@ export async function getSurfaceEloMap(
|
|||
const db = database();
|
||||
const rows = await db
|
||||
.select({
|
||||
participantId: participantSurfaceElos.participantId,
|
||||
worldRanking: participantSurfaceElos.worldRanking,
|
||||
eloHard: participantSurfaceElos.eloHard,
|
||||
eloClay: participantSurfaceElos.eloClay,
|
||||
eloGrass: participantSurfaceElos.eloGrass,
|
||||
participantId: seasonParticipantSurfaceElos.participantId,
|
||||
worldRanking: seasonParticipantSurfaceElos.worldRanking,
|
||||
eloHard: seasonParticipantSurfaceElos.eloHard,
|
||||
eloClay: seasonParticipantSurfaceElos.eloClay,
|
||||
eloGrass: seasonParticipantSurfaceElos.eloGrass,
|
||||
})
|
||||
.from(participantSurfaceElos)
|
||||
.where(eq(participantSurfaceElos.sportsSeasonId, sportsSeasonId));
|
||||
.from(seasonParticipantSurfaceElos)
|
||||
.where(eq(seasonParticipantSurfaceElos.sportsSeasonId, sportsSeasonId));
|
||||
|
||||
return new Map(rows.map((r) => [r.participantId, {
|
||||
worldRanking: r.worldRanking,
|
||||
|
|
|
|||
|
|
@ -230,8 +230,8 @@ export async function getRecentTeamScoreEvents(
|
|||
|
||||
const participantNameById = new Map<string, string>();
|
||||
if (allParticipantIds.length > 0) {
|
||||
const participantRows = await db.query.participants.findMany({
|
||||
where: inArray(schema.participants.id, allParticipantIds),
|
||||
const participantRows = await db.query.seasonParticipants.findMany({
|
||||
where: inArray(schema.seasonParticipants.id, allParticipantIds),
|
||||
columns: { id: true, name: true },
|
||||
});
|
||||
for (const p of participantRows) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue