41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
|
|
import { NextResponse } from 'next/server';
|
||
|
|
|
||
|
|
import { withAuth, withPermission } from '@/lib/api/helpers';
|
||
|
|
import { parseBody } from '@/lib/api/route-helpers';
|
||
|
|
import { createNoteSchema } from '@/lib/validators/notes';
|
||
|
|
import * as notesService from '@/lib/services/notes.service';
|
||
|
|
import { errorResponse, NotFoundError } from '@/lib/errors';
|
||
|
|
|
||
|
|
export const GET = withAuth(
|
||
|
|
withPermission('residential_clients', 'view', async (_req, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const id = params.id;
|
||
|
|
if (!id) throw new NotFoundError('Residential client');
|
||
|
|
const notes = await notesService.listForEntity(ctx.portId, 'residential_clients', id);
|
||
|
|
return NextResponse.json({ data: notes });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
export const POST = withAuth(
|
||
|
|
withPermission('residential_clients', 'edit', async (req, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const id = params.id;
|
||
|
|
if (!id) throw new NotFoundError('Residential client');
|
||
|
|
const body = await parseBody(req, createNoteSchema);
|
||
|
|
const note = await notesService.create(
|
||
|
|
ctx.portId,
|
||
|
|
'residential_clients',
|
||
|
|
id,
|
||
|
|
ctx.userId,
|
||
|
|
body,
|
||
|
|
);
|
||
|
|
return NextResponse.json({ data: note }, { status: 201 });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|