diff --git a/app/db/models/calendar.server.ts b/app/db/models/calendar.server.ts index 9698abbd4..c2724b2a7 100644 --- a/app/db/models/calendar.server.ts +++ b/app/db/models/calendar.server.ts @@ -222,7 +222,7 @@ const findWinnersByEventIdStm = sql.prepare(` order by "placement" asc `); -export function findWinnersByEventId(eventId: CalendarEvent["id"]) { +export function findResultsByEventId(eventId: CalendarEvent["id"]) { const rows = findWinnersByEventIdStm.all({ eventId }) as Array<{ id: CalendarEventResultTeam["id"]; teamName: CalendarEventResultTeam["name"]; diff --git a/app/db/seed.ts b/app/db/seed.ts index 9a9505c12..7277b15a6 100644 --- a/app/db/seed.ts +++ b/app/db/seed.ts @@ -35,6 +35,7 @@ const basicSeeds = [ patrons, calendarEvents, calendarEventBadges, + calendarEventResults, ]; export function seed() { @@ -320,7 +321,7 @@ function userIdsInRandomOrder() { return sql .prepare(`select "id" from "User" order by random()`) .all() - .map((u) => u.id); + .map((u) => u.id) as number[]; } function calendarEvents() { @@ -439,3 +440,48 @@ function calendarEventBadges() { } } } + +function calendarEventResults() { + let userIds = userIdsInRandomOrder(); + const eventIdsOfPast = new Set( + sql + .prepare( + `select "CalendarEvent"."id" + from "CalendarEvent" + join "CalendarEventDate" on "CalendarEventDate"."eventId" = "CalendarEvent"."id" + where "CalendarEventDate"."startTime" < $startTime` + ) + .all({ startTime: dateToDatabaseTimestamp(new Date()) }) + .map((r) => r.id) + ); + + for (const eventId of eventIdsOfPast) { + if (Math.random() < 0.3) continue; + + db.calendarEvents.upsertReportedScores({ + eventId, + participantCount: faker.datatype.number({ min: 10, max: 250 }), + results: new Array(faker.helpers.arrayElement([1, 1, 2, 3, 3, 3, 8, 8])) + .fill(null) + // eslint-disable-next-line no-loop-func + .map((_, i) => ({ + placement: i + 1, + teamName: capitalize(faker.word.noun()), + players: new Array( + faker.helpers.arrayElement([1, 2, 3, 4, 4, 4, 4, 4, 5, 6]) + ) + .fill(null) + .map(() => { + const withStringName = Math.random() < 0.2; + + return { + name: withStringName ? faker.name.firstName() : null, + userId: withStringName ? null : userIds.pop()!, + }; + }), + })), + }); + + userIds = userIdsInRandomOrder(); + } +} diff --git a/app/routes/calendar/$id/index.tsx b/app/routes/calendar/$id/index.tsx index 08e2e6b80..80f41d05c 100644 --- a/app/routes/calendar/$id/index.tsx +++ b/app/routes/calendar/$id/index.tsx @@ -63,6 +63,7 @@ export const loader = async ({ params, request }: LoaderArgs) => { event, badgePrizes: db.calendarEvents.findBadgesById(parsedParams.id), title: makeTitle([event.name, t("pages.calendar")]), + results: db.calendarEvents.findResultsByEventId(parsedParams.id), }); }; diff --git a/app/routes/calendar/$id/report-winners.tsx b/app/routes/calendar/$id/report-winners.tsx index 4ac3cb892..763dd3afb 100644 --- a/app/routes/calendar/$id/report-winners.tsx +++ b/app/routes/calendar/$id/report-winners.tsx @@ -138,7 +138,7 @@ export const loader = async ({ request, params }: LoaderArgs) => { return json({ name: event.name, participantCount: event.participantCount, - winners: db.calendarEvents.findWinnersByEventId(parsedParams.id), + winners: db.calendarEvents.findResultsByEventId(parsedParams.id), }); };