"use server"; import { eq, inArray } from "drizzle-orm"; import { revalidatePath } from "next/cache"; import { requirePermission } from "@/lib/admin/guard"; import { tryRemoveLocalPhotoFile } from "@/lib/admin/photo-files"; import { CameraWeb, db } from "@/lib/db"; import { PERMS } from "@/lib/permissions"; import { logStaffActivity } from "@/lib/services/staff-activity"; export async function deletePhoto(formData: FormData): Promise { const staff = await requirePermission(PERMS.PAGES_EDIT); const id = Number(formData.get("id")); if (!(id > 0)) return; const [row] = await db .select({ id: CameraWeb.id, url: CameraWeb.url }) .from(CameraWeb) .where(eq(CameraWeb.id, id)) .limit(1); if (row) { await db.delete(CameraWeb).where(eq(CameraWeb.id, id)); await tryRemoveLocalPhotoFile(row.url); await logStaffActivity({ staffId: staff.id, action: "photo_delete", description: `Deleted camera photo #${id}`, targetType: "camera_web", targetId: id, }); } revalidatePath("/admin/photos"); revalidatePath("/photos"); } export async function bulkDeletePhotos(formData: FormData): Promise { const staff = await requirePermission(PERMS.PAGES_EDIT); const raw = String(formData.get("ids") ?? ""); const ids = raw .split(",") .map((s) => Number(s.trim())) .filter((n) => Number.isFinite(n) && n > 0); if (ids.length === 0) return; const rows = await db .select({ id: CameraWeb.id, url: CameraWeb.url }) .from(CameraWeb) .where(inArray(CameraWeb.id, ids)); if (rows.length > 0) { await db.delete(CameraWeb).where( inArray( CameraWeb.id, rows.map((r) => r.id), ), ); await Promise.all(rows.map((r) => tryRemoveLocalPhotoFile(r.url))); await logStaffActivity({ staffId: staff.id, action: "photo_bulk_delete", description: `Deleted ${rows.length} camera photo(s)`, targetType: "camera_web", }); } revalidatePath("/admin/photos"); revalidatePath("/photos"); }