import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { requireAuth } from '$lib/server/middleware/authenticate.js'; import * as notificationService from '$lib/server/services/notificationService.js'; import { updateNotificationChannelSchema } from '$lib/utils/validators.js'; import { success, error } from '$lib/server/utils/response.js'; /** * GET /api/notifications/channels/:id — Get a single notification channel. */ export const GET: RequestHandler = async (event) => { const user = requireAuth(event); const { id } = event.params; try { const channel = await notificationService.getChannelById(id, user.id); return json(success(channel)); } catch (err) { const message = err instanceof Error ? err.message : 'Channel not found'; return json(error(message), { status: 404 }); } }; /** * PATCH /api/notifications/channels/:id — Update a notification channel. */ export const PATCH: RequestHandler = async (event) => { const user = requireAuth(event); const { id } = event.params; let body: unknown; try { body = await event.request.json(); } catch { return json(error('Invalid JSON body'), { status: 400 }); } const parsed = updateNotificationChannelSchema.safeParse(body); if (!parsed.success) { const messages = parsed.error.errors.map((e) => e.message).join(', '); return json(error(messages), { status: 400 }); } try { const channel = await notificationService.updateChannel(id, user.id, parsed.data); return json(success(channel)); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to update channel'; const status = message.includes('not found') ? 404 : 500; return json(error(message), { status }); } }; /** * DELETE /api/notifications/channels/:id — Delete a notification channel. */ export const DELETE: RequestHandler = async (event) => { const user = requireAuth(event); const { id } = event.params; try { await notificationService.deleteChannel(id, user.id); return json(success(null)); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to delete channel'; const status = message.includes('not found') ? 404 : 500; return json(error(message), { status }); } };