feat: Add server-side function getSnapshots to retrieve session snapshots from the database.

This commit is contained in:
Michael committed 2026-02-22 14:39:42 +01:00
1 parent d6e313f945
commit bab862bf5c
1 file changed
+16 -10
+16 -10
View File
@@ -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));
}