f4df90ac1e
Revisión con IA / ai-review (pull_request) Successful in 1m24s
Las tres sesiones (admin, cliente, jefe) se firman con el mismo secreto, asi que verificar solo la firma alcanzaba para que un token de un rol pasara por la cookie de otro. Verificado antes del cambio: un token de admin en cliente_session daba 200 en /api/cliente/autos, y uno de jefe cargaba /cliente/dashboard. Despues: 401 y 307. - Las 26 llamadas a verificarSesion pasan el rol esperado. - El middleware, que hacia su propio jwtVerify sin mirar el rol, ahora tambien lo comprueba. - Se sacan las comprobaciones sueltas de rol en las rutas de jefe, que quedaron redundantes. Matriz probada contra la app: los tres roles siguen entrando en su area (200) y ninguno entra en la de los otros, ni por API (401) ni por pagina (307). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { verificarSesion } from "@/lib/auth-admin";
|
|
import { crearTurno } from "@/lib/turnos";
|
|
|
|
export async function GET() {
|
|
const sesion = await verificarSesion("cliente_session", "cliente");
|
|
if (!sesion) return NextResponse.json({ error: "No autorizado" }, { status: 401 });
|
|
|
|
const turnos = await prisma.turno.findMany({
|
|
where: { clienteId: sesion.sub },
|
|
include: { auto: true, tipoServicio: true, jefeMecanico: { select: { id: true, nombre: true } } },
|
|
orderBy: { fechaInicio: "desc" },
|
|
});
|
|
|
|
return NextResponse.json(turnos);
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const sesion = await verificarSesion("cliente_session", "cliente");
|
|
if (!sesion) return NextResponse.json({ error: "No autorizado" }, { status: 401 });
|
|
|
|
const { autoId, tipoServicioId, jefeMecanicoId, fechaInicio, kilometros } = await req.json();
|
|
if (!autoId || !tipoServicioId || !jefeMecanicoId || !fechaInicio || !kilometros)
|
|
return NextResponse.json({ error: "Datos incompletos" }, { status: 400 });
|
|
|
|
const km = Number(kilometros);
|
|
if (!Number.isInteger(km) || km <= 0 || km > 1000000)
|
|
return NextResponse.json({ error: "Kilometraje inválido (debe ser entre 1 y 1.000.000)" }, { status: 400 });
|
|
|
|
// Verificar que el auto pertenece al cliente
|
|
const auto = await prisma.auto.findFirst({
|
|
where: { id: autoId, clienteId: sesion.sub },
|
|
});
|
|
if (!auto) return NextResponse.json({ error: "Auto no encontrado" }, { status: 404 });
|
|
|
|
const resultado = await crearTurno({
|
|
clienteId: sesion.sub,
|
|
jefeMecanicoId,
|
|
tipoServicioId,
|
|
fechaInicio: new Date(fechaInicio),
|
|
autoId,
|
|
kilometros: km,
|
|
});
|
|
|
|
if (!resultado.ok)
|
|
return NextResponse.json({ error: resultado.error }, { status: resultado.status });
|
|
|
|
return NextResponse.json(resultado.turno, { status: 201 });
|
|
}
|