40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
|
|
import { NextResponse } from 'next/server';
|
||
|
|
import { z } from 'zod';
|
||
|
|
|
||
|
|
import { withAuth, withPermission } from '@/lib/api/helpers';
|
||
|
|
import { parseBody } from '@/lib/api/route-helpers';
|
||
|
|
import { errorResponse } from '@/lib/errors';
|
||
|
|
import { addDocumentWatcher, listDocumentWatchers } from '@/lib/services/documents.service';
|
||
|
|
|
||
|
|
const addWatcherSchema = z.object({
|
||
|
|
userId: z.string().min(1),
|
||
|
|
});
|
||
|
|
|
||
|
|
export const GET = withAuth(
|
||
|
|
withPermission('documents', 'view', async (_req, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const watchers = await listDocumentWatchers(params.id!, ctx.portId);
|
||
|
|
return NextResponse.json({ data: watchers });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
export const POST = withAuth(
|
||
|
|
withPermission('documents', 'edit', async (req, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const body = await parseBody(req, addWatcherSchema);
|
||
|
|
const watcher = await addDocumentWatcher(params.id!, ctx.portId, body.userId, {
|
||
|
|
userId: ctx.userId,
|
||
|
|
portId: ctx.portId,
|
||
|
|
ipAddress: ctx.ipAddress,
|
||
|
|
userAgent: ctx.userAgent,
|
||
|
|
});
|
||
|
|
return NextResponse.json({ data: watcher }, { status: 201 });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|