All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 50s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { Router } from "@oak/oak";
|
|
import {
|
|
APIErrorCode,
|
|
type APIResponse,
|
|
type PaginatedData,
|
|
} from "../../shared/api.ts";
|
|
import { APIException } from "../lib/api-exception.ts";
|
|
import type { Notification } from "../../shared/notification.ts";
|
|
import { parsePagination } from "../lib/pagination.ts";
|
|
import { type AuthContext, authMiddleware } from "../middleware/auth.ts";
|
|
import {
|
|
getNotificationsForUser,
|
|
markAllRead,
|
|
markOneRead,
|
|
} from "../services/notification-service.ts";
|
|
|
|
const router = new Router({ prefix: "/api/notifications" });
|
|
|
|
// GET /api/notifications?page=N&limit=N
|
|
router.get("/", authMiddleware, (ctx: AuthContext) => {
|
|
if (!ctx.state.user) {
|
|
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authenticated");
|
|
}
|
|
const { page, limit } = parsePagination(ctx.request.url.searchParams);
|
|
const { items, hasMore } = getNotificationsForUser(
|
|
ctx.state.user.userId,
|
|
page,
|
|
limit,
|
|
);
|
|
const responseBody: APIResponse<PaginatedData<Notification>> = {
|
|
success: true,
|
|
data: { items, hasMore },
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
// POST /api/notifications/read-all
|
|
router.post("/read-all", authMiddleware, (ctx: AuthContext) => {
|
|
if (!ctx.state.user) {
|
|
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authenticated");
|
|
}
|
|
markAllRead(ctx.state.user.userId);
|
|
ctx.response.status = 204;
|
|
});
|
|
|
|
// PATCH /api/notifications/:id/read
|
|
router.patch("/:id/read", authMiddleware, (ctx) => {
|
|
const user = ctx.state.user;
|
|
if (!user) {
|
|
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authenticated");
|
|
}
|
|
markOneRead(ctx.params.id, user.userId);
|
|
ctx.response.status = 204;
|
|
});
|
|
|
|
export default router;
|