feat: Added notification dialog on frontend
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { Router } from "express";
|
||||
import { islogged, isadmin} from "@/utility";
|
||||
import { newsRouter } from "./admin/news";
|
||||
import { accsRouter } from "./admin/accs";
|
||||
import { menuRouter } from "./admin/menu";
|
||||
import { groupsRouter } from "./admin/groups";
|
||||
import { notifRouter } from "./admin/notif";
|
||||
import { keysRouter } from "./admin/keys";
|
||||
import { cleanRouter } from "./admin/clean";
|
||||
import { settingsRouter } from "./admin/settings";
|
||||
import { newsRouter } from "./news";
|
||||
import { accsRouter } from "./accs";
|
||||
import { menuRouter } from "./menu";
|
||||
import { groupsRouter } from "./groups";
|
||||
import { notifRouter } from "./notif";
|
||||
import { keysRouter } from "./keys";
|
||||
import { cleanRouter } from "./clean";
|
||||
import { settingsRouter } from "./settings";
|
||||
|
||||
const adminRouter = Router()
|
||||
export const adminRouter = Router()
|
||||
|
||||
adminRouter.use(islogged, isadmin)
|
||||
adminRouter.use('/news', newsRouter)
|
||||
@@ -25,5 +25,3 @@ adminRouter.get('/usearch', (req, res) => {
|
||||
// TODO: Add search
|
||||
res.send([req.query['q']])
|
||||
})
|
||||
|
||||
export {adminRouter};
|
||||
@@ -6,6 +6,7 @@ import capability, { Features } from "@/helpers/capability";
|
||||
import Inbox from "@/schemas/Inbox";
|
||||
import { Types } from "mongoose";
|
||||
import { IUser } from "@/schemas/User";
|
||||
import { outboxRouter } from "./outbox";
|
||||
|
||||
const notifRouter = Router()
|
||||
|
||||
@@ -49,19 +50,10 @@ notifRouter.post("/send", async (req: Request<undefined, PushResult, PushSendBod
|
||||
res.send(result)
|
||||
})
|
||||
|
||||
notifRouter.get("/outbox", async (req, res: Response) => {
|
||||
var result = await Inbox.find({}, {}, {sort: {sentDate: -1}}).populate<{rcpt: IUser & {_id: Types.ObjectId}}>("rcpt", ['fname', 'surname', 'uname', '_id', 'room']).exec()
|
||||
var final = result.map(v => {
|
||||
return {
|
||||
...v.toJSON(),
|
||||
ack: v.ack.length
|
||||
}
|
||||
})
|
||||
res.send(final)
|
||||
})
|
||||
|
||||
notifRouter.get("/groups", async (req, res) => {
|
||||
res.send(await Group.find({}, { name: 1, _id: 1 }))
|
||||
})
|
||||
|
||||
notifRouter.use("/outbox", outboxRouter)
|
||||
|
||||
export { notifRouter }
|
||||
35
src/routes/api/admin/notif/outbox.ts
Normal file
35
src/routes/api/admin/notif/outbox.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import Inbox from "@/schemas/Inbox";
|
||||
import { IUser } from "@/schemas/User";
|
||||
import { Response, Router } from "express";
|
||||
|
||||
export const outboxRouter = Router()
|
||||
|
||||
outboxRouter.get("/", async (req, res: Response) => {
|
||||
var result = await Inbox.find({}, {message: 1, sentDate: 1}, {sort: {sentDate: -1}})
|
||||
var final = result.map(v => {
|
||||
return {
|
||||
_id: v._id,
|
||||
sentDate: v.sentDate,
|
||||
title: v.message.title
|
||||
}
|
||||
})
|
||||
res.send(final)
|
||||
})
|
||||
|
||||
outboxRouter.get("/:id/message", async (req, res) => {
|
||||
var msg = await Inbox.findById(req.params.id, {message: 1})
|
||||
if (msg) {
|
||||
res.send(msg.message.body)
|
||||
} else {
|
||||
res.status(404).send({message: "ERR: 404 Message id not found"})
|
||||
}
|
||||
})
|
||||
|
||||
outboxRouter.get("/:id/rcpts", async (req, res) => {
|
||||
var msg = await Inbox.findById(req.params.id, {rcpt: 1}).populate<{rcpt: Pick<IUser, "uname" | "room" | "fname" | "surname">}>({path: "rcpt", select: ["uname", "room", "fname", "surname"]}).exec()
|
||||
if (msg) {
|
||||
res.send(msg.rcpt)
|
||||
} else {
|
||||
res.status(404).send({message: "ERR: 404 Message id not found"})
|
||||
}
|
||||
})
|
||||
@@ -9,7 +9,9 @@ import Key, { IKey } from "@schemas/Key";
|
||||
import usettings from "@/helpers/usettings";
|
||||
import Grade from "@schemas/Grade";
|
||||
import { createHash } from "node:crypto";
|
||||
const appRouter = Router();
|
||||
import Inbox from "@/schemas/Inbox";
|
||||
|
||||
export const appRouter = Router();
|
||||
|
||||
appRouter.use(islogged)
|
||||
|
||||
@@ -75,4 +77,26 @@ appRouter.get("/clean/:date", capability.mw(Features.Clean), async (req, res) =>
|
||||
}))
|
||||
})
|
||||
|
||||
export {appRouter};
|
||||
appRouter.get("/notif/check", capability.mw(Features.Notif), async (req, res) => {
|
||||
var result = await Inbox.find({rcpt: req.user._id, $nor: [{ack: req.user._id}]}, {message: 1, sentDate: 1})
|
||||
if (result) {
|
||||
res.send(result)
|
||||
} else {
|
||||
res.send([])
|
||||
}
|
||||
})
|
||||
|
||||
appRouter.post("/notif/:id/ack", capability.mw(Features.Notif), async (req, res) => {
|
||||
var result = await Inbox.findById(req.params.id)
|
||||
if (result) {
|
||||
if (result.rcpt.includes(req.user._id) && !result.ack.includes(req.user._id)) {
|
||||
result.ack.push(req.user._id)
|
||||
await result.save({})
|
||||
res.send({status: 200})
|
||||
} else {
|
||||
res.status(403).send({status: 401, message: "User doesn't have access or message already acknowledged"})
|
||||
}
|
||||
} else {
|
||||
res.status(404).send({status: 404, message: "Message not found"})
|
||||
}
|
||||
})
|
||||
8
src/routes/api/index.ts
Normal file
8
src/routes/api/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Router } from "express";
|
||||
import { appRouter } from "./app";
|
||||
import { adminRouter } from "./admin";
|
||||
|
||||
export const apiRouter = Router();
|
||||
|
||||
apiRouter.use("/app", appRouter)
|
||||
apiRouter.use("/admin", adminRouter)
|
||||
@@ -1,16 +1,14 @@
|
||||
import { Router } from "express";
|
||||
import Notification from "@schemas/Notification";
|
||||
import { islogged } from "@/utility";
|
||||
import { adminRouter } from "./api/adminRouter";
|
||||
import { appRouter } from "./api/appRouter";
|
||||
import { authRouter } from "./auth/index";
|
||||
import capability, { Features } from "@/helpers/capability";
|
||||
import mongoose from "mongoose";
|
||||
import { apiRouter } from "./api";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.use('/app', appRouter)
|
||||
router.use('/admin', adminRouter)
|
||||
router.use('/', apiRouter)
|
||||
router.use('/auth', authRouter)
|
||||
|
||||
router.get("/healthcheck", async (req, res) => {
|
||||
@@ -27,4 +25,6 @@ router.post("/notif", islogged, capability.mw(Features.Notif), async (req, res)
|
||||
res.send({"status": 200})
|
||||
})
|
||||
|
||||
router.use("/", apiRouter)
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user