magricambi/src/app/api/magazzini/route.ts
AV77web 85e2256022
All checks were successful
Deploy to VPS / build (push) Successful in 39s
Deploy to VPS / deploy (push) Successful in 27s
inserita la gestiione stabilimenti
2026-05-31 17:05:18 +02:00

47 lines
1.3 KiB
TypeScript

import { NextResponse } from "next/server";
import { auth } from "@/src/auth";
import { listMagazzini, createMagazzino } from "@/src/lib/stabilimenti";
export async function GET(request: Request) {
const session = await auth();
if (!session?.user) {
return NextResponse.json({ error: "Non autorizzato" }, { status: 401 });
}
try {
const { searchParams } = new URL(request.url);
const stabilimentoId = searchParams.get("stabilimentoId");
const magazzini = await listMagazzini(
stabilimentoId ? parseInt(stabilimentoId, 10) : undefined
);
return NextResponse.json({ magazzini });
} catch (error) {
console.error("[API magazzini] Errore:", error);
return NextResponse.json(
{ error: "Errore nel recupero dei magazzini" },
{ status: 500 }
);
}
}
export async function POST(request: Request) {
const session = await auth();
if (!session?.user) {
return NextResponse.json({ error: "Non autorizzato" }, { status: 401 });
}
try {
const magazzino = await createMagazzino(await request.json());
return NextResponse.json({ magazzino }, { status: 201 });
} catch (error) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Errore creazione magazzino" },
{ status: 400 }
);
}
}