This commit refactors the project to use a shared Prisma schema and restricts direct database access to the API service. Key changes: - Created a new shared package `core/prisma` containing the Prisma schema, generated client, and types. - Configured the monorepo to use NPM workspaces, including `core/prisma` and all services. - Updated all services (`api`, `ui`, `mcp`, `agent`, and the background processing service) to depend on `@empresa/prisma-schema`. - The API service now imports `PrismaClient` from `@empresa/prisma-schema/client`. - Other services import only types from `@empresa/prisma-schema`. - Removed redundant Prisma configurations from `api` and the background processing service. - Updated the background processing service's `sync-empleados.js` to fetch data via an API call instead of direct database access. - Updated TypeScript configurations (`tsconfig.base.json` and service-specific ones) to support the new structure and path aliases. - Updated `README.md` to reflect the new architecture and added convenience scripts for Prisma operations. This change promotes a single source of truth for data models, reduces code duplication, and improves the overall architecture by centralizing database operations within the API service.
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
import express from 'express';
|
|
import { PrismaClient } from '@empresa/prisma-schema/client';
|
|
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';
|
|
|
|
|
|
|
|
|
|
|
|
// Resto del código
|
|
|
|
BigInt.prototype.toJSON = function () { return this.toString(); };
|
|
|
|
const prisma = new PrismaClient();
|
|
export const app = express();
|
|
app.use(express.json());
|
|
|
|
|
|
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'));
|