60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
|
|
/**
|
|||
|
|
* Notification Routes – user notifications
|
|||
|
|
*/
|
|||
|
|
import type { FastifyInstance } from 'fastify';
|
|||
|
|
import type { DatabaseInterface } from '../database/DatabaseInterface.js';
|
|||
|
|
import { requireAuth } from '../auth/authMiddleware.js';
|
|||
|
|
import type { AuthService } from '../auth/AuthService.js';
|
|||
|
|
|
|||
|
|
export function registerNotificationRoutes(fastify: FastifyInstance, db: DatabaseInterface, authService: AuthService) {
|
|||
|
|
// List notifications for current user
|
|||
|
|
fastify.get('/api/notifications', async (request, reply) => {
|
|||
|
|
const user = requireAuth(request, reply, authService);
|
|||
|
|
if (!user) return;
|
|||
|
|
return db.listNotifications(user.id);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Create a notification
|
|||
|
|
fastify.post('/api/notifications', async (request, reply) => {
|
|||
|
|
const user = requireAuth(request, reply, authService);
|
|||
|
|
if (!user) return;
|
|||
|
|
const body = request.body as { type?: string; title: string; message?: string; project_id?: string };
|
|||
|
|
if (!body.title) return reply.code(400).send({ error: 'Title is required' });
|
|||
|
|
return db.createNotification({
|
|||
|
|
user_id: user.id,
|
|||
|
|
project_id: body.project_id ?? null,
|
|||
|
|
type: body.type ?? 'info',
|
|||
|
|
title: body.title,
|
|||
|
|
message: body.message ?? null,
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Mark single notification as read
|
|||
|
|
fastify.put('/api/notifications/:id/read', async (request, reply) => {
|
|||
|
|
const user = requireAuth(request, reply, authService);
|
|||
|
|
if (!user) return;
|
|||
|
|
const { id } = request.params as { id: string };
|
|||
|
|
const ok = db.markNotificationRead(id);
|
|||
|
|
if (!ok) return reply.code(404).send({ error: 'Notification not found' });
|
|||
|
|
return { success: true };
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Mark all notifications as read
|
|||
|
|
fastify.put('/api/notifications/read-all', async (request, reply) => {
|
|||
|
|
const user = requireAuth(request, reply, authService);
|
|||
|
|
if (!user) return;
|
|||
|
|
db.markAllNotificationsRead(user.id);
|
|||
|
|
return { success: true };
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Delete a notification
|
|||
|
|
fastify.delete('/api/notifications/:id', async (request, reply) => {
|
|||
|
|
const user = requireAuth(request, reply, authService);
|
|||
|
|
if (!user) return;
|
|||
|
|
const { id } = request.params as { id: string };
|
|||
|
|
const ok = db.deleteNotification(id);
|
|||
|
|
if (!ok) return reply.code(404).send({ error: 'Notification not found' });
|
|||
|
|
return { success: true };
|
|||
|
|
});
|
|||
|
|
}
|