Files
ipwa/backend/src/routes/api/admin/notif/index.ts
T

61 lines
1.7 KiB
TypeScript

import { Request, Response, Router } from "express";
import { Perms, adminPerm } from "@/utility";
import Group from "@schemas/Group";
import { Message } from "@/notif";
import { outboxRouter } from "./outbox";
import usettings, { Features } from "@/helpers/usettings";
const notifRouter = Router()
notifRouter.use(adminPerm(Perms.Notif))
notifRouter.use(usettings.mw(Features.Notif))
type PushSendBody = {
recp:
{ type: "uid", uid: string } |
{ type: "room", room: string } |
{ type: "group", group: string },
title: string,
body: string
}
notifRouter.post("/send", async (req: Request<undefined, undefined, PushSendBody>, res: Response) => {
let recp: string
switch (req.body.recp.type) {
case "uid":
recp = req.body.recp.uid
break;
case "room":
recp = req.body.recp.room
break;
case "group":
if (!usettings.value.modules.groups) return res.sendStatus(406).end()
recp = req.body.recp.group
break;
default:
res.status(400).end()
break;
}
const message = new Message(req.body.title, req.body.body, req.body.recp.type, recp)
let result = await message.send()
if (result === false) {
res.sendStatusMessage(404, "No recepients")
} else {
console.log(`
From: ${req.user.uname} (${req.user._id})
To: ${recp}
Subject: ${req.body.title}
${req.body.body}
`);
res.send(result)
}
})
notifRouter.get("/groups", async (req, res) => {
res.send(await Group.find({}, { name: 1, _id: 1 }))
})
notifRouter.use("/outbox", outboxRouter)
export { notifRouter }