implementado el manejo de errores correcto de prisma
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import express from 'express';
|
||||
const router = express.Router();
|
||||
import { PrismaClient } from '../../prisma/generated/client/index.js';
|
||||
import { PrismaClient, Prisma } from '../../prisma/generated/client/index.js';
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// GET all tareas
|
||||
@@ -8,9 +8,9 @@ router.get('/', async (req, res) => {
|
||||
try {
|
||||
const tareas = await prisma.tareaRealizada.findMany();
|
||||
res.json(tareas);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ message: 'Error al obtener tareas.' });
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
res.status(500).json({ message: 'Error al obtener tareas.' });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -26,8 +26,8 @@ router.get('/:id', async (req, res) => {
|
||||
} else {
|
||||
res.status(404).json({ message: 'Tarea no encontrada.' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
res.status(500).json({ message: 'Error al obtener tarea.' });
|
||||
}
|
||||
});
|
||||
@@ -44,42 +44,45 @@ router.post('/', async (req, res) => {
|
||||
fecha,
|
||||
tipo,
|
||||
fecha_anulado,
|
||||
creador_id, // Should ideally be from authenticated user
|
||||
creador_id,
|
||||
anulador_id,
|
||||
} = req.body;
|
||||
|
||||
try {
|
||||
// Basic validation
|
||||
if (!empleado_id || !titulo || !fecha) {
|
||||
return res.status(400).json({ message: 'Los campos empleado_id, titulo y fecha son obligatorios.' });
|
||||
}
|
||||
if (!empleado_id || !titulo || !fecha) {
|
||||
return res.status(400).json({
|
||||
message: 'Los campos empleado_id, titulo y fecha son obligatorios.',
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const nuevaTarea = await prisma.tareaRealizada.create({
|
||||
data: {
|
||||
empleado_id: parseInt(empleado_id),
|
||||
planilla_id: planilla_id ? parseInt(planilla_id) : null,
|
||||
titulo,
|
||||
precio: precio ? parseFloat(precio) : null,
|
||||
estado: estado || 'pendiente', // Default to 'pendiente' if not provided
|
||||
estado: estado || 'pendiente',
|
||||
observacion,
|
||||
fecha: new Date(fecha),
|
||||
tipo: tipo || '', // Default to empty string if not provided
|
||||
tipo: tipo || '',
|
||||
fecha_anulado: fecha_anulado ? new Date(fecha_anulado) : null,
|
||||
creador_id,
|
||||
creador_id,
|
||||
anulador_id,
|
||||
},
|
||||
});
|
||||
res.status(201).json(nuevaTarea);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
if (error.code === 'P2003') { // Foreign key constraint failed
|
||||
if (error.meta?.field_name?.includes('empleado_id')) {
|
||||
return res.status(400).json({ message: 'El empleado_id proporcionado no existe.' });
|
||||
}
|
||||
if (error.meta?.field_name?.includes('planilla_id')) {
|
||||
return res.status(400).json({ message: 'El planilla_id proporcionado no existe.' });
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (e.code === 'P2003') {
|
||||
if (e.meta?.field_name?.includes('empleado_id')) {
|
||||
return res.status(400).json({ message: 'El empleado_id proporcionado no existe.' });
|
||||
}
|
||||
if (e.meta?.field_name?.includes('planilla_id')) {
|
||||
return res.status(400).json({ message: 'El planilla_id proporcionado no existe.' });
|
||||
}
|
||||
}
|
||||
}
|
||||
console.error(e);
|
||||
res.status(500).json({ message: 'Error al crear tarea.' });
|
||||
}
|
||||
});
|
||||
@@ -112,26 +115,27 @@ router.put('/:id', async (req, res) => {
|
||||
if (tipo !== undefined) updateData.tipo = tipo;
|
||||
if (fecha_anulado !== undefined) updateData.fecha_anulado = fecha_anulado ? new Date(fecha_anulado) : null;
|
||||
if (anulador_id !== undefined) updateData.anulador_id = anulador_id;
|
||||
// creador_id is typically not updated.
|
||||
|
||||
const tareaActualizada = await prisma.tareaRealizada.update({
|
||||
where: { id: parseInt(id) },
|
||||
data: updateData,
|
||||
});
|
||||
res.json(tareaActualizada);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
if (error.code === 'P2025') { // Record to update not found
|
||||
return res.status(404).json({ message: 'Tarea no encontrada para actualizar.' });
|
||||
}
|
||||
if (error.code === 'P2003') { // Foreign key constraint failed
|
||||
if (error.meta?.field_name?.includes('empleado_id')) {
|
||||
return res.status(400).json({ message: 'El empleado_id proporcionado no existe.' });
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (e.code === 'P2025') {
|
||||
return res.status(404).json({ message: 'Tarea no encontrada para actualizar.' });
|
||||
}
|
||||
if (error.meta?.field_name?.includes('planilla_id')) {
|
||||
return res.status(400).json({ message: 'El planilla_id proporcionado no existe.' });
|
||||
if (e.code === 'P2003') {
|
||||
if (e.meta?.field_name?.includes('empleado_id')) {
|
||||
return res.status(400).json({ message: 'El empleado_id proporcionado no existe.' });
|
||||
}
|
||||
if (e.meta?.field_name?.includes('planilla_id')) {
|
||||
return res.status(400).json({ message: 'El planilla_id proporcionado no existe.' });
|
||||
}
|
||||
}
|
||||
}
|
||||
console.error(e);
|
||||
res.status(500).json({ message: 'Error al actualizar tarea.' });
|
||||
}
|
||||
});
|
||||
@@ -143,12 +147,14 @@ router.delete('/:id', async (req, res) => {
|
||||
await prisma.tareaRealizada.delete({
|
||||
where: { id: parseInt(id) },
|
||||
});
|
||||
res.status(204).send(); // No content
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
if (error.code === 'P2025') { // Record to delete not found
|
||||
return res.status(404).json({ message: 'Tarea no encontrada para eliminar.' });
|
||||
res.status(204).send();
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (e.code === 'P2025') {
|
||||
return res.status(404).json({ message: 'Tarea no encontrada para eliminar.' });
|
||||
}
|
||||
}
|
||||
console.error(e);
|
||||
res.status(500).json({ message: 'Error al eliminar tarea.' });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user