core/pages/api/v2/users/notification.ts
SKINMAKER 160fe4ecb3
feat: 투표 알림 기능 (#669)
* feat: ensure djs clients are singleton instances

* feat: add votes table

* feat: vote notification

* chore: reduce vote cooldown to 15 min

* feat: add SetNotification to server

* chore: add debug logs

* fix: do not add notification when token and voteid already exists

* feat: add loading indicator

* feat: refresh notification when voted

* feat: add opt-out

* feat: add debug log

* fix: initialize firebase app

* fix: remove app on messaging

* feat: show notifications only with service worker

* fix: state improperly used

* fix: schedule notification if notification is newly added

* chore:  remove duplicated notification

* chore: add spacing

* chore: get token if notification is granted

* chore: change vote cooldown to 12 hours

* chore: remove logging
2025-02-17 07:34:12 +09:00

50 lines
1.6 KiB
TypeScript

import { addNotification, get } from '@utils/Query'
import RequestHandler from '@utils/RequestHandler'
const Notification = RequestHandler()
.get(async (req, res) => {
const token = req.query.token as string
const target = req.query.target as string
const user = await get.Authorization(req.cookies.token)
if (!user) return res.status(401).json({ code: 401 })
const result = token
? await get.notifications.token(token, target)
: await get.notifications.user(user)
if (!result) return res.status(400).json({ code: 400 })
return res.status(200).json({ code: 200, data: result })
})
.post(async (req, res) => {
const user = await get.Authorization(req.cookies.token)
if (!user) return res.status(401).json({ code: 401 })
const { token, targetId } = req.body
if (!token || !targetId)
return res.status(400).json({ code: 400, message: 'Either token or targetId is missing' })
const result = await addNotification({ token, targetId, userId: user })
if (typeof result === 'string') return res.status(400).json({ code: 400, message: result })
return res.status(200).json({ code: 200 })
})
.delete(async (req, res) => {
const user = await get.Authorization(req.cookies.token)
if (!user) return res.status(401).json({ code: 401 })
const { token, targetId } = req.body
if (!token) return res.status(400).json({ code: 400 })
const result = global.notification.removeNotification({ userId: user, targetId, token })
if (!result) return res.status(400).json({ code: 400 })
return res.status(200).json({ code: 200 })
})
export default Notification