2025-10-28 23:50:50 -07:00
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
2026-03-12 10:12:38 -07:00
|
|
|
import { eq, and, desc, asc, gte, lte, or, inArray, isNotNull } from "drizzle-orm";
|
2026-03-15 21:52:47 -07:00
|
|
|
import type { BracketRegion } from "~/lib/bracket-templates";
|
2026-03-10 21:47:47 -07:00
|
|
|
import { recalculateAffectedLeagues } from "./scoring-calculator";
|
|
|
|
|
import { recalculateParticipantQP } from "./qualifying-points";
|
2025-10-28 23:50:50 -07:00
|
|
|
|
2026-03-07 21:59:29 -08:00
|
|
|
export type EventType = "playoff_game" | "major_tournament" | "final_standings" | "schedule_event";
|
|
|
|
|
|
|
|
|
|
export function getEventTypeLabel(eventType: string): string {
|
|
|
|
|
switch (eventType) {
|
|
|
|
|
case "playoff_game": return "Bracket";
|
|
|
|
|
case "major_tournament": return "Major Tournament";
|
|
|
|
|
case "final_standings": return "Final Standings";
|
|
|
|
|
case "schedule_event": return "Non-Scoring";
|
|
|
|
|
default: return eventType;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-28 23:50:50 -07:00
|
|
|
|
|
|
|
|
export interface CreateScoringEventData {
|
|
|
|
|
sportsSeasonId: string;
|
|
|
|
|
name: string;
|
|
|
|
|
eventDate?: Date;
|
2026-03-15 10:22:42 -07:00
|
|
|
eventStartsAt?: Date;
|
2025-10-28 23:50:50 -07:00
|
|
|
eventType: EventType;
|
|
|
|
|
isQualifyingEvent?: boolean;
|
2025-11-03 13:16:37 -08:00
|
|
|
// Template system fields (Phase 2.6)
|
|
|
|
|
bracketTemplateId?: string;
|
|
|
|
|
scoringStartsAtRound?: string;
|
2025-10-28 23:50:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UpdateScoringEventData {
|
|
|
|
|
name?: string;
|
2026-03-15 10:22:42 -07:00
|
|
|
eventDate?: Date | null;
|
|
|
|
|
eventStartsAt?: Date | null;
|
2025-10-28 23:50:50 -07:00
|
|
|
isComplete?: boolean;
|
|
|
|
|
completedAt?: Date;
|
2025-11-03 13:16:37 -08:00
|
|
|
bracketTemplateId?: string;
|
|
|
|
|
scoringStartsAtRound?: string;
|
2026-03-15 21:52:47 -07:00
|
|
|
/** Per-event region config for NCAA-style brackets (overrides template defaults) */
|
|
|
|
|
bracketRegionConfig?: BracketRegion[] | null;
|
2025-10-28 23:50:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new scoring event
|
|
|
|
|
*/
|
|
|
|
|
export async function createScoringEvent(
|
|
|
|
|
data: CreateScoringEventData,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
const [event] = await db
|
|
|
|
|
.insert(schema.scoringEvents)
|
|
|
|
|
.values({
|
|
|
|
|
sportsSeasonId: data.sportsSeasonId,
|
|
|
|
|
name: data.name,
|
|
|
|
|
eventDate: data.eventDate ? data.eventDate.toISOString().split('T')[0] : undefined,
|
2026-03-15 10:22:42 -07:00
|
|
|
eventStartsAt: data.eventStartsAt,
|
2025-10-28 23:50:50 -07:00
|
|
|
eventType: data.eventType,
|
|
|
|
|
isQualifyingEvent: data.isQualifyingEvent || false,
|
2025-11-03 13:16:37 -08:00
|
|
|
bracketTemplateId: data.bracketTemplateId,
|
|
|
|
|
scoringStartsAtRound: data.scoringStartsAtRound,
|
2025-10-28 23:50:50 -07:00
|
|
|
isComplete: false,
|
|
|
|
|
})
|
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
|
|
return event;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a scoring event by ID
|
|
|
|
|
*/
|
|
|
|
|
export async function getScoringEventById(
|
|
|
|
|
eventId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
return await db.query.scoringEvents.findFirst({
|
|
|
|
|
where: eq(schema.scoringEvents.id, eventId),
|
|
|
|
|
with: {
|
|
|
|
|
sportsSeason: {
|
|
|
|
|
with: {
|
|
|
|
|
sport: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all scoring events for a sports season
|
|
|
|
|
*/
|
|
|
|
|
export async function getScoringEventsForSportsSeason(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
return await db.query.scoringEvents.findMany({
|
|
|
|
|
where: eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
2026-03-15 10:22:42 -07:00
|
|
|
orderBy: [desc(schema.scoringEvents.eventDate), desc(schema.scoringEvents.eventStartsAt), desc(schema.scoringEvents.createdAt)],
|
2025-10-28 23:50:50 -07:00
|
|
|
with: {
|
|
|
|
|
eventResults: {
|
|
|
|
|
with: {
|
|
|
|
|
participant: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get incomplete scoring events for a sports season
|
|
|
|
|
*/
|
|
|
|
|
export async function getIncompleteScoringEvents(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
return await db.query.scoringEvents.findMany({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
eq(schema.scoringEvents.isComplete, false)
|
|
|
|
|
),
|
|
|
|
|
orderBy: [desc(schema.scoringEvents.eventDate), desc(schema.scoringEvents.createdAt)],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get qualifying events for a sports season
|
|
|
|
|
*/
|
|
|
|
|
export async function getQualifyingEvents(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
return await db.query.scoringEvents.findMany({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
eq(schema.scoringEvents.isQualifyingEvent, true)
|
|
|
|
|
),
|
|
|
|
|
orderBy: [desc(schema.scoringEvents.eventDate), desc(schema.scoringEvents.createdAt)],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update a scoring event
|
|
|
|
|
*/
|
|
|
|
|
export async function updateScoringEvent(
|
|
|
|
|
eventId: string,
|
|
|
|
|
data: UpdateScoringEventData,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
2026-03-21 09:44:05 -07:00
|
|
|
const updateData: Partial<typeof schema.scoringEvents.$inferInsert> = { updatedAt: new Date() };
|
2025-10-28 23:50:50 -07:00
|
|
|
|
|
|
|
|
if (data.name !== undefined) updateData.name = data.name;
|
|
|
|
|
if (data.eventDate !== undefined) {
|
2026-03-15 10:22:42 -07:00
|
|
|
updateData.eventDate = data.eventDate ? data.eventDate.toISOString().split('T')[0] : null;
|
2025-10-28 23:50:50 -07:00
|
|
|
}
|
2026-03-15 10:22:42 -07:00
|
|
|
if (data.eventStartsAt !== undefined) updateData.eventStartsAt = data.eventStartsAt;
|
2025-10-28 23:50:50 -07:00
|
|
|
if (data.isComplete !== undefined) updateData.isComplete = data.isComplete;
|
|
|
|
|
if (data.completedAt !== undefined) updateData.completedAt = data.completedAt;
|
2025-11-03 13:16:37 -08:00
|
|
|
if (data.bracketTemplateId !== undefined) updateData.bracketTemplateId = data.bracketTemplateId;
|
|
|
|
|
if (data.scoringStartsAtRound !== undefined) updateData.scoringStartsAtRound = data.scoringStartsAtRound;
|
2026-03-15 21:52:47 -07:00
|
|
|
if (data.bracketRegionConfig !== undefined) updateData.bracketRegionConfig = data.bracketRegionConfig;
|
2025-10-28 23:50:50 -07:00
|
|
|
|
|
|
|
|
const [updated] = await db
|
|
|
|
|
.update(schema.scoringEvents)
|
|
|
|
|
.set(updateData)
|
|
|
|
|
.where(eq(schema.scoringEvents.id, eventId))
|
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mark a scoring event as complete
|
|
|
|
|
*/
|
|
|
|
|
export async function completeScoringEvent(
|
|
|
|
|
eventId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
return await updateScoringEvent(
|
|
|
|
|
eventId,
|
|
|
|
|
{
|
|
|
|
|
isComplete: true,
|
|
|
|
|
completedAt: new Date(),
|
|
|
|
|
},
|
|
|
|
|
db
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-10 21:47:47 -07:00
|
|
|
* Delete a scoring event.
|
|
|
|
|
* For bracket events (playoff_game), also clears all participant results for the
|
|
|
|
|
* sports season (placements are stored there with no FK back to the event) and
|
|
|
|
|
* recalculates league standings.
|
2025-10-28 23:50:50 -07:00
|
|
|
*/
|
|
|
|
|
export async function deleteScoringEvent(
|
|
|
|
|
eventId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
2026-03-10 21:47:47 -07:00
|
|
|
const event = await db.query.scoringEvents.findFirst({
|
|
|
|
|
where: eq(schema.scoringEvents.id, eventId),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!event) return;
|
|
|
|
|
|
|
|
|
|
// For qualifying events: capture affected participant IDs before the cascade deletes
|
|
|
|
|
// eventResults (which is how we know who had QP awarded from this event).
|
|
|
|
|
let affectedParticipantIds: string[] = [];
|
|
|
|
|
let wasQPProcessed = false;
|
|
|
|
|
if (event.isQualifyingEvent) {
|
|
|
|
|
const results = await db.query.eventResults.findMany({
|
|
|
|
|
where: eq(schema.eventResults.scoringEventId, eventId),
|
|
|
|
|
});
|
|
|
|
|
affectedParticipantIds = results.map((r) => r.participantId);
|
|
|
|
|
wasQPProcessed = results.some(
|
|
|
|
|
(r) => r.qualifyingPointsAwarded && parseFloat(r.qualifyingPointsAwarded) > 0
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.transaction(async (tx) => {
|
|
|
|
|
if (event.eventType === "playoff_game") {
|
|
|
|
|
await tx
|
|
|
|
|
.delete(schema.participantResults)
|
|
|
|
|
.where(eq(schema.participantResults.sportsSeasonId, event.sportsSeasonId));
|
|
|
|
|
}
|
|
|
|
|
// Cascades: playoffMatches, eventResults, tournamentGroups/Members
|
|
|
|
|
await tx.delete(schema.scoringEvents).where(eq(schema.scoringEvents.id, eventId));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (event.eventType === "playoff_game") {
|
|
|
|
|
await recalculateAffectedLeagues(event.sportsSeasonId, db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.isQualifyingEvent && affectedParticipantIds.length > 0) {
|
|
|
|
|
// eventResults have been cascade-deleted; recalculate QP totals from remaining events
|
|
|
|
|
for (const participantId of affectedParticipantIds) {
|
|
|
|
|
await recalculateParticipantQP(participantId, event.sportsSeasonId, db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decrement majorsCompleted if this event had already been processed
|
|
|
|
|
if (wasQPProcessed) {
|
|
|
|
|
const sportsSeason = await db.query.sportsSeasons.findFirst({
|
|
|
|
|
where: eq(schema.sportsSeasons.id, event.sportsSeasonId),
|
|
|
|
|
});
|
|
|
|
|
if (sportsSeason && (sportsSeason.majorsCompleted ?? 0) > 0) {
|
|
|
|
|
await db
|
|
|
|
|
.update(schema.sportsSeasons)
|
|
|
|
|
.set({ majorsCompleted: (sportsSeason.majorsCompleted ?? 1) - 1, updatedAt: new Date() })
|
|
|
|
|
.where(eq(schema.sportsSeasons.id, event.sportsSeasonId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await recalculateAffectedLeagues(event.sportsSeasonId, db);
|
|
|
|
|
}
|
2025-10-28 23:50:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if all events for a sports season are complete
|
|
|
|
|
*/
|
|
|
|
|
export async function areAllEventsComplete(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
): Promise<boolean> {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
const incompleteEvents = await getIncompleteScoringEvents(sportsSeasonId, db);
|
|
|
|
|
return incompleteEvents.length === 0;
|
|
|
|
|
}
|
2026-03-07 21:59:29 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get upcoming (not yet complete) scoring events for a sports season,
|
|
|
|
|
* ordered by eventDate ascending (soonest first).
|
|
|
|
|
* Events without a date are included last.
|
|
|
|
|
*/
|
|
|
|
|
export async function getUpcomingScoringEvents(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
limit = 5,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
return db.query.scoringEvents.findMany({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
eq(schema.scoringEvents.isComplete, false)
|
|
|
|
|
),
|
2026-03-15 10:22:42 -07:00
|
|
|
orderBy: [asc(schema.scoringEvents.eventDate), asc(schema.scoringEvents.eventStartsAt), asc(schema.scoringEvents.createdAt)],
|
2026-03-07 21:59:29 -08:00
|
|
|
limit,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the single next upcoming event for a sports season (for card display).
|
|
|
|
|
*/
|
|
|
|
|
export async function getNextScoringEvent(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const upcoming = await getUpcomingScoringEvents(sportsSeasonId, 1, providedDb);
|
|
|
|
|
return upcoming[0] || null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get recently completed scoring events for a sports season,
|
|
|
|
|
* ordered by completedAt descending (most recent first).
|
|
|
|
|
*/
|
|
|
|
|
export async function getRecentCompletedEvents(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
limit = 3,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
return db.query.scoringEvents.findMany({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
eq(schema.scoringEvents.isComplete, true)
|
|
|
|
|
),
|
|
|
|
|
orderBy: [desc(schema.scoringEvents.completedAt), desc(schema.scoringEvents.eventDate)],
|
|
|
|
|
limit,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 10:12:38 -07:00
|
|
|
// Scoring patterns where each participant competes in every event.
|
|
|
|
|
// Valid scoringPatternEnum values: playoff_bracket | season_standings | qualifying_points
|
|
|
|
|
const ALL_COMPETE_PATTERNS = new Set(["season_standings", "qualifying_points"]);
|
|
|
|
|
|
|
|
|
|
export interface UpcomingParticipantEvent {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
eventDate: string | null;
|
|
|
|
|
/**
|
|
|
|
|
* Earliest scheduledAt timestamp (ISO string) across individual playoff match
|
|
|
|
|
* games for this event — bracket sports only. Used when eventDate is null.
|
|
|
|
|
*/
|
|
|
|
|
earliestGameTime: string | null;
|
|
|
|
|
/**
|
|
|
|
|
* e.g. "Semifinals Game #1" — derived from playoffMatches.round and
|
|
|
|
|
* playoffMatchGames.gameNumber. null when unavailable or redundant with event name.
|
|
|
|
|
*/
|
|
|
|
|
matchLabel: string | null;
|
|
|
|
|
eventType: string;
|
|
|
|
|
sportsSeasonId: string;
|
|
|
|
|
/** Only the current user's drafted participants involved in this event */
|
|
|
|
|
relevantParticipants: Array<{ id: string; name: string }>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get upcoming scoring events (within the given date window) that involve
|
|
|
|
|
* at least one of the given drafted participants.
|
|
|
|
|
*
|
|
|
|
|
* - Bracket sports (playoff_bracket, ucl_bracket): only events where a
|
|
|
|
|
* participant appears in a playoff match.
|
|
|
|
|
* - All-compete sports (season_standings, qualifying_points, etc.): every
|
|
|
|
|
* upcoming event; all draftedParticipants are attached.
|
|
|
|
|
*
|
|
|
|
|
* Events without a date or that are already complete are excluded.
|
|
|
|
|
*/
|
|
|
|
|
export async function getUpcomingEventsForDraftedParticipants(
|
|
|
|
|
sportsSeasonId: string,
|
|
|
|
|
scoringPattern: string,
|
|
|
|
|
draftedParticipants: Array<{ id: string; name: string }>,
|
|
|
|
|
dateFrom: Date,
|
|
|
|
|
dateTo: Date,
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
): Promise<UpcomingParticipantEvent[]> {
|
|
|
|
|
if (draftedParticipants.length === 0) return [];
|
|
|
|
|
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
const dateFromStr = dateFrom.toISOString().split("T")[0];
|
|
|
|
|
const dateToStr = dateTo.toISOString().split("T")[0];
|
|
|
|
|
const draftedIds = draftedParticipants.map((p) => p.id);
|
|
|
|
|
const draftedMap = new Map(draftedParticipants.map((p) => [p.id, p]));
|
|
|
|
|
|
|
|
|
|
if (ALL_COMPETE_PATTERNS.has(scoringPattern)) {
|
|
|
|
|
const events = await db.query.scoringEvents.findMany({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
eq(schema.scoringEvents.isComplete, false),
|
|
|
|
|
isNotNull(schema.scoringEvents.eventDate),
|
|
|
|
|
gte(schema.scoringEvents.eventDate, dateFromStr),
|
|
|
|
|
lte(schema.scoringEvents.eventDate, dateToStr)
|
|
|
|
|
),
|
|
|
|
|
orderBy: [asc(schema.scoringEvents.eventDate)],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return events.map((e) => ({
|
|
|
|
|
id: e.id,
|
|
|
|
|
name: e.name,
|
|
|
|
|
eventDate: e.eventDate,
|
2026-03-15 10:22:42 -07:00
|
|
|
earliestGameTime: e.eventStartsAt ? e.eventStartsAt.toISOString() : null,
|
2026-03-12 10:12:38 -07:00
|
|
|
matchLabel: null,
|
|
|
|
|
eventType: e.eventType,
|
|
|
|
|
sportsSeasonId: e.sportsSeasonId,
|
|
|
|
|
relevantParticipants: draftedParticipants,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Bracket sports: join through playoff_matches → playoff_match_games to find relevant events.
|
|
|
|
|
// scoringEvent.eventDate may be null — admins often set dates on individual games
|
|
|
|
|
// (playoffMatchGames.scheduledAt) rather than on the event record itself.
|
|
|
|
|
// We include an event if EITHER its eventDate OR any of its game scheduledAt timestamps
|
|
|
|
|
// falls within the window.
|
|
|
|
|
//
|
|
|
|
|
// Use start-of-day for the timestamp lower bound so games that already happened
|
|
|
|
|
// earlier today are still included.
|
|
|
|
|
const dateFromTimestamp = new Date(dateFromStr + "T00:00:00.000Z");
|
|
|
|
|
const dateToTimestamp = new Date(dateToStr + "T23:59:59.999Z");
|
|
|
|
|
|
|
|
|
|
const rows = await db
|
|
|
|
|
.selectDistinct({
|
|
|
|
|
id: schema.scoringEvents.id,
|
|
|
|
|
name: schema.scoringEvents.name,
|
|
|
|
|
eventDate: schema.scoringEvents.eventDate,
|
|
|
|
|
eventType: schema.scoringEvents.eventType,
|
|
|
|
|
sportsSeasonId: schema.scoringEvents.sportsSeasonId,
|
|
|
|
|
participant1Id: schema.playoffMatches.participant1Id,
|
|
|
|
|
participant2Id: schema.playoffMatches.participant2Id,
|
|
|
|
|
round: schema.playoffMatches.round,
|
|
|
|
|
gameNumber: schema.playoffMatchGames.gameNumber,
|
|
|
|
|
scheduledAt: schema.playoffMatchGames.scheduledAt,
|
|
|
|
|
})
|
|
|
|
|
.from(schema.scoringEvents)
|
|
|
|
|
.innerJoin(
|
|
|
|
|
schema.playoffMatches,
|
|
|
|
|
eq(schema.playoffMatches.scoringEventId, schema.scoringEvents.id)
|
|
|
|
|
)
|
|
|
|
|
.leftJoin(
|
|
|
|
|
schema.playoffMatchGames,
|
|
|
|
|
eq(schema.playoffMatchGames.playoffMatchId, schema.playoffMatches.id)
|
|
|
|
|
)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
eq(schema.scoringEvents.isComplete, false),
|
|
|
|
|
or(
|
|
|
|
|
inArray(schema.playoffMatches.participant1Id, draftedIds),
|
|
|
|
|
// participant2Id is nullable UUID — same underlying type as participant1Id;
|
|
|
|
|
// the notNull difference only exists at the TypeScript layer
|
2026-03-21 09:44:05 -07:00
|
|
|
// oxlint-disable-next-line typescript/no-explicit-any -- participant2Id is nullable UUID; notNull difference only exists at the TypeScript layer
|
2026-03-12 10:12:38 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
inArray(schema.playoffMatches.participant2Id as any, draftedIds)
|
|
|
|
|
),
|
|
|
|
|
or(
|
|
|
|
|
// scoringEvent has an explicit date within the window
|
|
|
|
|
and(
|
|
|
|
|
isNotNull(schema.scoringEvents.eventDate),
|
|
|
|
|
gte(schema.scoringEvents.eventDate, dateFromStr),
|
|
|
|
|
lte(schema.scoringEvents.eventDate, dateToStr)
|
|
|
|
|
),
|
|
|
|
|
// Or one of the individual games is scheduled within the window
|
|
|
|
|
and(
|
|
|
|
|
isNotNull(schema.playoffMatchGames.scheduledAt),
|
|
|
|
|
gte(schema.playoffMatchGames.scheduledAt, dateFromTimestamp),
|
|
|
|
|
lte(schema.playoffMatchGames.scheduledAt, dateToTimestamp)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.orderBy(asc(schema.scoringEvents.eventDate));
|
|
|
|
|
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
// Determine max game number per event across ALL games (not date-filtered)
|
|
|
|
|
// so we can distinguish single-game matchups from multi-game series.
|
|
|
|
|
const eventIdsFromRows = [...new Set(rows.map((r) => r.id))];
|
|
|
|
|
const maxGameNumberByEvent = new Map<string, number>();
|
|
|
|
|
if (eventIdsFromRows.length > 0) {
|
|
|
|
|
const allGameRows = await db
|
|
|
|
|
.selectDistinct({
|
|
|
|
|
eventId: schema.scoringEvents.id,
|
|
|
|
|
gameNumber: schema.playoffMatchGames.gameNumber,
|
|
|
|
|
})
|
|
|
|
|
.from(schema.scoringEvents)
|
|
|
|
|
.innerJoin(
|
|
|
|
|
schema.playoffMatches,
|
|
|
|
|
eq(schema.playoffMatches.scoringEventId, schema.scoringEvents.id)
|
|
|
|
|
)
|
|
|
|
|
.innerJoin(
|
|
|
|
|
schema.playoffMatchGames,
|
|
|
|
|
eq(schema.playoffMatchGames.playoffMatchId, schema.playoffMatches.id)
|
|
|
|
|
)
|
|
|
|
|
.where(inArray(schema.scoringEvents.id, eventIdsFromRows));
|
|
|
|
|
|
|
|
|
|
for (const row of allGameRows) {
|
|
|
|
|
if (row.gameNumber !== null) {
|
|
|
|
|
const current = maxGameNumberByEvent.get(row.eventId) ?? 0;
|
|
|
|
|
maxGameNumberByEvent.set(row.eventId, Math.max(current, row.gameNumber));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Group rows into calendar entries.
|
|
|
|
|
// Series events (max game number > 1) get one entry per game so each game
|
|
|
|
|
// appears as its own row on the calendar. Single-game events get one entry.
|
|
|
|
|
const entryMap = new Map<string, UpcomingParticipantEvent>();
|
|
|
|
|
const participantsByEntry = new Map<string, Map<string, { id: string; name: string }>>();
|
|
|
|
|
const gameKeysByEntry = new Map<string, Set<string>>();
|
|
|
|
|
const earliestTimeByEntry = new Map<string, Date>();
|
|
|
|
|
const isSeriesByEntry = new Map<string, boolean>();
|
2026-03-12 10:12:38 -07:00
|
|
|
|
|
|
|
|
for (const row of rows) {
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
const isSeries = (maxGameNumberByEvent.get(row.id) ?? 1) > 1;
|
|
|
|
|
const entryKey =
|
|
|
|
|
isSeries && row.gameNumber !== null ? `${row.id}|${row.gameNumber}` : row.id;
|
|
|
|
|
|
|
|
|
|
if (!entryMap.has(entryKey)) {
|
|
|
|
|
entryMap.set(entryKey, {
|
|
|
|
|
id: entryKey,
|
2026-03-12 10:12:38 -07:00
|
|
|
name: row.name,
|
|
|
|
|
eventDate: row.eventDate,
|
|
|
|
|
earliestGameTime: null,
|
|
|
|
|
matchLabel: null,
|
|
|
|
|
eventType: row.eventType,
|
|
|
|
|
sportsSeasonId: row.sportsSeasonId,
|
|
|
|
|
relevantParticipants: [],
|
|
|
|
|
});
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
participantsByEntry.set(entryKey, new Map());
|
|
|
|
|
gameKeysByEntry.set(entryKey, new Set());
|
|
|
|
|
isSeriesByEntry.set(entryKey, isSeries);
|
2026-03-12 10:12:38 -07:00
|
|
|
}
|
|
|
|
|
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
const pMap = participantsByEntry.get(entryKey);
|
Fix oxlint warnings: no-shadow, consistent-function-scoping, no-non-null-assertion, and others (#196)
* Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix no-non-null-assertion lint violations and promote to error
Eliminates all 208 no-non-null-assertion warnings across 38 files.
Promotes typescript/no-non-null-assertion from warn to error in
.oxlintrc.json.
Fix patterns applied:
- Map.get(key)! after .has() check → extract with get() + null guard
- Map.get(key)! on pre-populated count maps → ?? 0 default
- .set(id, map.get(id)! + 1) increment → ?? 0 before adding
- participant1Id!/participant2Id! on DB matches → ?? "" fallback
- array.find()! in tests → guard + throw or expect().toBeDefined()
- bracketTemplateCache.get(id)! → null guard extract
- Various nullable field accesses → optional chain or ?? default
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix prefer-add-event-listener, no-unassigned-import, require-module-specifiers
Resolves all 9 remaining non-console lint warnings and promotes all
three rules to errors in .oxlintrc.json.
- prefer-add-event-listener: converted onchange/onclick/onload
assignments to addEventListener in useDraftNotifications.ts and
admin.data-sync.tsx; stored changeHandler ref for proper cleanup
with removeEventListener
- no-unassigned-import: configured rule with allow list for legitimate
side-effect imports (*.css, @testing-library/jest-dom,
@testing-library/cypress/add-commands)
- require-module-specifiers: removed redundant `export {}` from
cypress/support/e2e.ts (file already has an import)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix TypeScript errors from no-non-null-assertion fixes
Two fixes introduced by the non-null assertion cleanup produced type
errors:
- scoring-event.ts: `?? ""` was wrong type for a participant object map;
restructured to explicit null guards so TypeScript can narrow correctly
- standings-sync/index.ts: `?? null` after name-match lookup lost the
truthy guarantee, causing TS18047 on the write-back block; added
`participant &&` guard before accessing its properties
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Add npm run typecheck as Stop hook in Claude settings
Runs a full project typecheck at the end of each Claude turn so type
errors surface as feedback before the next message.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 10:59:51 -07:00
|
|
|
if (pMap) {
|
|
|
|
|
if (row.participant1Id) {
|
|
|
|
|
const p1 = draftedMap.get(row.participant1Id);
|
|
|
|
|
if (p1) pMap.set(row.participant1Id, p1);
|
|
|
|
|
}
|
|
|
|
|
if (row.participant2Id) {
|
|
|
|
|
const p2 = draftedMap.get(row.participant2Id);
|
|
|
|
|
if (p2) pMap.set(row.participant2Id, p2);
|
|
|
|
|
}
|
2026-03-12 10:12:38 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-21 09:44:05 -07:00
|
|
|
if (row.round && row.gameNumber !== null) {
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
gameKeysByEntry.get(entryKey)?.add(`${row.round}|${row.gameNumber}`);
|
2026-03-12 10:12:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (row.scheduledAt) {
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
const prev = earliestTimeByEntry.get(entryKey);
|
2026-03-12 10:12:38 -07:00
|
|
|
if (!prev || row.scheduledAt < prev) {
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
earliestTimeByEntry.set(entryKey, row.scheduledAt);
|
2026-03-12 10:12:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
for (const [entryKey, event] of entryMap) {
|
|
|
|
|
event.relevantParticipants = Array.from(participantsByEntry.get(entryKey)?.values() ?? []);
|
2026-03-12 10:12:38 -07:00
|
|
|
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
const earliest = earliestTimeByEntry.get(entryKey);
|
2026-03-12 10:12:38 -07:00
|
|
|
event.earliestGameTime = earliest ? earliest.toISOString() : null;
|
|
|
|
|
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
const isSeries = isSeriesByEntry.get(entryKey) ?? false;
|
|
|
|
|
const gameKeys = gameKeysByEntry.get(entryKey) ?? new Set<string>();
|
|
|
|
|
|
|
|
|
|
if (isSeries) {
|
|
|
|
|
if (gameKeys.size >= 1) {
|
|
|
|
|
const [round, gameNumberStr] = [...gameKeys][0].split("|");
|
|
|
|
|
event.matchLabel =
|
|
|
|
|
round === event.name ? `Game #${gameNumberStr}` : `${round} Game #${gameNumberStr}`;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Single-game: omit the game number, just show the round name.
|
|
|
|
|
if (gameKeys.size === 1) {
|
|
|
|
|
const [round] = [...gameKeys][0].split("|");
|
2026-03-12 10:12:38 -07:00
|
|
|
event.matchLabel = round === event.name ? null : round;
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
} else if (gameKeys.size > 1) {
|
|
|
|
|
const rounds = new Set([...gameKeys].map((k) => k.split("|")[0]));
|
|
|
|
|
if (rounds.size === 1) {
|
|
|
|
|
const round = [...rounds][0];
|
|
|
|
|
event.matchLabel = round === event.name ? null : round;
|
|
|
|
|
}
|
2026-03-12 10:12:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Don't show Game #1 label for single-game matchups in calendar (#206)
* Don't show Game #1 label for single-game matchups in calendar
When a matchup has only one game, showing "Game #1" is redundant and
unhelpful. Only show game numbers when there are multiple games in a
matchup (gameKeys.size > 1). For single-game matchups, show just the
round name (or null if it matches the event name).
Closes #198
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number for Game #2+ in series, hide Game #1 for single-game matchups
If a matchup has only one game (game #1), the number is redundant — show
just the round name. If game #2 or higher is upcoming, it clearly belongs
to a multi-game series, so show the game number to distinguish it.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Fix Game #1 label for multi-game series when only Game #1 is in date window
Previously, Game #1 of a multi-game series showed no game number (e.g.
"Semifinals") if Game #2 fell outside the calendar date range. Since at
least 2 games are always entered for a series from the start, a second
un-date-filtered query now checks the max game number per event. If max
> 1, the event is a series and Game #1 is labelled accordingly
(e.g. "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Show game number when multiple games from same series are in date window
The gameKeys.size > 1 branch previously showed only the round name,
contradicting the rule that multi-game series always show "Game #x".
Now shows the lowest game number visible in the window (e.g. both
Game #1 and #2 upcoming → "Semifinals Game #1").
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Split series games into individual calendar entries, each showing Game #x
Each game in a multi-game bracket series (e.g. Game #1 on Mar 23,
Game #2 on Mar 25) now appears as its own row on the calendar with its
own date and "Round Game #x" label. Single-game matchups are unchanged.
Grouping key changes from eventId to eventId|gameNumber for series events.
The max-game-number lookup now runs before grouping so the correct key
can be determined while processing each row.
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
* Cache isSeries per entry to avoid re-deriving it in the finalization loop
https://claude.ai/code/session_01CRrqu43FKHshpJt1NyrDdX
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-22 16:39:26 -07:00
|
|
|
return Array.from(entryMap.values());
|
2026-03-12 10:12:38 -07:00
|
|
|
}
|
|
|
|
|
|
Add "Games to Score" widget to admin dashboard (#183)
* Add "Games to Score" widget to admin dashboard
Shows scoring events for today and tomorrow in a tabbed card, with
status badges (Pending/Scored), a scored/total progress counter, and
direct "Score" / "View" links to each event's results page. Excludes
non-scoring schedule_event types so only actionable events surface.
- app/models/scoring-event.ts: add getEventsForDates() model function
that queries scoringEvents joined with sportsSeasons/sport for a
given list of date strings
- app/routes/admin._index.tsx: call getEventsForDates([today, tomorrow])
in the loader and render the GamesToScore component below the stats row
- app/models/__tests__/scoring-event-dashboard.test.ts: 8 unit tests
covering empty input, shape mapping, multi-date filtering, completed
events, and the inArray where conditions
https://claude.ai/code/session_014zKK15o5RJfTaodLvnZpF5
* Fix getEventsForDates to include bracket events by game scheduledAt
The previous implementation only matched on scoringEvents.eventDate,
which is often null for bracket events where individual game times are
set on playoffMatchGames.scheduledAt instead.
Switched to a two-step query mirroring getUpcomingEventsForDraftedParticipants:
1. selectDistinct event IDs via left joins through playoffMatches →
playoffMatchGames, matching where eventDate IN dates OR any game's
scheduledAt falls within the date range's timestamp bounds
2. findMany on the matched IDs with sportsSeason + sport relations
Updated tests to cover the two-step flow, leftJoin usage, and the
scheduledAt timestamp bounds appearing in the where clause.
https://claude.ai/code/session_014zKK15o5RJfTaodLvnZpF5
* Document dual game date storage in CLAUDE.md
scoringEvents.eventDate and playoffMatchGames.scheduledAt both hold game
dates depending on the event type. Added a dedicated section under
Important Patterns explaining when each is used and the two-step
selectDistinct + findMany pattern required for correct date queries.
https://claude.ai/code/session_014zKK15o5RJfTaodLvnZpF5
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-19 19:25:37 -07:00
|
|
|
export interface DashboardScoringEvent {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
eventDate: string | null;
|
|
|
|
|
eventStartsAt: Date | null;
|
|
|
|
|
eventType: string;
|
|
|
|
|
isComplete: boolean;
|
|
|
|
|
completedAt: Date | null;
|
|
|
|
|
sportsSeasonId: string;
|
|
|
|
|
sportsSeasonName: string;
|
|
|
|
|
sportName: string;
|
|
|
|
|
sportSlug: string | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all scoring events (excluding schedule_event type) for the given dates,
|
|
|
|
|
* joined with their sports season and sport info. Used for the admin dashboard
|
|
|
|
|
* "Games to Score" widget.
|
|
|
|
|
*
|
|
|
|
|
* An event is included if EITHER its own eventDate falls on one of the given
|
|
|
|
|
* dates, OR any of its playoffMatchGames has a scheduledAt timestamp on one of
|
|
|
|
|
* those dates. This ensures bracket events where the admin set dates on
|
|
|
|
|
* individual games (rather than the event itself) are still surfaced.
|
|
|
|
|
*/
|
|
|
|
|
export async function getEventsForDates(
|
|
|
|
|
dates: string[],
|
|
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
): Promise<DashboardScoringEvent[]> {
|
|
|
|
|
if (dates.length === 0) return [];
|
|
|
|
|
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
// Build timestamp bounds covering the full span of the requested dates so we
|
|
|
|
|
// can range-check the playoffMatchGames.scheduledAt timestamp column.
|
2026-03-21 09:44:05 -07:00
|
|
|
const sortedDates = [...dates].toSorted();
|
Add "Games to Score" widget to admin dashboard (#183)
* Add "Games to Score" widget to admin dashboard
Shows scoring events for today and tomorrow in a tabbed card, with
status badges (Pending/Scored), a scored/total progress counter, and
direct "Score" / "View" links to each event's results page. Excludes
non-scoring schedule_event types so only actionable events surface.
- app/models/scoring-event.ts: add getEventsForDates() model function
that queries scoringEvents joined with sportsSeasons/sport for a
given list of date strings
- app/routes/admin._index.tsx: call getEventsForDates([today, tomorrow])
in the loader and render the GamesToScore component below the stats row
- app/models/__tests__/scoring-event-dashboard.test.ts: 8 unit tests
covering empty input, shape mapping, multi-date filtering, completed
events, and the inArray where conditions
https://claude.ai/code/session_014zKK15o5RJfTaodLvnZpF5
* Fix getEventsForDates to include bracket events by game scheduledAt
The previous implementation only matched on scoringEvents.eventDate,
which is often null for bracket events where individual game times are
set on playoffMatchGames.scheduledAt instead.
Switched to a two-step query mirroring getUpcomingEventsForDraftedParticipants:
1. selectDistinct event IDs via left joins through playoffMatches →
playoffMatchGames, matching where eventDate IN dates OR any game's
scheduledAt falls within the date range's timestamp bounds
2. findMany on the matched IDs with sportsSeason + sport relations
Updated tests to cover the two-step flow, leftJoin usage, and the
scheduledAt timestamp bounds appearing in the where clause.
https://claude.ai/code/session_014zKK15o5RJfTaodLvnZpF5
* Document dual game date storage in CLAUDE.md
scoringEvents.eventDate and playoffMatchGames.scheduledAt both hold game
dates depending on the event type. Added a dedicated section under
Important Patterns explaining when each is used and the two-step
selectDistinct + findMany pattern required for correct date queries.
https://claude.ai/code/session_014zKK15o5RJfTaodLvnZpF5
---------
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-19 19:25:37 -07:00
|
|
|
const dateFromTimestamp = new Date(sortedDates[0] + "T00:00:00.000Z");
|
|
|
|
|
const dateToTimestamp = new Date(sortedDates[sortedDates.length - 1] + "T23:59:59.999Z");
|
|
|
|
|
|
|
|
|
|
// Step 1: collect distinct event IDs where the event date OR any game's
|
|
|
|
|
// scheduledAt falls within the requested dates.
|
|
|
|
|
const rows = await db
|
|
|
|
|
.selectDistinct({ id: schema.scoringEvents.id })
|
|
|
|
|
.from(schema.scoringEvents)
|
|
|
|
|
.leftJoin(
|
|
|
|
|
schema.playoffMatches,
|
|
|
|
|
eq(schema.playoffMatches.scoringEventId, schema.scoringEvents.id)
|
|
|
|
|
)
|
|
|
|
|
.leftJoin(
|
|
|
|
|
schema.playoffMatchGames,
|
|
|
|
|
eq(schema.playoffMatchGames.playoffMatchId, schema.playoffMatches.id)
|
|
|
|
|
)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
inArray(schema.scoringEvents.eventType, [
|
|
|
|
|
"playoff_game",
|
|
|
|
|
"major_tournament",
|
|
|
|
|
"final_standings",
|
|
|
|
|
]),
|
|
|
|
|
or(
|
|
|
|
|
inArray(schema.scoringEvents.eventDate, dates),
|
|
|
|
|
and(
|
|
|
|
|
isNotNull(schema.playoffMatchGames.scheduledAt),
|
|
|
|
|
gte(schema.playoffMatchGames.scheduledAt, dateFromTimestamp),
|
|
|
|
|
lte(schema.playoffMatchGames.scheduledAt, dateToTimestamp)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (rows.length === 0) return [];
|
|
|
|
|
|
|
|
|
|
const eventIds = rows.map((r) => r.id);
|
|
|
|
|
|
|
|
|
|
// Step 2: fetch the matched events with sport season + sport info.
|
|
|
|
|
const events = await db.query.scoringEvents.findMany({
|
|
|
|
|
where: inArray(schema.scoringEvents.id, eventIds),
|
|
|
|
|
orderBy: [
|
|
|
|
|
asc(schema.scoringEvents.eventDate),
|
|
|
|
|
asc(schema.scoringEvents.eventStartsAt),
|
|
|
|
|
asc(schema.scoringEvents.createdAt),
|
|
|
|
|
],
|
|
|
|
|
with: {
|
|
|
|
|
sportsSeason: {
|
|
|
|
|
with: {
|
|
|
|
|
sport: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return events.map((e) => ({
|
|
|
|
|
id: e.id,
|
|
|
|
|
name: e.name,
|
|
|
|
|
eventDate: e.eventDate,
|
|
|
|
|
eventStartsAt: e.eventStartsAt,
|
|
|
|
|
eventType: e.eventType,
|
|
|
|
|
isComplete: e.isComplete,
|
|
|
|
|
completedAt: e.completedAt,
|
|
|
|
|
sportsSeasonId: e.sportsSeasonId,
|
|
|
|
|
sportsSeasonName: e.sportsSeason.name,
|
|
|
|
|
sportName: e.sportsSeason.sport.name,
|
|
|
|
|
sportSlug: e.sportsSeason.sport.slug,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 21:59:29 -08:00
|
|
|
/**
|
|
|
|
|
* Bulk create scoring events for a sports season.
|
|
|
|
|
* Returns created events in order.
|
|
|
|
|
*/
|
|
|
|
|
export async function bulkCreateScoringEvents(
|
|
|
|
|
sportsSeasonId: string,
|
2026-03-15 10:22:42 -07:00
|
|
|
events: Array<{ name: string; eventDate?: Date; eventStartsAt?: Date; eventType: EventType; isQualifyingEvent?: boolean }>,
|
2026-03-07 21:59:29 -08:00
|
|
|
providedDb?: ReturnType<typeof database>
|
|
|
|
|
) {
|
|
|
|
|
const db = providedDb || database();
|
|
|
|
|
|
|
|
|
|
if (events.length === 0) return [];
|
|
|
|
|
|
|
|
|
|
const rows = events.map((e) => ({
|
|
|
|
|
sportsSeasonId,
|
|
|
|
|
name: e.name,
|
|
|
|
|
eventDate: e.eventDate ? e.eventDate.toISOString().split("T")[0] : undefined,
|
2026-03-15 10:22:42 -07:00
|
|
|
eventStartsAt: e.eventStartsAt,
|
2026-03-07 21:59:29 -08:00
|
|
|
eventType: e.eventType,
|
|
|
|
|
isQualifyingEvent: e.isQualifyingEvent ?? false,
|
|
|
|
|
isComplete: false,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return db.insert(schema.scoringEvents).values(rows).returning();
|
|
|
|
|
}
|