⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
feat(api): add notification unread and delete
Andrej425a8af085fe
diff --git a/apps/api/src/notification/controllers/clear-notifications.ts b/apps/api/src/notification/controllers/clear-notifications.tsindex 3ddb4c1cb..1e7c853e5 100644--- a/apps/api/src/notification/controllers/clear-notifications.ts+++ b/apps/api/src/notification/controllers/clear-notifications.ts@@ -5,12 +5,17 @@ import { notificationTable } from "../../database/schema";import { notificationResourceAccess } from "../resource-access";import { notificationWorkspaceFilter } from "../workspace-filter";-async function clearNotifications(userId: string, workspaceId?: string) {+async function clearNotifications(+ userId: string,+ workspaceId?: string,+ onlyRead = false,+) {await db.delete(notificationTable).where(and(eq(notificationTable.userId, userId),+ onlyRead ? eq(notificationTable.isRead, true) : undefined,notificationWorkspaceFilter(workspaceId),workspaceId? notificationResourceAccess(diff --git a/apps/api/src/notification/controllers/delete-notification.ts b/apps/api/src/notification/controllers/delete-notification.tsnew file mode 100644index 000000000..f4fa829b0--- /dev/null+++ b/apps/api/src/notification/controllers/delete-notification.ts@@ -0,0 +1,33 @@+import { and, eq } from "drizzle-orm";+import { HTTPException } from "hono/http-exception";+import db from "../../database";+import { notificationTable } from "../../database/schema";++import { notificationResourceAccess } from "../resource-access";++async function deleteNotification(id: string, userId: string) {+ const [deleted] = await db+ .delete(notificationTable)+ .where(+ and(+ eq(notificationTable.id, id),+ eq(notificationTable.userId, userId),+ notificationResourceAccess(+ userId,+ notificationTable.resourceId,+ notificationTable.resourceType,+ ),+ ),+ )+ .returning({ id: notificationTable.id });++ if (!deleted) {+ throw new HTTPException(404, {+ message: "Notification not found",+ });+ }++ return { success: true };+}++export default deleteNotification;diff --git a/apps/api/src/notification/controllers/mark-notification-as-read.ts b/apps/api/src/notification/controllers/set-notification-read.tssimilarity index 83%rename from apps/api/src/notification/controllers/mark-notification-as-read.tsrename to apps/api/src/notification/controllers/set-notification-read.tsindex ec02d1cd1..0d143ac10 100644--- a/apps/api/src/notification/controllers/mark-notification-as-read.ts+++ b/apps/api/src/notification/controllers/set-notification-read.ts@@ -5,10 +5,14 @@ import { notificationTable } from "../../database/schema";import { notificationResourceAccess } from "../resource-access";-async function markNotificationAsRead(id: string, userId: string) {+async function setNotificationRead(+ id: string,+ userId: string,+ isRead: boolean,+) {const [notification] = await db.update(notificationTable)- .set({ isRead: true })+ .set({ isRead }).where(and(eq(notificationTable.id, id),@@ -31,4 +35,4 @@ async function markNotificationAsRead(id: string, userId: string) {return notification;}-export default markNotificationAsRead;+export default setNotificationRead;diff --git a/apps/api/src/notification/index.ts b/apps/api/src/notification/index.tsindex 443c9557e..aaab2a2c4 100644--- a/apps/api/src/notification/index.ts+++ b/apps/api/src/notification/index.ts@@ -10,9 +10,10 @@ import {} from "../openapi";import clearNotifications from "./controllers/clear-notifications";import createNotification from "./controllers/create-notification";+import deleteNotification from "./controllers/delete-notification";import getNotifications from "./controllers/get-notifications";import markAllNotificationsAsRead from "./controllers/mark-all-notifications-as-read";-import markAsRead from "./controllers/mark-notification-as-read";+import setNotificationRead from "./controllers/set-notification-read";import {bulkResultSchema,notificationListSchema,@@ -76,6 +77,21 @@ const markAsReadRoute = createRoute({},});+const markAsUnreadRoute = createRoute({+ method: "patch",+ operationId: "markNotificationAsUnread",+ path: "/{id}/unread",+ tags: ["Notifications"],+ summary: "Mark notification unread",+ description:+ "Mark one notification as unread again. Scoped to the current user, so another user's notification is not found.",+ request: { params: notificationParam },+ responses: {+ 200: jsonResponse("The updated notification", notificationSchema),+ 404: errorResponse("Notification not found"),+ },+});+const markAllAsReadRoute = createRoute({method: "patch",operationId: "markAllNotificationsAsRead",@@ -104,6 +120,35 @@ const clearAllRoute = createRoute({},});+const clearReadRoute = createRoute({+ method: "delete",+ operationId: "clearReadNotifications",+ path: "/clear-read",+ tags: ["Notifications"],+ summary: "Clear read notifications",+ request: { query: notificationWorkspaceQuery },+ description:+ "Permanently delete the current user's read notifications, optionally limited to one workspace. Unread notifications are kept. This cannot be undone.",+ responses: {+ 200: jsonResponse("Read notifications cleared", bulkResultSchema),+ },+});++const deleteNotificationRoute = createRoute({+ method: "delete",+ operationId: "deleteNotification",+ path: "/{id}",+ tags: ["Notifications"],+ summary: "Delete notification",+ description:+ "Permanently delete one notification. Scoped to the current user, so another user's notification is not found.",+ request: { params: notificationParam },+ responses: {+ 200: jsonResponse("Notification deleted", bulkResultSchema),+ 404: errorResponse("Notification not found"),+ },+});+const notification = apiRouter().openapi(listNotificationsRoute, async (c) =>c.json(@@ -134,7 +179,20 @@ const notification = apiRouter());}).openapi(markAsReadRoute, async (c) =>- c.json(await markAsRead(c.req.valid("param").id, c.get("userId")), 200),+ c.json(+ await setNotificationRead(c.req.valid("param").id, c.get("userId"), true),+ 200,+ ),+ )+ .openapi(markAsUnreadRoute, async (c) =>+ c.json(+ await setNotificationRead(+ c.req.valid("param").id,+ c.get("userId"),+ false,+ ),+ 200,+ ),).openapi(markAllAsReadRoute, async (c) =>c.json(@@ -153,6 +211,22 @@ const notification = apiRouter()),200,),+ )+ .openapi(clearReadRoute, async (c) =>+ c.json(+ await clearNotifications(+ c.get("userId"),+ c.req.valid("query").workspaceId,+ true,+ ),+ 200,+ ),+ )+ .openapi(deleteNotificationRoute, async (c) =>+ c.json(+ await deleteNotification(c.req.valid("param").id, c.get("userId")),+ 200,+ ),);subscribeToEvent<{diff --git a/apps/docs/api-reference/endpoints/notifications/clearReadNotifications.mdx b/apps/docs/api-reference/endpoints/notifications/clearReadNotifications.mdxnew file mode 100644index 000000000..0dc3b94fb--- /dev/null+++ b/apps/docs/api-reference/endpoints/notifications/clearReadNotifications.mdx@@ -0,0 +1,4 @@+---+title: "Clear read notifications"+openapi: "DELETE /notification/clear-read"+---diff --git a/apps/docs/api-reference/endpoints/notifications/deleteNotification.mdx b/apps/docs/api-reference/endpoints/notifications/deleteNotification.mdxnew file mode 100644index 000000000..7353ba268--- /dev/null+++ b/apps/docs/api-reference/endpoints/notifications/deleteNotification.mdx@@ -0,0 +1,4 @@+---+title: "Delete notification"+openapi: "DELETE /notification/{id}"+---diff --git a/apps/docs/api-reference/endpoints/notifications/markNotificationAsUnread.mdx b/apps/docs/api-reference/endpoints/notifications/markNotificationAsUnread.mdxnew file mode 100644index 000000000..b3b6b1264--- /dev/null+++ b/apps/docs/api-reference/endpoints/notifications/markNotificationAsUnread.mdx@@ -0,0 +1,4 @@+---+title: "Mark notification unread"+openapi: "PATCH /notification/{id}/unread"+---diff --git a/apps/docs/openapi.json b/apps/docs/openapi.jsonindex 101b9d675..8907e27be 100644--- a/apps/docs/openapi.json+++ b/apps/docs/openapi.json@@ -15486,6 +15486,61 @@}}},+ "/notification/{id}/unread": {+ "patch": {+ "operationId": "markNotificationAsUnread",+ "tags": [+ "Notifications"+ ],+ "summary": "Mark notification unread",+ "description": "Mark one notification as unread again. Scoped to the current user, so another user's notification is not found.",+ "parameters": [+ {+ "schema": {+ "type": "string"+ },+ "required": true,+ "name": "id",+ "in": "path"+ }+ ],+ "responses": {+ "200": {+ "description": "The updated notification",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/Notification"+ }+ }+ }+ },+ "401": {+ "description": "Missing or invalid credentials",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ApiError"+ }+ }+ }+ },+ "404": {+ "description": "Notification not found",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ApiError"+ }+ }+ }+ },+ "429": {+ "$ref": "#/components/responses/ApiKeyRateLimited"+ }+ }+ }+ },"/notification/read-all": {"patch": {"operationId": "markAllNotificationsAsRead",@@ -15582,6 +15637,109 @@}}},+ "/notification/clear-read": {+ "delete": {+ "operationId": "clearReadNotifications",+ "tags": [+ "Notifications"+ ],+ "summary": "Clear read notifications",+ "description": "Permanently delete the current user's read notifications, optionally limited to one workspace. Unread notifications are kept. This cannot be undone.",+ "parameters": [+ {+ "schema": {+ "type": "string",+ "minLength": 1,+ "description": "Limit notifications to resources currently in this workspace, plus user-wide notifications with no resource. Omit to include all workspaces."+ },+ "required": false,+ "description": "Limit notifications to resources currently in this workspace, plus user-wide notifications with no resource. Omit to include all workspaces.",+ "name": "workspaceId",+ "in": "query"+ }+ ],+ "responses": {+ "200": {+ "description": "Read notifications cleared",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/NotificationBulkResult"+ }+ }+ }+ },+ "401": {+ "description": "Missing or invalid credentials",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ApiError"+ }+ }+ }+ },+ "429": {+ "$ref": "#/components/responses/ApiKeyRateLimited"+ }+ }+ }+ },+ "/notification/{id}": {+ "delete": {+ "operationId": "deleteNotification",+ "tags": [+ "Notifications"+ ],+ "summary": "Delete notification",+ "description": "Permanently delete one notification. Scoped to the current user, so another user's notification is not found.",+ "parameters": [+ {+ "schema": {+ "type": "string"+ },+ "required": true,+ "name": "id",+ "in": "path"+ }+ ],+ "responses": {+ "200": {+ "description": "Notification deleted",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/NotificationBulkResult"+ }+ }+ }+ },+ "401": {+ "description": "Missing or invalid credentials",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ApiError"+ }+ }+ }+ },+ "404": {+ "description": "Notification not found",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ApiError"+ }+ }+ }+ },+ "429": {+ "$ref": "#/components/responses/ApiKeyRateLimited"+ }+ }+ }+ },"/notification-preferences": {"get": {"operationId": "getNotificationPreferences",diff --git a/tests/api-integration/notification-access.test.ts b/tests/api-integration/notification-access.test.tsindex b5924a585..a85bb0b22 100644--- a/tests/api-integration/notification-access.test.ts+++ b/tests/api-integration/notification-access.test.ts@@ -10,9 +10,11 @@ import {import createComment from "../../apps/api/src/activity/controllers/create-comment";import db, { schema } from "../../apps/api/src/database";import { taskReminderSentTable } from "../../apps/api/src/database/schema";+import clearNotifications from "../../apps/api/src/notification/controllers/clear-notifications";import createNotification from "../../apps/api/src/notification/controllers/create-notification";+import deleteNotification from "../../apps/api/src/notification/controllers/delete-notification";import getNotifications from "../../apps/api/src/notification/controllers/get-notifications";-import markAsRead from "../../apps/api/src/notification/controllers/mark-notification-as-read";+import setNotificationRead from "../../apps/api/src/notification/controllers/set-notification-read";const { deliverNotification } = await vi.importActual<typeof import("../../apps/api/src/notification-preferences/delivery")@@ -213,7 +215,7 @@ describe("notification recipient boundaries", () => {);expect(await getNotifications(member.user.id)).toEqual([]);await expect(- markAsRead(notification.id, member.user.id),+ setNotificationRead(notification.id, member.user.id, true),).rejects.toMatchObject({status: 404,});@@ -221,6 +223,49 @@ describe("notification recipient boundaries", () => {expect(sendEmail).toHaveBeenCalledTimes(1);});+ it("changes, deletes and clears only the caller's own notifications", async () => {+ const { actor, member, task } = await fixture();+ const insert = (userId: string, isRead: boolean) =>+ db+ .insert(schema.notificationTable)+ .values({+ userId,+ isRead,+ resourceType: "task",+ resourceId: task.id,+ type: "task_comment",+ })+ .returning()+ .then(([row]) => row);+ const read = await insert(member.user.id, true);+ const unread = await insert(member.user.id, false);+ const foreign = await insert(actor.user.id, true);++ await expect(+ setNotificationRead(read.id, member.user.id, false),+ ).resolves.toHaveProperty("isRead", false);+ await expect(+ setNotificationRead(foreign.id, member.user.id, false),+ ).rejects.toMatchObject({ status: 404 });+ await expect(+ deleteNotification(foreign.id, member.user.id),+ ).rejects.toMatchObject({ status: 404 });++ await setNotificationRead(read.id, member.user.id, true);+ await clearNotifications(member.user.id, actor.workspace.id, true);+ const remaining = await db.select().from(schema.notificationTable);+ expect(remaining.map((row) => row.id).sort()).toEqual(+ [unread.id, foreign.id].sort(),+ );++ await expect(+ deleteNotification(unread.id, member.user.id),+ ).resolves.toEqual({ success: true });+ expect(+ (await db.select().from(schema.notificationTable)).map((row) => row.id),+ ).toEqual([foreign.id]);+ });+it("rejects foreign assignees while preserving valid partial imports", async () => {const { actor, outsider, member, project, task } = await fixture();await expect(