Add search endpoints for all modules
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user