diff --git a/src/app/(frontend)/notifications/actions.ts b/src/app/(frontend)/notifications/actions.ts
index 24578357..a7f03f39 100644
--- a/src/app/(frontend)/notifications/actions.ts
+++ b/src/app/(frontend)/notifications/actions.ts
@@ -3,6 +3,11 @@
import config from "@payload-config";
import { getPayload } from "payload";
+import {
+ markAllNotificationsReadForUser,
+ markNotificationsReadForUser,
+} from "@/lib/notifications";
+
export interface NotificationActionResult {
success: boolean;
error?: string;
@@ -18,29 +23,9 @@ export async function markNotificationsRead(ids: number[]): Promise Number.isFinite(id) && id > 0))];
- if (cleanIds.length === 0) return { success: true };
-
- // Query with access control so users can only touch their own notifications.
- const found = await payload.find({
- collection: "user-notifications",
- where: { id: { in: cleanIds } },
- limit: Math.min(cleanIds.length, 100),
- depth: 0,
- });
-
- for (const doc of found.docs) {
- if (!doc.read) {
- await payload.update({
- collection: "user-notifications",
- id: doc.id,
- data: { read: true },
- overrideAccess: true,
- depth: 0,
- });
- }
- }
-
+ // 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." };
@@ -57,23 +42,8 @@ export async function markAllNotificationsRead(): Promise {
+ const owner = doc.user;
+ const ownerId = typeof owner === "object" && owner !== null ? owner.id : (owner as number);
+ const isMine = Number(ownerId) === userId;
+ return {
+ id: doc.id,
+ title: doc.title,
+ message: doc.message ?? null,
+ link: doc.link ?? null,
+ type: doc.type ?? null,
+ read: Boolean(doc.read),
+ createdAt: doc.createdAt,
+ ownerId: Number(ownerId),
+ ownerName: typeof owner === "object" && owner !== null ? (owner.username ?? "") : "",
+ isMine,
+ };
+ });
+
+ return NextResponse.json({ notifications });
+ } catch (error) {
+ console.error("notifications/inbox: failed to load", error);
+ return NextResponse.json({ error: "Failed to load notifications." }, { status: 500 });
+ }
+}
diff --git a/src/components/frontend/notifications/NotificationsBell.tsx b/src/components/frontend/notifications/NotificationsBell.tsx
index 85594401..69b8fdb2 100644
--- a/src/components/frontend/notifications/NotificationsBell.tsx
+++ b/src/components/frontend/notifications/NotificationsBell.tsx
@@ -29,6 +29,14 @@ interface NotificationItem {
link?: string | null;
read: boolean;
createdAt: string;
+ /**
+ * Whether this notification belongs to the current user. When false the
+ * current user (staff with read-all) is viewing someone else's notification;
+ * it can be shown and opened but its read-state must never be changed.
+ */
+ isMine?: boolean;
+ /** Display name of the user this notification is actually meant for. Only set when `!isMine`. */
+ ownerName?: string | null;
}
function timeAgo(iso: string): string {
@@ -58,12 +66,12 @@ export function NotificationsBell() {
const fetchNotifications = useCallback(async () => {
try {
- const res = await fetch("/api/user-notifications?limit=12&sort=-createdAt", {
+ const res = await fetch("/api/notifications/inbox?limit=12&sort=-createdAt", {
cache: "no-store",
});
if (!res.ok) throw new Error("Failed to fetch");
- const data = (await res.json()) as { docs?: NotificationItem[] };
- setNotifications(data.docs ?? []);
+ const data = (await res.json()) as { notifications?: NotificationItem[] };
+ setNotifications(data.notifications ?? []);
setError(false);
} catch {
setError(true);
@@ -83,8 +91,11 @@ export function NotificationsBell() {
};
}, [fetchNotifications]);
+ // Read-state is personal to the recipient. Never let marking one change
+ // another user's read flag — staff are viewing it as an oversight, not as
+ // that user, so flipping it would hide the notification from them.
async function handleMarkOne(item: NotificationItem) {
- if (item.read || marking) return;
+ if (item.read || marking || item.isMine === false) return;
setMarking(true);
setActionError(null);
try {
@@ -104,7 +115,9 @@ export function NotificationsBell() {
}
async function handleItemClick(item: NotificationItem) {
- if (!item.read) await handleMarkOne(item);
+ // Own notifications are marked read on open; others' are only opened, never
+ // marked, so the real owner still sees them as unread.
+ if (!item.read && item.isMine !== false) await handleMarkOne(item);
if (item.link) {
router.push(item.link);
setOpen(false);
@@ -118,7 +131,11 @@ export function NotificationsBell() {
try {
const result = await markAllNotificationsRead();
if (result.success) {
- setNotifications((prev) => prev.map((n) => (n.read ? n : { ...n, read: true })));
+ // Only own rows are guaranteed to have been flipped, so only optimistic-
+ // update those. Staff viewing others' rows must not see them flip to read.
+ setNotifications((prev) =>
+ prev.map((n) => (n.isMine === false ? n : { ...n, read: true })),
+ );
} else {
setActionError(result.error ?? "Failed to update notifications.");
}
@@ -153,7 +170,7 @@ export function NotificationsBell() {
)}
- {notifications.map((item) => (
- void handleItemClick(item)}
- >
-
-
- {!item.read && (
-
- )}
- {item.title}
-
-
- {!item.read && (
+ {notifications.map((item) => {
+ const isUnread = !item.read;
+ const isForeign = item.isMine === false;
+ return (
+ void handleItemClick(item)}
+ >
+
+
+ {!isForeign && isUnread && (
+
+ )}
+ {item.title}
+
+ {!isForeign && item.read && (
)}
-
- {timeAgo(item.createdAt)}
+
+
+ {timeAgo(item.createdAt)}
+
-
- {item.message && (
-
- {item.message}
+ {isForeign && item.ownerName && (
+
+ meant for {item.ownerName}
+
+ )}
+ {item.message && (
+
+ {item.message}
+
+ )}
+
+ {notificationLabel(item.type)}
- )}
-
- {notificationLabel(item.type)}
-
-
- ))}
+
+ );
+ })}
>
)}
diff --git a/src/lib/notifications/index.ts b/src/lib/notifications/index.ts
index dcb37b6e..b544f8f6 100644
--- a/src/lib/notifications/index.ts
+++ b/src/lib/notifications/index.ts
@@ -52,6 +52,87 @@ export async function notifyUser(payload: PayloadType, input: NotifyInput): Prom
}
}
+/**
+ * Mark the given notifications read, scoped so an actor can only change the
+ * read-state of notifications that belong to THEM.
+ *
+ * Read-state is personal to the recipient. Staff with read-all oversight can
+ * *view* anyone's notifications, but they must never flip another user's read
+ * flag — doing so would hide that notification from its real owner. The ownership
+ * guard (`doc.user === actorUserId`) is what enforces this; `overrideAccess` on
+ * the reads/updates just fetches and writes the rows.
+ *
+ * @returns the ids that were actually flipped from unread to read.
+ */
+export async function markNotificationsReadForUser(
+ payload: PayloadType,
+ actorUserId: number,
+ ids: number[],
+): Promise {
+ const cleanIds = [...new Set(ids.filter((id) => Number.isFinite(id) && id > 0))];
+ if (cleanIds.length === 0) return [];
+
+ const found = await payload.find({
+ collection: "user-notifications",
+ where: { id: { in: cleanIds } },
+ limit: cleanIds.length,
+ depth: 0,
+ overrideAccess: true,
+ });
+
+ const updated: number[] = [];
+ for (const doc of found.docs) {
+ if (Number(doc.user) !== actorUserId) continue;
+ if (!doc.read) {
+ await payload.update({
+ collection: "user-notifications",
+ id: doc.id,
+ data: { read: true },
+ overrideAccess: true,
+ depth: 0,
+ });
+ updated.push(doc.id);
+ }
+ }
+ return updated;
+}
+
+/**
+ * Mark every unread notification that belongs to the actor as read. Never
+ * touches another user's notifications — unlike the old inline "mark all read"
+ * which swept unread rows across every user.
+ *
+ * @returns the ids that were actually flipped from unread to read.
+ */
+export async function markAllNotificationsReadForUser(
+ payload: PayloadType,
+ actorUserId: number,
+): Promise {
+ const found = await payload.find({
+ collection: "user-notifications",
+ where: { read: { equals: false } },
+ limit: 100,
+ depth: 0,
+ overrideAccess: true,
+ });
+
+ const updated: number[] = [];
+ for (const doc of found.docs) {
+ if (Number(doc.user) !== actorUserId) continue;
+ if (!doc.read) {
+ await payload.update({
+ collection: "user-notifications",
+ id: doc.id,
+ data: { read: true },
+ overrideAccess: true,
+ depth: 0,
+ });
+ updated.push(doc.id);
+ }
+ }
+ return updated;
+}
+
/** Short display label for a notification type, or the raw type when unknown. */
export function notificationLabel(type?: string | null): string {
switch (type) {