Files
GPI/src/server/controllers/notificationController.ts

98 lines
3.7 KiB
TypeScript

import { Request, Response } from 'express';
import { notificationService } from '../services/notificationService.js';
export const notificationController = {
getUserNotifications: async (req: Request, res: Response) => {
try {
const organizationId = req.headers['x-organization-id'] as string;
const userId = req.headers['x-user-id'] as string; // Assumindo que temos o ID do usuário (clerkId ou email)
// Se não tiver userId no header (ainda não implementado auth full), tentar pegar do query ou usar um fallback
// Nota: Idealmente o middleware de auth popula req.user. Vamos assumir que passamos x-user-id no frontend por enquanto.
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID is required' });
}
const notifications = await notificationService.getUserNotifications(
userId,
organizationId,
req.query.includeArchived === 'true'
);
res.json(notifications);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error fetching notifications' });
}
},
markAsRead: async (req: Request, res: Response) => {
try {
const { id } = req.params;
const notification = await notificationService.markAsRead(id as string);
res.json(notification);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error marking notification as read' });
}
},
markAllAsRead: async (req: Request, res: Response) => {
try {
const organizationId = req.headers['x-organization-id'] as string;
const userId = req.headers['x-user-id'] as string;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID is required' });
}
await notificationService.markAllAsRead(userId, organizationId);
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error marking all as read' });
}
},
clearAll: async (req: Request, res: Response) => {
try {
const organizationId = req.headers['x-organization-id'] as string;
const userId = req.headers['x-user-id'] as string;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID is required' });
}
await notificationService.clearAll(userId, organizationId);
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error clearing all notifications' });
}
},
archive: async (req: Request, res: Response) => {
try {
const { id } = req.params;
const userId = req.headers['x-user-id'] as string;
const notification = await notificationService.archive(id as string, userId);
res.json(notification);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error archiving notification' });
}
},
delete: async (req: Request, res: Response) => {
try {
const { id } = req.params;
const userId = req.headers['x-user-id'] as string;
await notificationService.softDelete(id as string, userId);
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Error deleting notification' });
}
}
};