77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
import express from 'express';
|
|
import { PrismaClient } from './prisma/generated/client/index.js';
|
|
import { Decimal } from '@prisma/client/runtime/library.js';
|
|
import cors from 'cors';
|
|
|
|
// Import new routers
|
|
import empleadosRouter from './routes/empleados/empleados.js';
|
|
import asistenciasRouter from './routes/asistencias/asistencias.js';
|
|
import tareasRouter from './routes/tareas/tareas.js';
|
|
import planillasRouter from './routes/planillas/planillas.js';
|
|
|
|
|
|
|
|
console.log( `Conectando a la base de datos: ${process.env.DATABASE_URL}`);
|
|
|
|
|
|
// Helper function for logging duration
|
|
const getDurationInMilliseconds = (start) => {
|
|
const NS_PER_SEC = 1e9;
|
|
const NS_TO_MS = 1e-6;
|
|
const diff = process.hrtime(start);
|
|
return (diff[0] * NS_PER_SEC + diff[1]) * NS_TO_MS;
|
|
};
|
|
|
|
// Resto del código
|
|
|
|
BigInt.prototype.toJSON = function () { return this.toString(); };
|
|
Decimal.prototype.toJSON = function () { return this.toString(); };
|
|
|
|
const prisma = new PrismaClient();
|
|
export const app = express();
|
|
app.use(express.json());
|
|
|
|
// Custom Logging Middleware
|
|
app.use((req, res, next) => {
|
|
const start = process.hrtime();
|
|
console.log(`[${new Date().toISOString()}] Incoming Request: ${req.method} ${req.originalUrl} from ${req.ip}`);
|
|
|
|
res.on('finish', () => {
|
|
const durationInMilliseconds = getDurationInMilliseconds(start);
|
|
console.log(`[${new Date().toISOString()}] Response Sent: ${res.statusCode} ${res.statusMessage}; ${req.method} ${req.originalUrl}; Duration: ${durationInMilliseconds.toLocaleString()} ms`);
|
|
});
|
|
|
|
next();
|
|
});
|
|
|
|
app.use(cors({
|
|
origin: ['http://localhost:5173', 'https://planilla.interno.com'],
|
|
credentials: true
|
|
}));
|
|
// Mount new routers
|
|
app.use('/api/empleados', empleadosRouter);
|
|
app.use('/api/asistencias', asistenciasRouter);
|
|
app.use('/api/tareas', tareasRouter);
|
|
app.use('/api/planillas', planillasRouter);
|
|
|
|
app.get('/api/test', (req, res) => res.json({ message: 'Hello World' }));
|
|
|
|
app.post('/api/clientes/random', async (_req, res) => {
|
|
try {
|
|
const cliente = await prisma.cliente.create({
|
|
data: {
|
|
name: 'Cliente ' + Math.floor(Math.random() * 10000),
|
|
cedula: BigInt(Math.floor(Math.random() * 1_000_000_000)),
|
|
ubicacion: 'Río Frío',
|
|
empleado: true,
|
|
},
|
|
});
|
|
res.json(cliente); // ← ahora sí serializa
|
|
} catch (err) {
|
|
console.error('❌ Error al crear cliente:', err);
|
|
res.status(500).json({ error: 'Error al crear cliente' });
|
|
}
|
|
});
|
|
|
|
app.listen(4000, () => console.log('API corriendo en :4000'));
|