Skip to content

Commit e27f45a

Browse files
committed
fix(web): scope notification read updates to their recipient
1 parent ea509a5 commit e27f45a

2 files changed

Lines changed: 95 additions & 12 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
const mocks = vi.hoisted(() => ({
4+
query: vi.fn(),
5+
getCurrentUser: vi.fn(),
6+
revalidatePath: vi.fn(),
7+
}));
8+
9+
vi.mock("@cap/database", async () => {
10+
const { drizzle } = await import("drizzle-orm/mysql-proxy");
11+
const client = drizzle(mocks.query);
12+
return { db: () => client };
13+
});
14+
vi.mock("@cap/database/auth/session", () => ({
15+
getCurrentUser: mocks.getCurrentUser,
16+
}));
17+
vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
18+
19+
import { markAsRead } from "@/actions/notifications/mark-as-read";
20+
21+
beforeEach(() => {
22+
mocks.getCurrentUser.mockResolvedValue({ id: "user-1" });
23+
mocks.query.mockResolvedValue({ rows: [{ affectedRows: 1, insertId: 0 }] });
24+
});
25+
26+
describe("notification read state", () => {
27+
it.each(["own-notification", "another-users-notification"])(
28+
"scopes an individual update to the current recipient for %s",
29+
async (notificationId) => {
30+
await markAsRead(notificationId);
31+
32+
expect(mocks.query).toHaveBeenCalledExactlyOnceWith(
33+
expect.stringContaining(
34+
"where (`notifications`.`recipientId` = ? and `notifications`.`id` = ?)",
35+
),
36+
[expect.any(String), "user-1", notificationId],
37+
"execute",
38+
);
39+
expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard");
40+
},
41+
);
42+
43+
it("marks all notifications for the current recipient when no ID is supplied", async () => {
44+
await markAsRead();
45+
46+
expect(mocks.query).toHaveBeenCalledExactlyOnceWith(
47+
"update `notifications` set `readAt` = ? where `notifications`.`recipientId` = ?",
48+
[expect.any(String), "user-1"],
49+
"execute",
50+
);
51+
expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard");
52+
});
53+
54+
it("succeeds without exposing whether a notification matched", async () => {
55+
mocks.query.mockResolvedValueOnce({
56+
rows: [{ affectedRows: 0, insertId: 0 }],
57+
});
58+
59+
await expect(markAsRead("missing-notification")).resolves.toBeUndefined();
60+
61+
expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard");
62+
});
63+
64+
it("does not write when the requester is not authenticated", async () => {
65+
mocks.getCurrentUser.mockResolvedValueOnce(null);
66+
67+
await expect(markAsRead("notification-1")).rejects.toThrow(
68+
"User not found",
69+
);
70+
71+
expect(mocks.query).not.toHaveBeenCalled();
72+
expect(mocks.revalidatePath).not.toHaveBeenCalled();
73+
});
74+
75+
it("reports a failed write without revalidating the dashboard", async () => {
76+
mocks.query.mockRejectedValueOnce(new Error("Database unavailable"));
77+
vi.spyOn(console, "log").mockImplementation(() => {});
78+
79+
await expect(markAsRead("notification-1")).rejects.toThrow(
80+
"Error marking notification(s) as read",
81+
);
82+
83+
expect(mocks.revalidatePath).not.toHaveBeenCalled();
84+
});
85+
});

‎apps/web/actions/notifications/mark-as-read.ts‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { db } from "@cap/database";
44
import { getCurrentUser } from "@cap/database/auth/session";
55
import { notifications } from "@cap/database/schema";
6-
import { eq } from "drizzle-orm";
6+
import { and, eq } from "drizzle-orm";
77
import { revalidatePath } from "next/cache";
88

99
export const markAsRead = async (notificationId?: string) => {
@@ -14,17 +14,15 @@ export const markAsRead = async (notificationId?: string) => {
1414

1515
try {
1616
const now = new Date();
17-
if (notificationId) {
18-
await db()
19-
.update(notifications)
20-
.set({ readAt: now })
21-
.where(eq(notifications.id, notificationId));
22-
} else {
23-
await db()
24-
.update(notifications)
25-
.set({ readAt: now })
26-
.where(eq(notifications.recipientId, currentUser.id));
27-
}
17+
await db()
18+
.update(notifications)
19+
.set({ readAt: now })
20+
.where(
21+
and(
22+
eq(notifications.recipientId, currentUser.id),
23+
notificationId ? eq(notifications.id, notificationId) : undefined,
24+
),
25+
);
2826
} catch (error) {
2927
console.log(error);
3028
throw new Error("Error marking notification(s) as read");

0 commit comments

Comments
 (0)