2026-05-29 15:52:39 +02:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
|
|
|
|
import { auth } from "@/src/auth";
|
2026-05-31 17:41:45 +02:00
|
|
|
import { requirePermission } from "@/src/lib/auth/requireSession";
|
2026-05-29 15:52:39 +02:00
|
|
|
import {
|
|
|
|
|
getMacchinaById,
|
|
|
|
|
updateMacchina,
|
|
|
|
|
deleteMacchina,
|
|
|
|
|
} from "@/src/lib/macchine";
|
|
|
|
|
import type { MacchinaUpdateInput } from "@/src/types/macchina";
|
|
|
|
|
|
|
|
|
|
interface RouteParams {
|
|
|
|
|
params: Promise<{ id: string }>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function GET(request: Request, { params }: RouteParams) {
|
|
|
|
|
const session = await auth();
|
|
|
|
|
|
|
|
|
|
if (!session?.user) {
|
|
|
|
|
return NextResponse.json({ error: "Non autorizzato" }, { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
const macchinaId = parseInt(id, 10);
|
|
|
|
|
|
|
|
|
|
if (isNaN(macchinaId)) {
|
|
|
|
|
return NextResponse.json({ error: "ID non valido" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const macchina = await getMacchinaById(macchinaId);
|
|
|
|
|
|
|
|
|
|
if (!macchina) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Macchina non trovata" },
|
|
|
|
|
{ status: 404 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ macchina });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("[API macchine/:id] Errore GET:", error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Errore nel recupero della macchina" },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function PUT(request: Request, { params }: RouteParams) {
|
2026-05-31 17:41:45 +02:00
|
|
|
const session = await requirePermission((p) => p.canEditMacchine);
|
|
|
|
|
if (session instanceof NextResponse) return session;
|
2026-05-29 15:52:39 +02:00
|
|
|
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
const macchinaId = parseInt(id, 10);
|
|
|
|
|
|
|
|
|
|
if (isNaN(macchinaId)) {
|
|
|
|
|
return NextResponse.json({ error: "ID non valido" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
|
|
|
|
|
const input: MacchinaUpdateInput = {};
|
|
|
|
|
if (body.nome !== undefined) input.nome = body.nome;
|
|
|
|
|
if (body.tipo !== undefined) input.tipo = body.tipo;
|
|
|
|
|
if (body.id_padre !== undefined) input.id_padre = body.id_padre;
|
|
|
|
|
if (body.id_stabilimento !== undefined) input.id_stabilimento = body.id_stabilimento;
|
|
|
|
|
if (body.colore_testo !== undefined) input.colore_testo = body.colore_testo;
|
|
|
|
|
|
|
|
|
|
const macchina = await updateMacchina(macchinaId, input);
|
|
|
|
|
|
|
|
|
|
if (!macchina) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Macchina non trovata" },
|
|
|
|
|
{ status: 404 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ macchina });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("[API macchine/:id] Errore PUT:", error);
|
|
|
|
|
const message = error instanceof Error ? error.message : "Errore nell'aggiornamento";
|
|
|
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function DELETE(request: Request, { params }: RouteParams) {
|
|
|
|
|
const session = await auth();
|
|
|
|
|
|
|
|
|
|
if (!session?.user) {
|
|
|
|
|
return NextResponse.json({ error: "Non autorizzato" }, { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
const macchinaId = parseInt(id, 10);
|
|
|
|
|
|
|
|
|
|
if (isNaN(macchinaId)) {
|
|
|
|
|
return NextResponse.json({ error: "ID non valido" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-31 11:54:06 +02:00
|
|
|
// Leggi il body per ottenere il magazzino di rientro (opzionale)
|
|
|
|
|
let idMagazzinoRientro: number | undefined;
|
|
|
|
|
try {
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
if (body.idMagazzinoRientro) {
|
|
|
|
|
idMagazzinoRientro = parseInt(body.idMagazzinoRientro, 10);
|
|
|
|
|
if (isNaN(idMagazzinoRientro)) {
|
|
|
|
|
idMagazzinoRientro = undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Body vuoto o non JSON, continua senza magazzino
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 17:41:45 +02:00
|
|
|
const user = session.user.email || session.user.utente || "unknown";
|
2026-05-31 11:54:06 +02:00
|
|
|
const deleted = await deleteMacchina(macchinaId, idMagazzinoRientro, user);
|
2026-05-29 15:52:39 +02:00
|
|
|
|
|
|
|
|
if (!deleted) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Macchina non trovata" },
|
|
|
|
|
{ status: 404 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ success: true });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("[API macchine/:id] Errore DELETE:", error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Errore nell'eliminazione" },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|