mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-20 21:48:33 +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" };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user