import { NextResponse } from 'next/server'; import type { AuthContext } from '@/lib/api/helpers'; import { parseQuery } from '@/lib/api/route-helpers'; import { errorResponse } from '@/lib/errors'; import { listTenancies } from '@/lib/services/berth-tenancies.service'; import { listTenanciesSchema } from '@/lib/validators/tenancies'; /** * Port-scoped global list of tenancies across all berths. Inner handler * lives here so it can be invoked directly from integration tests without * the `withAuth(withPermission(...))` wrappers (matches the convention * used throughout `src/app/api/v1/*`). */ export async function listHandler(req: Request, ctx: AuthContext): Promise { try { const query = parseQuery(req as never, listTenanciesSchema); const result = await listTenancies(ctx.portId, query); const { page, limit } = query; const totalPages = Math.ceil(result.total / limit); return NextResponse.json({ data: result.data, pagination: { page, pageSize: limit, total: result.total, totalPages, hasNextPage: page < totalPages, hasPreviousPage: page > 1, }, }); } catch (error) { return errorResponse(error); } }