import type { Payload, Where } from "payload"; import type { LedgerEntry } from "./boards"; /** * Read helpers for the `minigame-entries` ledger. Shared by the close pipeline * and the leaderboard endpoint so both interpret rows identically. */ interface EntryDoc { user: number | { id: number }; game: string; score: number | null; durationMs: number | null; accuracy: number | null; createdAt: string; } const userIdOf = (user: EntryDoc["user"]): number => (typeof user === "object" ? user.id : user); export const toLedgerEntry = (doc: EntryDoc): LedgerEntry => ({ userId: userIdOf(doc.user), game: doc.game, score: typeof doc.score === "number" ? doc.score : 0, durationMs: typeof doc.durationMs === "number" ? doc.durationMs : 0, accuracy: typeof doc.accuracy === "number" ? doc.accuracy : null, createdAtMs: new Date(doc.createdAt).getTime() || 0, }); /** Fetch every ledger entry matching `where`, following pagination. */ export async function fetchLedgerEntries(payload: Payload, where: Where): Promise { const entries: LedgerEntry[] = []; let page = 1; for (;;) { const res = await payload.find({ collection: "minigame-entries", where, sort: "createdAt", limit: 500, page, depth: 0, overrideAccess: true, }); for (const doc of res.docs) entries.push(toLedgerEntry(doc as unknown as EntryDoc)); if (!res.hasNextPage || page >= 500) break; page += 1; } return entries; }