1
0
Fork 0
polaris-task-force/src/app/(frontend)/notifications/actions.ts
Z8MB1E 66e70d3d56 feat(notifications): scope notification read state per recipient
Read state is personal to the recipient. Marking a notification read now
only ever flips the acting user's own rows — never another user's, even
when staff view it via read-all oversight (which would otherwise hide it
from its real owner).

- add /api/notifications/inbox endpoint that enriches each row with
  ownerId, ownerName, and isMine so the bell can label "meant for
  someone else" rows
- move mark-read logic into per-user lib helpers (markNotificationsReadForUser,
  markAllNotificationsReadForUser) and have the actions delegate
- surface ownership in the bell UI and skip marking rows that aren't mine
2026-10-09 13:13:16 -04:00

51 lines
1.8 KiB
TypeScript

"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<NotificationActionResult> {
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<NotificationActionResult> {
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." };
}
}