mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-22 14:38:10 +02:00
feat: Add server-side function getSnapshots to retrieve session snapshots from the database.
This commit is contained in:
1 parent
d6e313f945
commit
bab862bf5c
1 file changed
+16
-10
@@ -6,17 +6,23 @@ import { auth } from "@/lib/auth";
|
|||||||
import { and, desc, eq } from "drizzle-orm";
|
import { and, desc, eq } from "drizzle-orm";
|
||||||
|
|
||||||
export async function getSnapshots(type?: "snapshot" | "export") {
|
export async function getSnapshots(type?: "snapshot" | "export") {
|
||||||
const session = await auth();
|
try {
|
||||||
const userId = session?.user ? Number((session.user as any).id) : null;
|
const session = await auth();
|
||||||
|
const userId = session?.user ? Number((session.user as any).id) : null;
|
||||||
|
|
||||||
let query = db.select().from(sessionSnapshots);
|
let query = db.select().from(sessionSnapshots);
|
||||||
const conditions = [];
|
const conditions = [];
|
||||||
if (userId) conditions.push(eq(sessionSnapshots.userId, userId));
|
if (userId) conditions.push(eq(sessionSnapshots.userId, userId));
|
||||||
if (type) conditions.push(eq(sessionSnapshots.type, type));
|
if (type) conditions.push(eq(sessionSnapshots.type, type));
|
||||||
|
|
||||||
if (conditions.length > 0) {
|
if (conditions.length > 0) {
|
||||||
return query.where(and(...conditions)).orderBy(desc(sessionSnapshots.createdAt));
|
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));
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user