Adds Express.js routes and Prisma-based handlers for common database operations (Create, Read, Update, Delete) for the following modules: - Empleados (subset of Cliente model) - Asistencias - Tareas (TareaRealizada model) - Planillas Each module's routes are separated into their own files within `api/routes/`. The new routes are registered in `api/server.js`. Basic error handling, including try-catch blocks and checks for common Prisma errors (e.g., P2025 for record not found, P2003 for foreign key violations), has been implemented in each endpoint.
143 lines
4.2 KiB
JavaScript
143 lines
4.2 KiB
JavaScript
import express from 'express';
|
|
const router = express.Router();
|
|
import { PrismaClient } from '../../prisma/generated/client/index.js';
|
|
const prisma = new PrismaClient();
|
|
|
|
// GET all empleados
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const empleados = await prisma.cliente.findMany({
|
|
where: { empleado: true },
|
|
});
|
|
res.json(empleados);
|
|
} catch (error) {
|
|
console.error(error); // Log the error for debugging
|
|
res.status(500).json({ error: 'Error al obtener empleados.' });
|
|
}
|
|
});
|
|
|
|
// GET empleado by ID
|
|
router.get('/:id', async (req, res) => {
|
|
const { id } = req.params;
|
|
try {
|
|
const empleado = await prisma.cliente.findFirst({
|
|
where: {
|
|
id: parseInt(id),
|
|
empleado: true
|
|
},
|
|
});
|
|
if (empleado) {
|
|
res.json(empleado);
|
|
} else {
|
|
res.status(404).json({ error: 'Empleado no encontrado.' });
|
|
}
|
|
} catch (error) {
|
|
console.error(error); // Log the error for debugging
|
|
res.status(500).json({ error: 'Error al obtener empleado.' });
|
|
}
|
|
});
|
|
|
|
// POST create new empleado
|
|
router.post('/', async (req, res) => {
|
|
const { nombre, apellido, dni, telefono, direccion, email } = req.body;
|
|
try {
|
|
const nuevoEmpleado = await prisma.cliente.create({
|
|
data: {
|
|
nombre,
|
|
apellido,
|
|
dni,
|
|
telefono,
|
|
direccion,
|
|
email,
|
|
empleado: true, // Ensure empleado is set to true
|
|
},
|
|
});
|
|
res.status(201).json(nuevoEmpleado);
|
|
} catch (error) {
|
|
console.error(error); // Log the error for debugging
|
|
if (error.code === 'P2002' && error.meta?.target?.includes('dni')) {
|
|
return res.status(400).json({ error: 'Ya existe un cliente con este DNI.' });
|
|
}
|
|
if (error.code === 'P2002' && error.meta?.target?.includes('email')) {
|
|
return res.status(400).json({ error: 'Ya existe un cliente con este Email.' });
|
|
}
|
|
res.status(500).json({ error: 'Error al crear empleado.' });
|
|
}
|
|
});
|
|
|
|
// PUT update empleado by ID
|
|
router.put('/:id', async (req, res) => {
|
|
const { id } = req.params;
|
|
const { nombre, apellido, dni, telefono, direccion, email } = req.body;
|
|
try {
|
|
// First, check if the employee exists and is an employee
|
|
const existingEmpleado = await prisma.cliente.findFirst({
|
|
where: {
|
|
id: parseInt(id),
|
|
empleado: true,
|
|
},
|
|
});
|
|
|
|
if (!existingEmpleado) {
|
|
return res.status(404).json({ error: 'Empleado no encontrado.' });
|
|
}
|
|
|
|
const empleadoActualizado = await prisma.cliente.update({
|
|
where: { id: parseInt(id) },
|
|
data: {
|
|
nombre,
|
|
apellido,
|
|
dni,
|
|
telefono,
|
|
direccion,
|
|
email,
|
|
// empleado: true, // Keep it as an employee, or allow changing this? For now, keep as true.
|
|
},
|
|
});
|
|
res.json(empleadoActualizado);
|
|
} catch (error) {
|
|
console.error(error); // Log the error for debugging
|
|
if (error.code === 'P2002' && error.meta?.target?.includes('dni')) {
|
|
return res.status(400).json({ error: 'Ya existe un cliente con este DNI.' });
|
|
}
|
|
if (error.code === 'P2002' && error.meta?.target?.includes('email')) {
|
|
return res.status(400).json({ error: 'Ya existe un cliente con este Email.' });
|
|
}
|
|
if (error.code === 'P2025') { // Record to update not found
|
|
return res.status(404).json({ error: 'Empleado no encontrado para actualizar.' });
|
|
}
|
|
res.status(500).json({ error: 'Error al actualizar empleado.' });
|
|
}
|
|
});
|
|
|
|
// DELETE empleado by ID
|
|
router.delete('/:id', async (req, res) => {
|
|
const { id } = req.params;
|
|
try {
|
|
// First, check if the employee exists and is an employee
|
|
const existingEmpleado = await prisma.cliente.findFirst({
|
|
where: {
|
|
id: parseInt(id),
|
|
empleado: true,
|
|
},
|
|
});
|
|
|
|
if (!existingEmpleado) {
|
|
return res.status(404).json({ error: 'Empleado no encontrado para eliminar.' });
|
|
}
|
|
|
|
await prisma.cliente.delete({
|
|
where: { id: parseInt(id) },
|
|
});
|
|
res.status(204).send(); // No content
|
|
} catch (error) {
|
|
console.error(error); // Log the error for debugging
|
|
if (error.code === 'P2025') { // Record to delete not found
|
|
return res.status(404).json({ error: 'Empleado no encontrado para eliminar.' });
|
|
}
|
|
res.status(500).json({ error: 'Error al eliminar empleado.' });
|
|
}
|
|
});
|
|
|
|
export default router;
|