import { NextResponse } from 'next/server'; import { withAuth, withPermission } from '@/lib/api/helpers'; import { parseBody } from '@/lib/api/route-helpers'; import { updateBerthSchema, archiveBerthSchema } from '@/lib/validators/berths'; import { getBerthById, updateBerth, archiveBerth } from '@/lib/services/berths.service'; import { errorResponse } from '@/lib/errors'; // GET /api/v1/berths/[id] export const GET = withAuth( withPermission('berths', 'view', async (req, ctx, params) => { try { const berth = await getBerthById(params.id!, ctx.portId); return NextResponse.json({ data: berth }); } catch (error) { return errorResponse(error); } }), ); // PATCH /api/v1/berths/[id] export const PATCH = withAuth( withPermission('berths', 'edit', async (req, ctx, params) => { try { const body = await parseBody(req, updateBerthSchema); const updated = await updateBerth(params.id!, ctx.portId, body, { userId: ctx.userId, portId: ctx.portId, ipAddress: ctx.ipAddress, userAgent: ctx.userAgent, }); return NextResponse.json({ data: updated }); } catch (error) { return errorResponse(error); } }), ); // DELETE /api/v1/berths/[id] // Post-audit F5: this is a SOFT-ARCHIVE, not a hard delete. The body // must carry `{ reason: string (>=5 chars) }`. Use POST /restore to // reverse. Archive is blocked when an active interest is still linked. export const DELETE = withAuth( withPermission('berths', 'edit', async (req, ctx, params) => { try { const body = await parseBody(req, archiveBerthSchema); await archiveBerth(params.id!, ctx.portId, body, { userId: ctx.userId, portId: ctx.portId, ipAddress: ctx.ipAddress, userAgent: ctx.userAgent, }); return new NextResponse(null, { status: 204 }); } catch (error) { return errorResponse(error); } }), );