mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-18 13:15:39 +00:00
feat(notifications): User Notifications
This commit is contained in:
@@ -33,6 +33,7 @@ import { registerIntegrationAuthRouter } from "./integration-auth-router";
|
||||
import { registerIntegrationRouter } from "./integration-router";
|
||||
import { registerInviteOrgRouter } from "./invite-org-router";
|
||||
import { registerMicrosoftTeamsRouter } from "./microsoft-teams-router";
|
||||
import { registerNotificationRouter } from "./notification-router";
|
||||
import { registerOrgAdminRouter } from "./org-admin-router";
|
||||
import { registerOrgRouter } from "./organization-router";
|
||||
import { registerPasswordRouter } from "./password-router";
|
||||
@@ -83,6 +84,7 @@ export const registerV1Routes = async (server: FastifyZodProvider) => {
|
||||
await server.register(registerAdminRouter, { prefix: "/admin" });
|
||||
await server.register(registerOrgAdminRouter, { prefix: "/organization-admin" });
|
||||
await server.register(registerUserRouter, { prefix: "/user" });
|
||||
await server.register(registerNotificationRouter, { prefix: "/notifications" });
|
||||
await server.register(registerInviteOrgRouter, { prefix: "/invite-org" });
|
||||
await server.register(registerUserActionRouter, { prefix: "/user-action" });
|
||||
await server.register(registerSecretImportRouter, { prefix: "/secret-imports" });
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { UserNotificationsSchema } from "@app/db/schemas/user-notifications";
|
||||
import { UnauthorizedError } from "@app/lib/errors";
|
||||
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
|
||||
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
|
||||
import { AuthMode } from "@app/services/auth/auth-type";
|
||||
|
||||
export const registerNotificationRouter = async (server: FastifyZodProvider) => {
|
||||
server.route({
|
||||
url: "/user",
|
||||
config: {
|
||||
rateLimit: readLimit
|
||||
},
|
||||
method: "GET",
|
||||
schema: {
|
||||
response: {
|
||||
200: z.object({
|
||||
notifications: UserNotificationsSchema.array()
|
||||
})
|
||||
}
|
||||
},
|
||||
onRequest: verifyAuth([AuthMode.JWT]),
|
||||
handler: async (req) => {
|
||||
if (req.auth.authMode !== AuthMode.JWT) {
|
||||
throw new UnauthorizedError({ message: "This endpoint can only be accessed by users" });
|
||||
}
|
||||
|
||||
const notifications = await server.services.notification.listUserNotifications({ userId: req.auth.userId });
|
||||
|
||||
return { notifications };
|
||||
}
|
||||
});
|
||||
|
||||
server.route({
|
||||
url: "/user/:notificationId",
|
||||
config: {
|
||||
rateLimit: writeLimit
|
||||
},
|
||||
method: "DELETE",
|
||||
schema: {
|
||||
params: z.object({
|
||||
notificationId: z.string()
|
||||
}),
|
||||
response: {
|
||||
200: z.object({
|
||||
notification: UserNotificationsSchema
|
||||
})
|
||||
}
|
||||
},
|
||||
onRequest: verifyAuth([AuthMode.JWT]),
|
||||
handler: async (req) => {
|
||||
if (req.auth.authMode !== AuthMode.JWT) {
|
||||
throw new UnauthorizedError({ message: "This endpoint can only be accessed by users" });
|
||||
}
|
||||
|
||||
const notification = await server.services.notification.deleteUserNotification({
|
||||
notificationId: req.params.notificationId,
|
||||
userId: req.auth.userId
|
||||
});
|
||||
|
||||
return { notification };
|
||||
}
|
||||
});
|
||||
|
||||
server.route({
|
||||
url: "/user/:notificationId/mark-as-read",
|
||||
config: {
|
||||
rateLimit: writeLimit
|
||||
},
|
||||
method: "POST",
|
||||
schema: {
|
||||
params: z.object({
|
||||
notificationId: z.string()
|
||||
})
|
||||
},
|
||||
onRequest: verifyAuth([AuthMode.JWT]),
|
||||
handler: async (req) => {
|
||||
if (req.auth.authMode !== AuthMode.JWT) {
|
||||
throw new UnauthorizedError({ message: "This endpoint can only be accessed by users" });
|
||||
}
|
||||
|
||||
await server.services.notification.markUserNotificationAsRead({
|
||||
notificationId: req.params.notificationId,
|
||||
userId: req.auth.userId
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Mark all user notifications as read
|
||||
server.route({
|
||||
url: "/user/mark-as-read",
|
||||
config: {
|
||||
rateLimit: writeLimit
|
||||
},
|
||||
method: "POST",
|
||||
onRequest: verifyAuth([AuthMode.JWT]),
|
||||
handler: async (req) => {
|
||||
if (req.auth.authMode !== AuthMode.JWT) {
|
||||
throw new UnauthorizedError({ message: "This endpoint can only be accessed by users" });
|
||||
}
|
||||
|
||||
await server.services.notification.markUserNotificationsAsRead({ userId: req.auth.userId });
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user