feat: add notifications and project shares features

- Backend: notifications + shares routes, DB tables, SqliteAdapter methods
- Frontend: NotificationPanel (bell icon + dropdown), ShareDialog, Dashboard integration
- Styles: notification + share dialog CSS
This commit is contained in:
2026-06-28 13:52:54 +02:00
parent a4371ca3ce
commit 20432f4b47
11 changed files with 655 additions and 7 deletions
+63
View File
@@ -0,0 +1,63 @@
/**
* Notifications Routes List, create, mark read, delete notifications
*/
import type { FastifyInstance } from 'fastify';
import type { DatabaseInterface } from '../database/DatabaseInterface.js';
import type { AuthService } from '../auth/AuthService.js';
function extractToken(request: any): string | null {
const auth = request.headers?.authorization;
if (auth && auth.startsWith('Bearer ')) return auth.slice(7);
return null;
}
export function registerNotificationRoutes(fastify: FastifyInstance, db: DatabaseInterface, authService: AuthService) {
// List notifications for current user
fastify.get('/api/notifications', async (request, reply) => {
const token = extractToken(request);
if (!token) return reply.code(401).send({ error: 'Authentication required' });
const user = authService.getUserFromSession(token);
if (!user) return reply.code(401).send({ error: 'Invalid or expired session' });
return db.listNotifications(user.id);
});
// Create notification
fastify.post('/api/notifications', async (request, reply) => {
const token = extractToken(request);
if (!token) return reply.code(401).send({ error: 'Authentication required' });
const user = authService.getUserFromSession(token);
if (!user) return reply.code(401).send({ error: 'Invalid or expired session' });
const body = request.body as { type?: string; title?: string; message?: string; user_id?: string };
if (!body.title || !body.message) return reply.code(400).send({ error: 'title and message are required' });
return reply.code(201).send(db.createNotification({
user_id: body.user_id ?? user.id,
type: body.type ?? 'info',
title: body.title,
message: body.message,
}));
});
// Mark notification as read
fastify.patch('/api/notifications/:id/read', async (request, reply) => {
const token = extractToken(request);
if (!token) return reply.code(401).send({ error: 'Authentication required' });
const user = authService.getUserFromSession(token);
if (!user) return reply.code(401).send({ error: 'Invalid or expired session' });
const { id } = request.params as { id: string };
const ok = db.markNotificationRead(id);
if (!ok) return reply.code(404).send({ error: 'Notification not found' });
return reply.code(204).send();
});
// Delete notification
fastify.delete('/api/notifications/:id', async (request, reply) => {
const token = extractToken(request);
if (!token) return reply.code(401).send({ error: 'Authentication required' });
const user = authService.getUserFromSession(token);
if (!user) return reply.code(401).send({ error: 'Invalid or expired session' });
const { id } = request.params as { id: string };
const ok = db.deleteNotification(id);
if (!ok) return reply.code(404).send({ error: 'Notification not found' });
return reply.code(204).send();
});
}
+67
View File
@@ -0,0 +1,67 @@
/**
* Project Shares Routes List, create, delete project shares
*/
import type { FastifyInstance } from 'fastify';
import type { DatabaseInterface } from '../database/DatabaseInterface.js';
import type { AuthService } from '../auth/AuthService.js';
function extractToken(request: any): string | null {
const auth = request.headers?.authorization;
if (auth && auth.startsWith('Bearer ')) return auth.slice(7);
return null;
}
export function registerShareRoutes(fastify: FastifyInstance, db: DatabaseInterface, authService: AuthService) {
// List shares for a project
fastify.get('/api/projects/:projectId/shares', async (request, reply) => {
const token = extractToken(request);
if (!token) return reply.code(401).send({ error: 'Authentication required' });
const user = authService.getUserFromSession(token);
if (!user) return reply.code(401).send({ error: 'Invalid or expired session' });
const { projectId } = request.params as { projectId: string };
return db.listProjectShares(projectId);
});
// Create a project share
fastify.post('/api/projects/:projectId/shares', async (request, reply) => {
const token = extractToken(request);
if (!token) return reply.code(401).send({ error: 'Authentication required' });
const user = authService.getUserFromSession(token);
if (!user) return reply.code(401).send({ error: 'Invalid or expired session' });
const { projectId } = request.params as { projectId: string };
const body = request.body as { shared_with_email?: string; permission?: string };
if (!body.shared_with_email) return reply.code(400).send({ error: 'shared_with_email is required' });
const share = db.createProjectShare({
project_id: projectId,
shared_with_email: body.shared_with_email,
shared_by: user.id,
permission: body.permission ?? 'view',
});
// Create a notification for the shared user if they exist
const sharedUser = db.getUserByEmail(body.shared_with_email);
if (sharedUser) {
db.createNotification({
user_id: sharedUser.id,
type: 'share',
title: 'Projekt geteilt',
message: `${user.name} hat ein Projekt mit Ihnen geteilt: ${db.getProject(projectId)?.name ?? 'Unbenannt'}`,
});
}
return reply.code(201).send(share);
});
// Delete a project share
fastify.delete('/api/shares/:id', async (request, reply) => {
const token = extractToken(request);
if (!token) return reply.code(401).send({ error: 'Authentication required' });
const user = authService.getUserFromSession(token);
if (!user) return reply.code(401).send({ error: 'Invalid or expired session' });
const { id } = request.params as { id: string };
const ok = db.deleteProjectShare(id);
if (!ok) return reply.code(404).send({ error: 'Share not found' });
return reply.code(204).send();
});
}