Merge pull request #37 from josedario87/codex/add-search-api-for-all-modules
Add search endpoints to all modules
This commit is contained in:
@@ -14,6 +14,53 @@ router.get('/', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 🔍 GET /api/asistencias/search
|
||||||
|
router.get('/search', async (req, res) => {
|
||||||
|
const {
|
||||||
|
q,
|
||||||
|
empleado_id,
|
||||||
|
estado,
|
||||||
|
desde,
|
||||||
|
hasta,
|
||||||
|
limit = 0,
|
||||||
|
} = req.query;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const where = {};
|
||||||
|
|
||||||
|
if (empleado_id) where.empleado_id = BigInt(empleado_id);
|
||||||
|
if (estado) where.estado = { contains: estado, mode: 'insensitive' };
|
||||||
|
|
||||||
|
if (desde || hasta) {
|
||||||
|
where.entrada = {};
|
||||||
|
if (desde) where.entrada.gte = new Date(desde);
|
||||||
|
if (hasta) where.entrada.lte = new Date(hasta);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (q) {
|
||||||
|
const qi = parseInt(q);
|
||||||
|
where.OR = [
|
||||||
|
...(isNaN(qi) ? [] : [{ id: qi }, { empleado_id: qi }]),
|
||||||
|
{ estado: { contains: q, mode: 'insensitive' } },
|
||||||
|
{ observacion: { contains: q, mode: 'insensitive' } },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
const take = parseInt(limit) || (Object.keys(where).length ? undefined : 100);
|
||||||
|
|
||||||
|
const asistencias = await prisma.asistencia.findMany({
|
||||||
|
where,
|
||||||
|
take,
|
||||||
|
orderBy: { id: 'desc' },
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json(asistencias);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
res.status(500).json({ message: 'Error al buscar asistencias.' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// GET asistencia by ID
|
// GET asistencia by ID
|
||||||
router.get('/:id(\\d+)', async (req, res) => {
|
router.get('/:id(\\d+)', async (req, res) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|||||||
@@ -18,6 +18,42 @@ router.get('/', async (_req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 🔍 GET /api/empleados/search
|
||||||
|
router.get('/search', async (req, res) => {
|
||||||
|
const { q, name, cedula, telefono, limit = 0 } = req.query
|
||||||
|
|
||||||
|
try {
|
||||||
|
const where = { empleado: true }
|
||||||
|
|
||||||
|
if (name) where.name = { contains: name, mode: 'insensitive' }
|
||||||
|
if (cedula) where.cedula = BigInt(cedula)
|
||||||
|
if (telefono) where.telefono = { contains: telefono, mode: 'insensitive' }
|
||||||
|
|
||||||
|
if (q) {
|
||||||
|
const qi = parseInt(q)
|
||||||
|
where.OR = [
|
||||||
|
...(isNaN(qi) ? [] : [{ id: BigInt(qi) }, { cedula: BigInt(qi) }]),
|
||||||
|
{ name: { contains: q, mode: 'insensitive' } },
|
||||||
|
{ telefono: { contains: q, mode: 'insensitive' } },
|
||||||
|
{ ubicacion: { contains: q, mode: 'insensitive' } },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
const take = parseInt(limit) || (Object.keys(where).length > 1 ? undefined : 100)
|
||||||
|
|
||||||
|
const empleados = await prisma.cliente.findMany({
|
||||||
|
where,
|
||||||
|
take,
|
||||||
|
orderBy: { id: 'desc' },
|
||||||
|
})
|
||||||
|
|
||||||
|
res.json(fixBigInt(empleados))
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
res.status(500).json({ message: 'Error al buscar empleados.' })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// ───── GET empleado por ID ─────
|
// ───── GET empleado por ID ─────
|
||||||
router.get('/:id(\\d+)', async (req, res) => {
|
router.get('/:id(\\d+)', async (req, res) => {
|
||||||
const id = BigInt(req.params.id)
|
const id = BigInt(req.params.id)
|
||||||
|
|||||||
@@ -14,6 +14,58 @@ router.get('/', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 🔍 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
|
// GET tarea by ID
|
||||||
router.get('/:id(\\d+)', async (req, res) => {
|
router.get('/:id(\\d+)', async (req, res) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|||||||
Reference in New Issue
Block a user