feat: marca Uriel Jareth Consulting, API segura para agentes, aprobacion humana y webhooks HMAC
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { requireStaff } from "@/lib/auth";
|
||||
import { applyHeaders, requireActor } from "@/lib/agent-auth";
|
||||
import { withIdempotency } from "@/lib/idempotency";
|
||||
import { registrarAuditoria } from "@/lib/audit";
|
||||
import { clienteSchema } from "@/lib/schemas";
|
||||
import { buscarDuplicado, clienteSelect, mensajeDuplicado } from "@/lib/clientes";
|
||||
|
||||
@@ -8,10 +10,12 @@ import { buscarDuplicado, clienteSelect, mensajeDuplicado } from "@/lib/clientes
|
||||
// email y RFC.
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const session = await requireStaff();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "No autorizado" }, { status: 403 });
|
||||
}
|
||||
const auth = await requireActor(request, {
|
||||
scopes: ["clientes:leer"],
|
||||
recursoTipo: "cliente",
|
||||
});
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const q = request.nextUrl.searchParams.get("q")?.trim();
|
||||
const clientes = await prisma.cliente.findMany({
|
||||
where: q
|
||||
@@ -27,41 +31,58 @@ export async function GET(request: NextRequest) {
|
||||
orderBy: { nombre: "asc" },
|
||||
select: clienteSelect,
|
||||
});
|
||||
return NextResponse.json(clientes);
|
||||
return applyHeaders(NextResponse.json(clientes), auth.rateLimitHeaders);
|
||||
} catch (error: unknown) {
|
||||
const msg = error instanceof Error ? error.message : "Error interno";
|
||||
return NextResponse.json({ error: msg }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/clientes — alta directa de un cliente, sin pasar por una cotizacion
|
||||
// (ej. para programarle cobros recurrentes). Solo staff.
|
||||
// POST /api/clientes — alta directa de un cliente (staff o agente con clientes:escribir).
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await requireStaff();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "No autorizado" }, { status: 403 });
|
||||
}
|
||||
const auth = await requireActor(request, {
|
||||
scopes: ["clientes:escribir"],
|
||||
recursoTipo: "cliente",
|
||||
});
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const body = await request.json();
|
||||
const parsed = clienteSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: parsed.error.issues.map((i) => i.message).join(", ") },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const data = parsed.data;
|
||||
const { actor, rateLimitHeaders } = auth;
|
||||
const actorKey = actor.type === "agent" ? `agent:${actor.keyId}` : `user:${actor.userId}`;
|
||||
|
||||
if (await buscarDuplicado(data.nombre, data.empresa)) {
|
||||
return NextResponse.json(
|
||||
{ error: mensajeDuplicado(data.nombre, data.empresa) },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
const res = await withIdempotency(request, actorKey, async ({ body }) => {
|
||||
const parsed = clienteSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: parsed.error.issues.map((i) => i.message).join(", ") },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const data = parsed.data;
|
||||
|
||||
const cliente = await prisma.cliente.create({ data, select: clienteSelect });
|
||||
return NextResponse.json(cliente, { status: 201 });
|
||||
if (await buscarDuplicado(data.nombre, data.empresa)) {
|
||||
return NextResponse.json(
|
||||
{ error: mensajeDuplicado(data.nombre, data.empresa) },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
const cliente = await prisma.cliente.create({ data, select: clienteSelect });
|
||||
|
||||
await registrarAuditoria({
|
||||
actor,
|
||||
accion: "cliente.crear",
|
||||
recursoTipo: "cliente",
|
||||
recursoId: cliente.id,
|
||||
request,
|
||||
exito: true,
|
||||
detalle: { nombre: cliente.nombre, empresa: cliente.empresa },
|
||||
});
|
||||
|
||||
return NextResponse.json(cliente, { status: 201 });
|
||||
});
|
||||
|
||||
return applyHeaders(res, rateLimitHeaders);
|
||||
} catch (error: unknown) {
|
||||
const msg = error instanceof Error ? error.message : "Error interno";
|
||||
return NextResponse.json({ error: msg }, { status: 500 });
|
||||
|
||||
Reference in New Issue
Block a user