139 lines
3.8 KiB
JavaScript
139 lines
3.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 empleado por ID ─────
|
|
router.get('/:id', 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' && e.meta?.target?.includes('cedula')) {
|
|
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', 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' && e.meta?.target?.includes('cedula')) {
|
|
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', 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
|