From bab862bf5cba90b396981763d2285aca1ec54182 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 22 Feb 2026 14:39:42 +0100 Subject: [PATCH] feat: Add server-side function `getSnapshots` to retrieve session snapshots from the database. --- src/features/snapshots/api/get-snapshots.ts | 26 +++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/features/snapshots/api/get-snapshots.ts b/src/features/snapshots/api/get-snapshots.ts index 288a34e..b17db7e 100644 --- a/src/features/snapshots/api/get-snapshots.ts +++ b/src/features/snapshots/api/get-snapshots.ts @@ -6,17 +6,23 @@ import { auth } from "@/lib/auth"; import { and, desc, eq } from "drizzle-orm"; export async function getSnapshots(type?: "snapshot" | "export") { - const session = await auth(); - const userId = session?.user ? Number((session.user as any).id) : null; + try { + const session = await auth(); + const userId = session?.user ? Number((session.user as any).id) : null; - let query = db.select().from(sessionSnapshots); - const conditions = []; - if (userId) conditions.push(eq(sessionSnapshots.userId, userId)); - if (type) conditions.push(eq(sessionSnapshots.type, type)); + let query = db.select().from(sessionSnapshots); + const conditions = []; + if (userId) conditions.push(eq(sessionSnapshots.userId, userId)); + if (type) conditions.push(eq(sessionSnapshots.type, type)); - if (conditions.length > 0) { - return query.where(and(...conditions)).orderBy(desc(sessionSnapshots.createdAt)); + if (conditions.length > 0) { + return query.where(and(...conditions)).orderBy(desc(sessionSnapshots.createdAt)); + } + + return query.orderBy(desc(sessionSnapshots.createdAt)); + } catch (err: any) { + const msg = (err?.message || String(err)).split("\n")[0]; + console.error(`[getSnapshots] ERROR (type=${type}):`, msg, err); + return []; } - - return query.orderBy(desc(sessionSnapshots.createdAt)); }