Add search endpoints for all modules

This commit is contained in:
josedario87
2025-06-05 02:42:48 -06:00
parent 9cfe6b2817
commit d00b67442b
3 changed files with 135 additions and 0 deletions

View File

@@ -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 ─────
router.get('/:id(\\d+)', async (req, res) => {
const id = BigInt(req.params.id)