Refactor: Centralize Prisma schema and restrict DB access

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.
This commit is contained in:
google-labs-jules[bot]
2025-05-30 23:40:00 +00:00
parent 4f1ec58a99
commit 242dc66983
26 changed files with 381 additions and 243 deletions

14
api/jsconfig.json Normal file
View File

@@ -0,0 +1,14 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"checkJs": false,
"resolveJsonModule": true,
"moduleResolution": "node",
"target": "esnext", // Or appropriate target for your Node.js version
"module": "esnext", // Since package.json has "type": "module"
// Paths are inherited from tsconfig.base.json
"baseUrl": "." // baseUrl is still needed if there are other local paths
},
"include": ["**/*.js"],
"exclude": ["node_modules"]
}

View File

@@ -7,13 +7,11 @@
"start": "node server.js"
},
"dependencies": {
"@prisma/client": "^6.8.2",
"@empresa/prisma-schema": "1.0.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"node-cron": "^4.0.5",
"pg": "^8.8.0"
},
"devDependencies": {
"prisma": "^6.8.2"
}
"devDependencies": {}
}

View File

@@ -1,84 +0,0 @@
-- CreateTable
CREATE TABLE "Cliente" (
"id" BIGSERIAL NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
"name" TEXT NOT NULL,
"cedula" BIGINT NOT NULL,
"ubicacion" TEXT NOT NULL DEFAULT '.',
"grupo_estudio" TEXT,
"empleado" BOOLEAN NOT NULL DEFAULT false,
"avatar_url" TEXT,
"telefono" TEXT,
"idciat" TEXT,
CONSTRAINT "Cliente_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Planilla" (
"id" BIGSERIAL NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
"fecha_desde" TIMESTAMP(3) NOT NULL,
"fecha_hasta" TIMESTAMP(3) NOT NULL,
"titulo" TEXT NOT NULL,
"total" DECIMAL(65,30),
"estado" TEXT NOT NULL DEFAULT 'pagado',
"fecha_anulado" TIMESTAMP(3),
"empleado_id" BIGINT NOT NULL,
"creador_id" UUID,
"anulador_id" UUID,
CONSTRAINT "Planilla_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "TareaRealizada" (
"id" BIGSERIAL NOT NULL,
"empleado_id" BIGINT NOT NULL,
"planilla_id" BIGINT,
"titulo" TEXT NOT NULL,
"precio" DOUBLE PRECISION,
"estado" TEXT NOT NULL DEFAULT 'pendiente',
"observacion" TEXT,
"fecha" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
"tipo" TEXT NOT NULL DEFAULT '',
"fecha_anulado" TIMESTAMP(3),
"creador_id" UUID NOT NULL,
"anulador_id" UUID,
CONSTRAINT "TareaRealizada_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Asistencia" (
"id" BIGSERIAL NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
"entrada" TIMESTAMP(3) DEFAULT (now() AT TIME ZONE 'utc'),
"salida" TIMESTAMP(3),
"historial" JSONB,
"observacion" TEXT,
"estado" TEXT DEFAULT 'pendiente',
"fecha_anulado" TIMESTAMP(3),
"empleado_id" BIGINT NOT NULL,
"creador_id" UUID NOT NULL,
"modificado_id" UUID,
"anulador_id" UUID,
CONSTRAINT "Asistencia_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Cliente_cedula_key" ON "Cliente"("cedula");
-- AddForeignKey
ALTER TABLE "Planilla" ADD CONSTRAINT "Planilla_empleado_id_fkey" FOREIGN KEY ("empleado_id") REFERENCES "Cliente"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "TareaRealizada" ADD CONSTRAINT "TareaRealizada_empleado_id_fkey" FOREIGN KEY ("empleado_id") REFERENCES "Cliente"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Asistencia" ADD CONSTRAINT "Asistencia_empleado_id_fkey" FOREIGN KEY ("empleado_id") REFERENCES "Cliente"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@@ -1,18 +0,0 @@
-- AlterTable
ALTER TABLE "Asistencia" ALTER COLUMN "created_at" SET DEFAULT (now() AT TIME ZONE 'utc'),
ALTER COLUMN "updated_at" SET DEFAULT (now() AT TIME ZONE 'utc'),
ALTER COLUMN "entrada" SET DEFAULT (now() AT TIME ZONE 'utc');
-- AlterTable
ALTER TABLE "Cliente" ALTER COLUMN "created_at" SET DEFAULT (now() AT TIME ZONE 'utc'),
ALTER COLUMN "updated_at" SET DEFAULT (now() AT TIME ZONE 'utc');
-- AlterTable
ALTER TABLE "Planilla" ALTER COLUMN "created_at" SET DEFAULT (now() AT TIME ZONE 'utc'),
ALTER COLUMN "updated_at" SET DEFAULT (now() AT TIME ZONE 'utc');
-- AlterTable
ALTER TABLE "TareaRealizada" ALTER COLUMN "created_at" SET DEFAULT (now() AT TIME ZONE 'utc');
-- AddForeignKey
ALTER TABLE "TareaRealizada" ADD CONSTRAINT "TareaRealizada_planilla_id_fkey" FOREIGN KEY ("planilla_id") REFERENCES "Planilla"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@@ -1,3 +0,0 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"

View File

@@ -1,88 +0,0 @@
generator client {
provider = "prisma-client-js"
output = "generated/client"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Cliente {
id BigInt @id @default(autoincrement())
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
updated_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')")) @updatedAt
name String
cedula BigInt @unique
ubicacion String @default(".")
grupo_estudio String?
empleado Boolean @default(false)
avatar_url String?
telefono String?
idciat String?
asistencias Asistencia[]
tareasRealizadas TareaRealizada[]
planillas Planilla[]
}
model Planilla {
id BigInt @id @default(autoincrement())
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
updated_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')")) @updatedAt
fecha_desde DateTime
fecha_hasta DateTime
titulo String
total Decimal?
estado String @default("pagado")
fecha_anulado DateTime?
tareasRealizadas TareaRealizada[]
empleado_id BigInt
empleado Cliente @relation(fields: [empleado_id], references: [id])
creador_id String? @default(uuid()) @db.Uuid
anulador_id String? @db.Uuid
}
model TareaRealizada {
id BigInt @id @default(autoincrement())
titulo String
precio Float?
estado String @default("pendiente")
observacion String?
fecha DateTime
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
tipo String @default("")
fecha_anulado DateTime?
planilla_id BigInt?
planilla Planilla? @relation(fields: [planilla_id], references: [id])
empleado_id BigInt
empleado Cliente @relation(fields: [empleado_id], references: [id])
creador_id String @default(uuid()) @db.Uuid
anulador_id String? @db.Uuid
}
model Asistencia {
id BigInt @id @default(autoincrement())
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
updated_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')")) @updatedAt
entrada DateTime? @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
salida DateTime?
historial Json?
observacion String?
estado String? @default("pendiente")
fecha_anulado DateTime?
empleado_id BigInt
empleado Cliente @relation(fields: [empleado_id], references: [id])
creador_id String @default(uuid()) @db.Uuid
modificado_id String? @db.Uuid
anulador_id String? @db.Uuid
}

View File

@@ -1,6 +1,5 @@
import express from 'express';
import { PrismaClient } from './prisma/generated/client/index.js';
import { Decimal } from '@prisma/client/runtime/library.js';
import { PrismaClient } from '@empresa/prisma-schema/client';
import cors from 'cors';
// Import new routers
@@ -16,7 +15,6 @@ import planillasRouter from './routes/planillas/planillas.js';
// 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();