84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { applyHeaders, requireActor } from "@/lib/agent-auth";
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params;
|
|
const auth = await requireActor(request, {
|
|
scopes: ["catalogo:leer"],
|
|
recursoTipo: "paquete",
|
|
recursoId: id,
|
|
});
|
|
if (!auth.ok) return auth.response;
|
|
|
|
const paquete = await prisma.paquete.findUnique({
|
|
where: { id },
|
|
include: {
|
|
fases: {
|
|
orderBy: { orden: "asc" },
|
|
include: {
|
|
servicios: {
|
|
include: { servicio: { include: { categoriaRel: true } } },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (!paquete) return applyHeaders(NextResponse.json({ error: "No encontrado" }, { status: 404 }), auth.rateLimitHeaders);
|
|
const salida =
|
|
auth.actor.type === "agent"
|
|
? {
|
|
...paquete,
|
|
fases: paquete.fases.map((f) => ({
|
|
...f,
|
|
servicios: f.servicios.filter((sp) => !sp.servicio?.soloAdmin),
|
|
})),
|
|
}
|
|
: paquete;
|
|
return applyHeaders(NextResponse.json(salida), auth.rateLimitHeaders);
|
|
} catch (error: unknown) {
|
|
const msg = error instanceof Error ? error.message : "Error interno";
|
|
return NextResponse.json({ error: msg }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
const paquete = await prisma.paquete.update({
|
|
where: { id },
|
|
data: {
|
|
nombre: body.nombre,
|
|
descripcion: body.descripcion || null,
|
|
activo: body.activo !== false,
|
|
},
|
|
});
|
|
return NextResponse.json(paquete);
|
|
} catch (error: unknown) {
|
|
const msg = error instanceof Error ? error.message : "Error interno";
|
|
return NextResponse.json({ error: msg }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params;
|
|
await prisma.paquete.delete({ where: { id } });
|
|
return NextResponse.json({ ok: true });
|
|
} catch (error: unknown) {
|
|
const msg = error instanceof Error ? error.message : "Error interno";
|
|
return NextResponse.json({ error: msg }, { status: 500 });
|
|
}
|
|
}
|