"use server"; import config from "@payload-config"; import { getPayload } from "payload"; import { markAllNotificationsReadForUser, markNotificationsReadForUser, } from "@/lib/notifications"; export interface NotificationActionResult { success: boolean; error?: string; } export async function markNotificationsRead(ids: number[]): Promise { try { const payloadConfig = await config; const payload = await getPayload({ config: payloadConfig }); const { headers } = await import("next/headers"); const hdrs = await headers(); const { user } = await payload.auth({ headers: hdrs, canSetHeaders: false }); if (!user) return { success: false, error: "You must be logged in." }; // Read-state is personal to the recipient: this only ever flips the acting // user's own notifications, never another user's (staff oversight included). await markNotificationsReadForUser(payload, Number(user.id), ids); return { success: true }; } catch { return { success: false, error: "Failed to update notifications." }; } } export async function markAllNotificationsRead(): Promise { try { const payloadConfig = await config; const payload = await getPayload({ config: payloadConfig }); const { headers } = await import("next/headers"); const hdrs = await headers(); const { user } = await payload.auth({ headers: hdrs, canSetHeaders: false }); if (!user) return { success: false, error: "You must be logged in." }; // Only the acting user's own unread notifications are marked read. await markAllNotificationsReadForUser(payload, Number(user.id)); return { success: true }; } catch { return { success: false, error: "Failed to update notifications." }; } }