Files
planilla/api/routes/planillas/planillas.js
josedario87 97b879433d
Some checks failed
build-and-deploy / filter (push) Successful in 2s
Sync to GitHub / sync (push) Failing after 1s
build-and-deploy / build (push) Successful in 9s
build-and-deploy / deploy (push) Successful in 14s
implementado el manejo de errores correcto de prisma
2025-05-30 12:44:51 -06:00

145 lines
4.2 KiB
JavaScript

import express from 'express';
const router = express.Router();
import { PrismaClient, Prisma } from '../../prisma/generated/client/index.js';
const prisma = new PrismaClient();
// GET all planillas
router.get('/', async (req, res) => {
try {
const planillas = await prisma.planilla.findMany();
res.json(planillas);
} catch (e) {
console.error(e);
res.status(500).json({ message: 'Error al obtener planillas.' });
}
});
// GET planilla by ID
router.get('/:id', async (req, res) => {
const { id } = req.params;
try {
const planilla = await prisma.planilla.findUnique({
where: { id: parseInt(id) },
});
if (planilla) {
res.json(planilla);
} else {
res.status(404).json({ message: 'Planilla no encontrada.' });
}
} catch (e) {
console.error(e);
res.status(500).json({ message: 'Error al obtener planilla.' });
}
});
// POST create new planilla
router.post('/', async (req, res) => {
const {
empleado_id,
fecha_desde,
fecha_hasta,
titulo,
total,
estado,
fecha_anulado,
creador_id,
anulador_id,
} = req.body;
if (!empleado_id || !fecha_desde || !fecha_hasta || !titulo) {
return res.status(400).json({
message: 'Los campos empleado_id, fecha_desde, fecha_hasta y titulo son obligatorios.',
});
}
try {
const nuevaPlanilla = await prisma.planilla.create({
data: {
empleado_id: parseInt(empleado_id),
fecha_desde: new Date(fecha_desde),
fecha_hasta: new Date(fecha_hasta),
titulo,
total: total ? parseFloat(total) : null,
estado: estado || 'pagado',
fecha_anulado: fecha_anulado ? new Date(fecha_anulado) : null,
creador_id,
anulador_id,
},
});
res.status(201).json(nuevaPlanilla);
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
if (e.code === 'P2003' && e.meta?.field_name?.includes('empleado_id')) {
return res.status(400).json({ message: 'El empleado_id proporcionado no existe.' });
}
}
console.error(e);
res.status(500).json({ message: 'Error al crear planilla.' });
}
});
// PUT update planilla by ID
router.put('/:id', async (req, res) => {
const { id } = req.params;
const {
empleado_id,
fecha_desde,
fecha_hasta,
titulo,
total,
estado,
fecha_anulado,
anulador_id,
} = req.body;
try {
const updateData = {};
if (empleado_id !== undefined) updateData.empleado_id = parseInt(empleado_id);
if (fecha_desde !== undefined) updateData.fecha_desde = new Date(fecha_desde);
if (fecha_hasta !== undefined) updateData.fecha_hasta = new Date(fecha_hasta);
if (titulo !== undefined) updateData.titulo = titulo;
if (total !== undefined) updateData.total = total ? parseFloat(total) : null;
if (estado !== undefined) updateData.estado = estado;
if (fecha_anulado !== undefined) updateData.fecha_anulado = fecha_anulado ? new Date(fecha_anulado) : null;
if (anulador_id !== undefined) updateData.anulador_id = anulador_id;
const planillaActualizada = await prisma.planilla.update({
where: { id: parseInt(id) },
data: updateData,
});
res.json(planillaActualizada);
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
if (e.code === 'P2025') {
return res.status(404).json({ message: 'Planilla no encontrada para actualizar.' });
}
if (e.code === 'P2003' && e.meta?.field_name?.includes('empleado_id')) {
return res.status(400).json({ message: 'El empleado_id proporcionado no existe.' });
}
}
console.error(e);
res.status(500).json({ message: 'Error al actualizar planilla.' });
}
});
// DELETE planilla by ID
router.delete('/:id', async (req, res) => {
const { id } = req.params;
try {
await prisma.planilla.delete({
where: { id: parseInt(id) },
});
res.status(204).send();
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
if (e.code === 'P2025') {
return res.status(404).json({ message: 'Planilla no encontrada para eliminar.' });
}
}
console.error(e);
res.status(500).json({ message: 'Error al eliminar planilla.' });
}
});
export default router;