2026-05-02 23:17:08 +02:00
|
|
|
import { NextResponse } from 'next/server';
|
|
|
|
|
|
|
|
|
|
import type { AuthContext } from '@/lib/api/helpers';
|
|
|
|
|
import { parseQuery } from '@/lib/api/route-helpers';
|
|
|
|
|
import { errorResponse } from '@/lib/errors';
|
2026-05-25 15:09:35 +02:00
|
|
|
import { listTenancies } from '@/lib/services/berth-tenancies.service';
|
|
|
|
|
import { listTenanciesSchema } from '@/lib/validators/tenancies';
|
2026-05-02 23:17:08 +02:00
|
|
|
|
|
|
|
|
/**
|
2026-05-25 15:09:35 +02:00
|
|
|
* Port-scoped global list of tenancies across all berths. Inner handler
|
2026-05-02 23:17:08 +02:00
|
|
|
* 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<NextResponse> {
|
|
|
|
|
try {
|
2026-05-25 15:09:35 +02:00
|
|
|
const query = parseQuery(req as never, listTenanciesSchema);
|
|
|
|
|
const result = await listTenancies(ctx.portId, query);
|
2026-05-02 23:17:08 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|