diff --git a/src/repositories/pointLedgerRepository.ts b/src/repositories/pointLedgerRepository.ts index 8906bc4..0cc0985 100644 --- a/src/repositories/pointLedgerRepository.ts +++ b/src/repositories/pointLedgerRepository.ts @@ -1,12 +1,24 @@ -import type { Transaction } from "firebase-admin/firestore"; -import { firestore } from "../firebase"; -import type { PointLedgerEntry } from "../types/points"; +import type {Transaction} from "firebase-admin/firestore"; +import {firestore} from "../firebase"; +import type {PointLedgerEntry} from "../types/points"; function col(uid: string) { return firestore.collection(`users/${uid}/pointLedger`); } -export function createLedgerEntryTx(tx: Transaction, uid: string, entry: PointLedgerEntry) { const ref = col(uid).doc(entry.txId); tx.create(ref, entry); return ref; } -export async function listLedger(uid: string, limit = 20, cursor?: string) { - let q = col(uid).orderBy("createdAt", "desc").limit(Math.min(Math.max(limit, 1), 100)); - if (cursor) { const snap = await col(uid).doc(cursor).get(); if (snap.exists) q = q.startAfter(snap) as typeof q; } - const snap = await q.get(); - return { items: snap.docs.map((d) => ({ id: d.id, ...(d.data() as PointLedgerEntry) })), cursor: snap.docs.length ? snap.docs[snap.docs.length - 1].id : null }; + +export function createLedgerEntryTx(tx: Transaction, uid: string, entry: PointLedgerEntry) { + const ref = col(uid).doc(entry.txId); + tx.create(ref, entry); + return ref; +} + +export async function listLedger(uid: string, limit = 20, cursor?: string) { + let q = col(uid).orderBy("createdAt", "desc").limit(Math.min(Math.max(limit, 1), 100)); + if (cursor) { + const snap = await col(uid).doc(cursor).get(); + if (snap.exists) q = q.startAfter(snap) as typeof q; + } + const snap = await q.get(); + return { + items: snap.docs.map((d) => ({id: d.id, ...(d.data() as PointLedgerEntry)})), + cursor: snap.docs.length ? snap.docs[snap.docs.length - 1].id : null + }; }