GET /api/areas, GET /api/inventory-scans, PATCH+DELETE /api/inventory-scans/[id], GET /api/inventory-scans/[id]/photo (proxies Telegram file_id on demand, derives Content-Type from file_path extension since Telegram serves application/octet-stream regardless of actual type). Co-Authored-By: Claude Sonnet 5 <[email protected]>
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { auth } from "@/auth";
|
|
import { createOrGetArea, listAreas, validateCreateAreaInput } from "@/lib/areas";
|
|
|
|
// Session-authed Area list/create for the review page — mirrors
|
|
// src/app/api/employees/route.ts's pattern. The bot uses the separate
|
|
// bot-secret-authed /api/bot/areas instead (see src/lib/bot-auth.ts).
|
|
export async function GET(): Promise<NextResponse> {
|
|
const session = await auth();
|
|
if (!session) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
const areas = await listAreas();
|
|
return NextResponse.json(areas, { status: 200 });
|
|
}
|
|
|
|
export async function POST(request: Request): Promise<NextResponse> {
|
|
const session = await auth();
|
|
if (!session) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
let body: unknown;
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Request body must be valid JSON" }, { status: 400 });
|
|
}
|
|
if (typeof body !== "object" || body === null) {
|
|
return NextResponse.json({ error: "Request body must be a JSON object" }, { status: 400 });
|
|
}
|
|
|
|
const { name, team } = body as Record<string, unknown>;
|
|
const validation = validateCreateAreaInput({ name, team });
|
|
if (!validation.ok) {
|
|
return NextResponse.json({ error: validation.error }, { status: 400 });
|
|
}
|
|
|
|
const created = await createOrGetArea(validation.value);
|
|
if (!created.ok) {
|
|
return NextResponse.json({ error: created.error }, { status: 400 });
|
|
}
|
|
return NextResponse.json(created.value, { status: 201 });
|
|
}
|