175 lines
4.8 KiB
JavaScript
175 lines
4.8 KiB
JavaScript
import express from 'express'
|
|
import { PrismaClient, Prisma } from '../../prisma/generated/client/index.js'
|
|
|
|
const router = express.Router()
|
|
const prisma = new PrismaClient()
|
|
|
|
const fixBigInt = (data) =>
|
|
JSON.parse(JSON.stringify(data, (_, v) => (typeof v === 'bigint' ? v.toString() : v)))
|
|
|
|
// ───── GET todos los empleados ─────
|
|
router.get('/', async (_req, res) => {
|
|
try {
|
|
const empleados = await prisma.cliente.findMany({ where: { empleado: true } })
|
|
res.json(fixBigInt(empleados))
|
|
} catch (e) {
|
|
console.error(e)
|
|
res.status(500).json({ message: 'Error al obtener empleados.' })
|
|
}
|
|
})
|
|
|
|
// 🔍 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)
|
|
try {
|
|
const empleado = await prisma.cliente.findFirst({ where: { id, empleado: true } })
|
|
if (!empleado) return res.status(404).json({ message: 'Empleado no encontrado.' })
|
|
res.json(fixBigInt(empleado))
|
|
} catch (e) {
|
|
console.error(e)
|
|
res.status(500).json({ message: 'Error al obtener empleado.' })
|
|
}
|
|
})
|
|
|
|
// ───── POST crear empleado ─────
|
|
router.post('/', async (req, res) => {
|
|
const {
|
|
name,
|
|
cedula,
|
|
telefono,
|
|
ubicacion = '.',
|
|
grupo_estudio,
|
|
avatar_url,
|
|
idciat,
|
|
} = req.body
|
|
|
|
try {
|
|
const nuevo = await prisma.cliente.create({
|
|
data: {
|
|
name,
|
|
cedula: BigInt(cedula),
|
|
telefono,
|
|
ubicacion,
|
|
grupo_estudio,
|
|
avatar_url,
|
|
idciat,
|
|
empleado: true,
|
|
},
|
|
})
|
|
res.status(201).json(fixBigInt(nuevo))
|
|
} catch (e) {
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
if (e.code === 'P2002') {
|
|
return res.status(400).json({ message: 'Ya existe un cliente con esa cédula.' })
|
|
}
|
|
}
|
|
|
|
console.error(e)
|
|
res.status(500).json({ message: 'Error al crear empleado.' })
|
|
}
|
|
})
|
|
|
|
// ───── PUT actualizar empleado ─────
|
|
router.put('/:id(\\d+)', async (req, res) => {
|
|
const id = BigInt(req.params.id)
|
|
const {
|
|
name,
|
|
cedula,
|
|
telefono,
|
|
ubicacion,
|
|
grupo_estudio,
|
|
avatar_url,
|
|
idciat,
|
|
} = req.body
|
|
|
|
try {
|
|
const existe = await prisma.cliente.findFirst({ where: { id, empleado: true } })
|
|
if (!existe) return res.status(404).json({ message: 'Empleado no encontrado.' })
|
|
|
|
const actualizado = await prisma.cliente.update({
|
|
where: { id },
|
|
data: {
|
|
name,
|
|
cedula: cedula !== undefined ? BigInt(cedula) : undefined,
|
|
telefono,
|
|
ubicacion,
|
|
grupo_estudio,
|
|
avatar_url,
|
|
idciat,
|
|
},
|
|
})
|
|
res.json(fixBigInt(actualizado))
|
|
} catch (e) {
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
if (e.code === 'P2002') {
|
|
return res.status(400).json({ message: 'Ya existe un cliente con esa cédula.' })
|
|
}
|
|
if (e.code === 'P2025') {
|
|
return res.status(404).json({ message: 'Empleado no encontrado para actualizar.' })
|
|
}
|
|
}
|
|
|
|
console.error(e)
|
|
res.status(500).json({ message: 'Error al actualizar empleado.' })
|
|
}
|
|
})
|
|
|
|
// ───── DELETE eliminar empleado ─────
|
|
router.delete('/:id(\\d+)', async (req, res) => {
|
|
const id = BigInt(req.params.id)
|
|
try {
|
|
const existe = await prisma.cliente.findFirst({ where: { id, empleado: true } })
|
|
if (!existe) return res.status(404).json({ message: 'Empleado no encontrado.' })
|
|
|
|
await prisma.cliente.delete({ where: { id } })
|
|
res.status(204).send()
|
|
} catch (e) {
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
if (e.code === 'P2025') {
|
|
return res.status(404).json({ message: 'Empleado no encontrado para eliminar.' })
|
|
}
|
|
}
|
|
|
|
console.error(e)
|
|
res.status(500).json({ message: 'Error al eliminar empleado.' })
|
|
}
|
|
})
|
|
|
|
export default router
|