161 lines
5.8 KiB
TypeScript
161 lines
5.8 KiB
TypeScript
import { bulkNotificationIdsSchema, getNotificationsSchema } from "./data";
|
|
import { getNotificationController } from "./controller";
|
|
import { sValidator } from "@hono/standard-validator";
|
|
import { HonoContext } from "@core/hono.helpers";
|
|
import { Hono } from "hono";
|
|
|
|
const nc = getNotificationController();
|
|
|
|
export const notificationsRouter = new Hono<HonoContext>()
|
|
.get("/", async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const userId = c.env.locals.user.id;
|
|
const url = new URL(c.req.url);
|
|
|
|
const filters = {
|
|
userId,
|
|
isRead: url.searchParams.get("isRead")
|
|
? url.searchParams.get("isRead") === "true"
|
|
: undefined,
|
|
isArchived: url.searchParams.get("isArchived")
|
|
? url.searchParams.get("isArchived") === "true"
|
|
: undefined,
|
|
type: url.searchParams.get("type") || undefined,
|
|
category: url.searchParams.get("category") || undefined,
|
|
priority: url.searchParams.get("priority") || undefined,
|
|
search: url.searchParams.get("search") || undefined,
|
|
};
|
|
|
|
const pagination = {
|
|
page: parseInt(url.searchParams.get("page") || "1"),
|
|
pageSize: parseInt(url.searchParams.get("pageSize") || "20"),
|
|
sortBy: url.searchParams.get("sortBy") || "createdAt",
|
|
sortOrder: url.searchParams.get("sortOrder") || "desc",
|
|
};
|
|
|
|
const res = await nc.getNotifications(fctx, filters, pagination);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
})
|
|
.post(
|
|
"/get-notifications",
|
|
sValidator("json", getNotificationsSchema),
|
|
async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const data = c.req.valid("json");
|
|
const res = await nc.getNotifications(fctx, data.filters, data.pagination);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
},
|
|
)
|
|
.put(
|
|
"/mark-read",
|
|
sValidator("json", bulkNotificationIdsSchema),
|
|
async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const data = c.req.valid("json");
|
|
const userId = c.env.locals.user.id;
|
|
const res = await nc.markAsRead(fctx, [...data.notificationIds], userId);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
},
|
|
)
|
|
.put(
|
|
"/mark-unread",
|
|
sValidator("json", bulkNotificationIdsSchema),
|
|
async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const data = c.req.valid("json");
|
|
const userId = c.env.locals.user.id;
|
|
const res = await nc.markAsUnread(fctx, [...data.notificationIds], userId);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
},
|
|
)
|
|
.put(
|
|
"/archive",
|
|
sValidator("json", bulkNotificationIdsSchema),
|
|
async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const data = c.req.valid("json");
|
|
const userId = c.env.locals.user.id;
|
|
const res = await nc.archive(fctx, [...data.notificationIds], userId);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
},
|
|
)
|
|
.put(
|
|
"/unarchive",
|
|
sValidator("json", bulkNotificationIdsSchema),
|
|
async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const data = c.req.valid("json");
|
|
const userId = c.env.locals.user.id;
|
|
const res = await nc.unarchive(fctx, [...data.notificationIds], userId);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
},
|
|
)
|
|
.delete(
|
|
"/delete",
|
|
sValidator("json", bulkNotificationIdsSchema),
|
|
async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const data = c.req.valid("json");
|
|
const userId = c.env.locals.user.id;
|
|
const res = await nc.deleteNotifications(fctx, [...data.notificationIds], userId);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
},
|
|
)
|
|
.put("/mark-all-read", async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const userId = c.env.locals.user.id;
|
|
const res = await nc.markAllAsRead(fctx, userId);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
})
|
|
.get("/unread-count", async (c) => {
|
|
const fctx = c.env.locals.fCtx;
|
|
const userId = c.env.locals.user.id;
|
|
const res = await nc.getUnreadCount(fctx, userId);
|
|
return c.json(
|
|
res.isOk()
|
|
? { data: res.value, error: null }
|
|
: { data: null, error: res.error },
|
|
res.isOk() ? 200 : 400,
|
|
);
|
|
});
|