import { FastifyRequest, FastifyReply } from "fastify"; import { PageQueryParams } from "../pagination"; import { createNotification, deleteNotification, listNotifications, updateNotification, } from "./notification.service"; import { CreateNotificationInput, UpdateNotificationInput, } from "./notification.schema"; export async function createNotificationHandler( req: FastifyRequest, res: FastifyReply ) { const input = req.body as CreateNotificationInput; try { const notification = await createNotification(input, req.user.tenantId); return res.code(201).send(notification); } catch (err) { return err; } } export async function listNotificationsHandler( req: FastifyRequest, res: FastifyReply ) { const queryParams = req.query as PageQueryParams; try { const notificationList = await listNotifications( queryParams, req.user.tenantId ); return res.code(200).send(notificationList); } catch (err) { return err; } } export async function updateNotificationHandler( req: FastifyRequest, res: FastifyReply ) { const input = req.body as UpdateNotificationInput; const { notifId } = req.params as { notifId: string }; try { const updatedNotification = await updateNotification( notifId, input, req.user.tenantId ); if (!updatedNotification) return res.code(404).send({ error: "resource not found" }); return res.code(200).send(updatedNotification); } catch (err) { return err; } } export async function deleteNotificationHandler( req: FastifyRequest, res: FastifyReply ) { const { notifId } = req.params as { notifId: string }; try { const deleteResult = await deleteNotification(notifId, req.user.tenantId); if (deleteResult.deletedCount == 0) return res.code(404).send({ error: "resource not found" }); return res.code(204).send(); } catch (err) { return err; } }