Files
base/src/routes/api/notifications/[id]/+server.ts
T

35 lines
1.2 KiB
TypeScript

import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const PATCH: RequestHandler = async ({ params, request, locals }) => {
if (!locals.user) {
return json({ error: 'Unauthorized' }, { status: 401 });
}
const id = params.id;
if (!id) {
return json({ error: 'Missing notification id' }, { status: 400 });
}
let body: { isRead?: boolean; isDeleted?: boolean } = {};
try {
body = await request.json();
} catch {
body = {};
}
try {
const record = await locals.pb.collection('notifications').getOne(id);
if ((record as Record<string, unknown>).user !== locals.user.id) {
return json({ error: 'Forbidden' }, { status: 403 });
}
const updates: Record<string, boolean> = {};
if (typeof body.isRead === 'boolean') updates.isRead = body.isRead;
if (typeof body.isDeleted === 'boolean') updates.isDeleted = body.isDeleted;
if (Object.keys(updates).length === 0) {
return json({ error: 'No updates' }, { status: 400 });
}
await locals.pb.collection('notifications').update(id, updates);
return json({ ok: true, ...updates });
} catch {
return json({ error: 'Notification not found or update failed' }, { status: 404 });
}
};