mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-19 13:08:11 +02:00
fix: snapshot deletion, restore grid logic and add diagnostic API
This commit is contained in:
1 parent
5eb3f6732e
commit
8237cf2d9f
3 files changed
+83
-30
No files matched your search
@@ -7,18 +7,31 @@ import { auth } from "@/lib/auth";
|
||||
|
||||
export async function deleteSnapshot(id: number) {
|
||||
const session = await auth();
|
||||
const userId = session?.user ? Number((session.user as any).id) : null;
|
||||
const rawUserId = (session?.user as any)?.id;
|
||||
const userId = rawUserId ? parseInt(String(rawUserId), 10) : null;
|
||||
|
||||
if (!userId) {
|
||||
console.log(`[deleteSnapshot] Request for ID: ${id}, Session UserId: ${userId} (raw: ${rawUserId})`);
|
||||
|
||||
if (!userId || isNaN(userId)) {
|
||||
console.error("[deleteSnapshot] Unauthorized: userId is missing or invalid");
|
||||
return { success: false, error: "Non autorisé" };
|
||||
}
|
||||
|
||||
try {
|
||||
await db.delete(sessionSnapshots)
|
||||
.where(and(eq(sessionSnapshots.id, id), eq(sessionSnapshots.userId, userId)));
|
||||
const result = await db.delete(sessionSnapshots)
|
||||
.where(and(eq(sessionSnapshots.id, id), eq(sessionSnapshots.userId, userId)))
|
||||
.returning({ deletedId: sessionSnapshots.id });
|
||||
|
||||
if (result.length === 0) {
|
||||
console.warn(`[deleteSnapshot] No rows deleted for ID ${id} and User ${userId}. Maybe the snapshot doesn't belong to the user?`);
|
||||
// We return success: false to trigger the error modal in UI if nothing was actually deleted
|
||||
return { success: false, error: "Élément non trouvé ou non autorisé" };
|
||||
}
|
||||
|
||||
console.log(`[deleteSnapshot] Successfully deleted ID: ${id}`);
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return { success: false, error: "Delete failed" };
|
||||
} catch (err: any) {
|
||||
console.error("[deleteSnapshot] DB Error:", err);
|
||||
return { success: false, error: err.message || "Delete failed" };
|
||||
}
|
||||
}
|
||||
@@ -8,18 +8,21 @@ import { and, desc, eq } from "drizzle-orm";
|
||||
export async function getSnapshots(type?: "snapshot" | "export") {
|
||||
try {
|
||||
const session = await auth();
|
||||
const userId = session?.user ? Number((session.user as any).id) : null;
|
||||
const rawUserId = (session?.user as any)?.id;
|
||||
const userId = rawUserId ? parseInt(String(rawUserId), 10) : 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));
|
||||
|
||||
if (conditions.length > 0) {
|
||||
return query.where(and(...conditions)).orderBy(desc(sessionSnapshots.createdAt));
|
||||
if (!userId || isNaN(userId)) {
|
||||
console.warn("[getSnapshots] Unauthenticated access attempt");
|
||||
return [];
|
||||
}
|
||||
|
||||
return query.orderBy(desc(sessionSnapshots.createdAt));
|
||||
const conditions = [eq(sessionSnapshots.userId, userId)];
|
||||
if (type) conditions.push(eq(sessionSnapshots.type, type));
|
||||
|
||||
return await db.select()
|
||||
.from(sessionSnapshots)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(sessionSnapshots.createdAt));
|
||||
} catch (err: any) {
|
||||
const msg = (err?.message || String(err)).split("\n")[0];
|
||||
console.error(`[getSnapshots] ERROR (type=${type}):`, msg, err);
|
||||
|
||||
Reference in new issue
Block a user