217 lines
6.2 KiB
JavaScript
217 lines
6.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 tareas
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const tareas = await prisma.tareaRealizada.findMany();
|
|
res.json(tareas);
|
|
} catch (e) {
|
|
console.error(e);
|
|
res.status(500).json({ message: 'Error al obtener tareas.' });
|
|
}
|
|
});
|
|
|
|
// 🔍 GET /api/tareas/search
|
|
router.get('/search', async (req, res) => {
|
|
const {
|
|
q,
|
|
empleado_id,
|
|
planilla_id,
|
|
estado,
|
|
titulo,
|
|
fecha_desde,
|
|
fecha_hasta,
|
|
limit = 0,
|
|
} = req.query;
|
|
|
|
try {
|
|
const where = {};
|
|
|
|
if (empleado_id) where.empleado_id = parseInt(empleado_id);
|
|
if (planilla_id) where.planilla_id = parseInt(planilla_id);
|
|
if (estado) where.estado = { contains: estado, mode: 'insensitive' };
|
|
if (titulo) where.titulo = { contains: titulo, mode: 'insensitive' };
|
|
|
|
if (fecha_desde || fecha_hasta) {
|
|
where.fecha = {};
|
|
if (fecha_desde) where.fecha.gte = new Date(fecha_desde);
|
|
if (fecha_hasta) where.fecha.lte = new Date(fecha_hasta);
|
|
}
|
|
|
|
if (q) {
|
|
const qi = parseInt(q);
|
|
where.OR = [
|
|
...(isNaN(qi) ? [] : [{ id: qi }, { empleado_id: qi }, { planilla_id: qi }]),
|
|
{ titulo: { contains: q, mode: 'insensitive' } },
|
|
{ estado: { contains: q, mode: 'insensitive' } },
|
|
{ observacion: { contains: q, mode: 'insensitive' } },
|
|
];
|
|
}
|
|
|
|
const take = parseInt(limit) || (Object.keys(where).length ? undefined : 100);
|
|
|
|
const tareas = await prisma.tareaRealizada.findMany({
|
|
where,
|
|
take,
|
|
orderBy: { id: 'desc' },
|
|
});
|
|
|
|
res.json(tareas);
|
|
} catch (e) {
|
|
console.error(e);
|
|
res.status(500).json({ message: 'Error al buscar tareas.' });
|
|
}
|
|
});
|
|
|
|
// GET tarea by ID
|
|
router.get('/:id(\\d+)', async (req, res) => {
|
|
const { id } = req.params;
|
|
try {
|
|
const tarea = await prisma.tareaRealizada.findUnique({
|
|
where: { id: parseInt(id) },
|
|
});
|
|
if (tarea) {
|
|
res.json(tarea);
|
|
} else {
|
|
res.status(404).json({ message: 'Tarea no encontrada.' });
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
res.status(500).json({ message: 'Error al obtener tarea.' });
|
|
}
|
|
});
|
|
|
|
// POST create new tarea
|
|
router.post('/', async (req, res) => {
|
|
const {
|
|
empleado_id,
|
|
planilla_id,
|
|
titulo,
|
|
precio,
|
|
estado,
|
|
observacion,
|
|
fecha,
|
|
tipo,
|
|
fecha_anulado,
|
|
creador_id,
|
|
anulador_id,
|
|
} = req.body;
|
|
|
|
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',
|
|
observacion,
|
|
fecha: new Date(fecha),
|
|
tipo: tipo || '',
|
|
fecha_anulado: fecha_anulado ? new Date(fecha_anulado) : null,
|
|
creador_id,
|
|
anulador_id,
|
|
},
|
|
});
|
|
res.status(201).json(nuevaTarea);
|
|
} catch (e) {
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
if (e.code === 'P2003') {
|
|
const constraint = e.meta?.constraint || '';
|
|
if (constraint.includes('empleado_id')) {
|
|
return res.status(400).json({ message: 'El empleado_id proporcionado no existe.' });
|
|
}
|
|
if (constraint.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.' });
|
|
}
|
|
});
|
|
|
|
// PUT update tarea by ID
|
|
router.put('/:id(\\d+)', async (req, res) => {
|
|
const { id } = req.params;
|
|
const {
|
|
empleado_id,
|
|
planilla_id,
|
|
titulo,
|
|
precio,
|
|
estado,
|
|
observacion,
|
|
fecha,
|
|
tipo,
|
|
fecha_anulado,
|
|
anulador_id,
|
|
} = req.body;
|
|
|
|
try {
|
|
const updateData = {};
|
|
if (empleado_id !== undefined) updateData.empleado_id = parseInt(empleado_id);
|
|
if (planilla_id !== undefined) updateData.planilla_id = planilla_id ? parseInt(planilla_id) : null;
|
|
if (titulo !== undefined) updateData.titulo = titulo;
|
|
if (precio !== undefined) updateData.precio = precio ? parseFloat(precio) : null;
|
|
if (estado !== undefined) updateData.estado = estado;
|
|
if (observacion !== undefined) updateData.observacion = observacion;
|
|
if (fecha !== undefined) updateData.fecha = new Date(fecha);
|
|
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;
|
|
|
|
const tareaActualizada = await prisma.tareaRealizada.update({
|
|
where: { id: parseInt(id) },
|
|
data: updateData,
|
|
});
|
|
res.json(tareaActualizada);
|
|
} catch (e) {
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
console.log(e);
|
|
if (e.code === 'P2025') {
|
|
return res.status(404).json({ message: 'Tarea no encontrada para actualizar.' });
|
|
}
|
|
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.' });
|
|
}
|
|
});
|
|
|
|
// DELETE tarea by ID
|
|
router.delete('/:id(\\d+)', async (req, res) => {
|
|
const { id } = req.params;
|
|
try {
|
|
await prisma.tareaRealizada.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: 'Tarea no encontrada para eliminar.' });
|
|
}
|
|
}
|
|
console.error(e);
|
|
res.status(500).json({ message: 'Error al eliminar tarea.' });
|
|
}
|
|
});
|
|
|
|
export default router;
|