493 lines
16 KiB
Plaintext
493 lines
16 KiB
Plaintext
generator client {
|
|
provider = "prisma-client"
|
|
output = "../src/generated/prisma"
|
|
}
|
|
|
|
datasource db {
|
|
provider = "postgresql"
|
|
}
|
|
|
|
model User {
|
|
id String @id @default(cuid())
|
|
email String @unique
|
|
password String
|
|
name String
|
|
// "admin" | "asesor" | "cliente". Un usuario con role="cliente" DEBE tener
|
|
// clienteId: su sesion da acceso unicamente al panel /cliente de ese Cliente.
|
|
role String @default("asesor")
|
|
clienteId String? @unique
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cotizaciones Cotizacion[] @relation("AsesorCotizaciones")
|
|
cliente Cliente? @relation("ClienteUsuario", fields: [clienteId], references: [id])
|
|
suscripcionesPush SuscripcionPush[]
|
|
}
|
|
|
|
model Cliente {
|
|
id String @id @default(cuid())
|
|
nombre String
|
|
empresa String?
|
|
// Cargo de la persona de contacto (ej. "Director"). Solo informativo.
|
|
puesto String?
|
|
email String?
|
|
telefono String?
|
|
rfc String?
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cotizaciones Cotizacion[]
|
|
// Cuenta de acceso al panel de cliente (role="cliente"). Una por cliente.
|
|
usuario User? @relation("ClienteUsuario")
|
|
programasPago ProgramaPago[]
|
|
|
|
@@index([nombre, empresa])
|
|
}
|
|
|
|
model Cotizacion {
|
|
id String @id @default(cuid())
|
|
numero String @unique
|
|
fecha DateTime @default(now())
|
|
vigencia DateTime
|
|
moneda String @default("MXN")
|
|
tipoCambio String @default("NA")
|
|
proyecto String @default("MKT Digital")
|
|
esquemaPago String @default("Pago Unico/Mensual")
|
|
estado String @default("borrador")
|
|
incluirBonos Boolean @default(false)
|
|
incluirFinanciamiento Boolean @default(false)
|
|
incluirIva Boolean @default(true)
|
|
esDoble Boolean @default(false)
|
|
opcionesMetadata Json?
|
|
// Texto que SI ve el cliente: se imprime en el PDF y en el Excel.
|
|
observaciones String?
|
|
// Contexto de discovery, notas del asesor, riesgos. NO sale de la app: ningun
|
|
// exportador declara este campo en su interfaz de datos (CotizacionPDFData /
|
|
// ExcelData) y la herramienta MCP obtener_cotizacion usa lista blanca de
|
|
// columnas. La garantia es estructural, no depende de recordar filtrarlo.
|
|
observacionesInternas String?
|
|
clienteId String
|
|
asesorId String
|
|
aprobadaPorId String?
|
|
aprobadaAt DateTime?
|
|
aprobacionMetodo String?
|
|
enviadaAt DateTime?
|
|
enviadaPor String?
|
|
enviadaCanal String?
|
|
enviadaDestino String?
|
|
aceptadaAt DateTime?
|
|
rechazadaAt DateTime?
|
|
vencidaAt DateTime?
|
|
creadaPorAgente String?
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cliente Cliente @relation(fields: [clienteId], references: [id])
|
|
asesor User @relation("AsesorCotizaciones", fields: [asesorId], references: [id])
|
|
servicios ServicioCotizado[]
|
|
planBucefalo PlanBucefaloCotizacion?
|
|
registrosHoras RegistroHoras[]
|
|
propuestasIA PropuestaIA[]
|
|
programasPago ProgramaPago[]
|
|
approvalTokens ApprovalToken[]
|
|
|
|
@@index([clienteId])
|
|
@@index([asesorId])
|
|
@@index([estado])
|
|
@@index([createdAt])
|
|
}
|
|
|
|
model Categoria {
|
|
id String @id @default(cuid())
|
|
nombre String @unique
|
|
descripcion String?
|
|
color String @default("#6b7280")
|
|
activo Boolean @default(true)
|
|
orden Int @default(0)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
servicios ServicioCatalogo[]
|
|
}
|
|
|
|
model Paquete {
|
|
id String @id @default(cuid())
|
|
nombre String
|
|
descripcion String?
|
|
activo Boolean @default(true)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
fases FasePaquete[]
|
|
}
|
|
|
|
model FasePaquete {
|
|
id String @id @default(cuid())
|
|
paqueteId String
|
|
nombre String
|
|
orden Int @default(0)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
paquete Paquete @relation(fields: [paqueteId], references: [id], onDelete: Cascade)
|
|
servicios ServicioPaquete[]
|
|
|
|
@@index([paqueteId])
|
|
}
|
|
|
|
model ServicioCatalogo {
|
|
id String @id @default(cuid())
|
|
nombre String
|
|
descripcion String?
|
|
fase Int @default(1)
|
|
tipoPago String @default("unico")
|
|
precioBase Float
|
|
tiempoEntrega String @default("4 - 10 dias")
|
|
entregablesDefault Json @default("[]")
|
|
categoriaId String?
|
|
variante String?
|
|
nivel String?
|
|
activo Boolean @default(true)
|
|
orden Int @default(0)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
servicios ServicioCotizado[]
|
|
categoriaRel Categoria? @relation(fields: [categoriaId], references: [id])
|
|
paquetes ServicioPaquete[]
|
|
|
|
@@index([categoriaId])
|
|
@@index([activo, fase, orden])
|
|
}
|
|
|
|
model ServicioPaquete {
|
|
id String @id @default(cuid())
|
|
servicioCatalogoId String
|
|
fasePaqueteId String
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
servicio ServicioCatalogo @relation(fields: [servicioCatalogoId], references: [id], onDelete: Cascade)
|
|
fasePaquete FasePaquete @relation(fields: [fasePaqueteId], references: [id], onDelete: Cascade)
|
|
|
|
@@unique([servicioCatalogoId, fasePaqueteId])
|
|
@@index([fasePaqueteId])
|
|
}
|
|
|
|
model ServicioCotizado {
|
|
id String @id @default(cuid())
|
|
cotizacionId String
|
|
servicioCatalogoId String?
|
|
nombre String?
|
|
esPersonalizado Boolean @default(false)
|
|
horas Float?
|
|
tarifaHora Float?
|
|
modeloCobro String @default("fijo")
|
|
montoMinimo Float?
|
|
horasIncluidas Float?
|
|
opcion String?
|
|
fase Int
|
|
tipoPago String
|
|
precio Float
|
|
tiempoEntrega String
|
|
entregables Json @default("[]")
|
|
beneficios Json @default("[]")
|
|
notas String?
|
|
seleccionado Boolean @default(true)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cotizacion Cotizacion @relation(fields: [cotizacionId], references: [id], onDelete: Cascade)
|
|
servicioCatalogo ServicioCatalogo? @relation(fields: [servicioCatalogoId], references: [id])
|
|
|
|
@@index([cotizacionId])
|
|
@@index([servicioCatalogoId])
|
|
}
|
|
|
|
model PlanBucefaloCotizacion {
|
|
id String @id @default(cuid())
|
|
cotizacionId String @unique
|
|
nivel String @default("basico")
|
|
precio Float
|
|
seleccionado Boolean @default(true)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cotizacion Cotizacion @relation(fields: [cotizacionId], references: [id], onDelete: Cascade)
|
|
}
|
|
|
|
// Registro de horas trabajadas contra una cotizacion aprobada con cobro por tiempo
|
|
// (modeloCobro horas/retainer/demanda). Se usa para emitir notas de pago al cliente:
|
|
// cada entrada es un dia con un rango horario, una descripcion breve y una tarifa
|
|
// (snapshot). `horas` se calcula del rango (calcularHorasRango) y se persiste.
|
|
model RegistroHoras {
|
|
id String @id @default(cuid())
|
|
cotizacionId String
|
|
fecha DateTime
|
|
horaInicio String
|
|
horaFin String
|
|
horas Float
|
|
tarifaHora Float @default(0)
|
|
descripcion String
|
|
// Estado de cobro de la partida de horas: "por_pagar" (default) o "pagada".
|
|
// Permite separar lo pendiente (nota por pagar al cliente) de lo ya cobrado
|
|
// (recibo/historial) sin que los totales se mezclen. fechaPago = cuando se marco pagada.
|
|
estadoPago String @default("por_pagar")
|
|
fechaPago DateTime?
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cotizacion Cotizacion @relation(fields: [cotizacionId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([cotizacionId])
|
|
@@index([fecha])
|
|
@@index([estadoPago])
|
|
}
|
|
|
|
// Documento consultivo generado por IA a partir del contexto humano de la cotizacion.
|
|
// Se guarda por separado lo que produjo el modelo y lo que edito el asesor, para poder
|
|
// mostrar que cambio y para no perder el original al editar.
|
|
model PropuestaIA {
|
|
id String @id @default(cuid())
|
|
cotizacionId String
|
|
estado String @default("borrador") // borrador | aprobada
|
|
contenidoIA Json
|
|
contenidoEditado Json?
|
|
// Transcripcion sanitizada usada al generar. Sin esto, el PATCH revalida con
|
|
// fuentes incompletas y R3 no puede comprobar que la cita destacada sea literal.
|
|
transcripcion String?
|
|
// Traza de la generacion: modelo, intentos por paso, tokens y aciertos de cache.
|
|
traza Json?
|
|
// Hallazgos de validacion y de contenido que el asesor debe revisar antes de enviar.
|
|
avisos Json @default("[]")
|
|
modelo String
|
|
aprobadaPor String?
|
|
aprobadaAt DateTime?
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cotizacion Cotizacion @relation(fields: [cotizacionId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([cotizacionId])
|
|
}
|
|
|
|
model Configuracion {
|
|
id String @id @default(cuid())
|
|
clave String @unique
|
|
valor String
|
|
updatedAt DateTime @updatedAt
|
|
}
|
|
|
|
model Bono {
|
|
id String @id @default(cuid())
|
|
numero Int
|
|
titulo String
|
|
descripcion String
|
|
activo Boolean @default(true)
|
|
}
|
|
|
|
model FinanciamientoPlan {
|
|
id String @id @default(cuid())
|
|
meses Int @unique
|
|
tasa Float
|
|
comision Float
|
|
montoMinimo Float
|
|
iva Float @default(0.16)
|
|
}
|
|
|
|
// Programa de cobros recurrentes configurado por el administrador.
|
|
// Ejemplo real: Instituto Santiago Tapia — cada sabado se genera un cobro de $750
|
|
// por la administracion web. La generacion es idempotente: un PagoRecurrente por
|
|
// (programaId, fecha), calculado desde fechaInicio con la cadencia.
|
|
model ProgramaPago {
|
|
id String @id @default(cuid())
|
|
clienteId String
|
|
// Vinculo opcional con la cotizacion que origina el servicio recurrente.
|
|
cotizacionId String?
|
|
concepto String
|
|
monto Float
|
|
moneda String @default("MXN")
|
|
// "semanal" | "quincenal" | "mensual"
|
|
frecuencia String @default("semanal")
|
|
// Dia de la semana 0=domingo..6=sabado (requerido si frecuencia=semanal).
|
|
diaSemana Int?
|
|
// Dia del mes 1..31 (requerido si frecuencia=mensual; quincenal usa dia 1 y 15).
|
|
diaMes Int?
|
|
fechaInicio DateTime
|
|
fechaFin DateTime?
|
|
activo Boolean @default(true)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
cliente Cliente @relation(fields: [clienteId], references: [id], onDelete: Cascade)
|
|
cotizacion Cotizacion? @relation(fields: [cotizacionId], references: [id], onDelete: SetNull)
|
|
pagos PagoRecurrente[]
|
|
|
|
@@index([clienteId])
|
|
@@index([activo])
|
|
}
|
|
|
|
// Un cobro generado para un periodo concreto (ej. sabado 2026-07-04).
|
|
// estados: pendiente (sin comprobante) | en_revision (comprobante subido,
|
|
// esperando admin) | verificado (admin aprobo, o registro el pago a mano) |
|
|
// rechazado (admin rechazo y borro el archivo; el cliente puede volver a subir) |
|
|
// anulado (el admin lo dejo sin cargo; no cuenta como adeudo).
|
|
model PagoRecurrente {
|
|
id String @id @default(cuid())
|
|
programaId String
|
|
// Fecha del periodo (medianoche America/Mexico_City al momento de generar).
|
|
fecha DateTime
|
|
monto Float
|
|
estado String @default("pendiente")
|
|
// Fecha en que el admin valido el comprobante (estado=verificado).
|
|
verificadoAt DateTime?
|
|
rechazadoAt DateTime?
|
|
rechazoMotivo String?
|
|
// Ultimo recordatorio de pago enviado al cliente mientras el cobro sigue
|
|
// por pagar (pendiente/rechazado). El scheduler lo usa para no repetir el
|
|
// aviso antes de COBROS_RECORDATORIO_DIAS; el envio manual lo ignora.
|
|
ultimoRecordatorioAt DateTime?
|
|
recordatoriosEnviados Int @default(0)
|
|
// Nota interna del admin (no la ve el cliente): forma de pago o abonos al
|
|
// registrar un pago manual, o el motivo al anular un cobro.
|
|
nota String?
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
programa ProgramaPago @relation(fields: [programaId], references: [id], onDelete: Cascade)
|
|
comprobante ComprobantePago?
|
|
|
|
@@unique([programaId, fecha])
|
|
@@index([estado])
|
|
@@index([fecha])
|
|
}
|
|
|
|
// Comprobante de pago subido por el cliente (imagen o PDF). El archivo vive en
|
|
// UPLOAD_DIR/comprobantes/ y se sirve solo por un route handler autenticado
|
|
// (nunca desde /public). Al rechazar, el admin borra el archivo del servidor
|
|
// y se elimina este registro; el cliente vuelve a "pendiente".
|
|
model ComprobantePago {
|
|
id String @id @default(cuid())
|
|
pagoId String @unique
|
|
archivoRuta String // relativa a UPLOAD_DIR, ej. "comprobantes/<pagoId>/<uuid>.webp"
|
|
archivoNombre String // nombre original que vio el cliente
|
|
mime String // image/webp | application/pdf
|
|
tamano Int // bytes finales en disco
|
|
subidoAt DateTime @default(now())
|
|
|
|
pago PagoRecurrente @relation(fields: [pagoId], references: [id], onDelete: Cascade)
|
|
}
|
|
|
|
// Suscripcion Web Push del navegador del cliente (endpoint VAPID + claves p256dh/auth).
|
|
model SuscripcionPush {
|
|
id String @id @default(cuid())
|
|
userId String
|
|
endpoint String @unique
|
|
p256dh String
|
|
auth String
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
usuario User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([userId])
|
|
}
|
|
|
|
// ============================================================================
|
|
// API para Agentes, Auditoria, Aprobacion, Idempotencia y Webhooks
|
|
// ============================================================================
|
|
|
|
model AgentApiKey {
|
|
id String @id @default(cuid())
|
|
nombre String @unique
|
|
prefijo String
|
|
keyHash String @unique
|
|
scopes String[]
|
|
rateLimitPerMin Int @default(60)
|
|
activo Boolean @default(true)
|
|
revokedAt DateTime?
|
|
lastUsedAt DateTime?
|
|
createdById String?
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
}
|
|
|
|
model AuditLog {
|
|
id String @id @default(cuid())
|
|
actorTipo String
|
|
actorId String?
|
|
actorNombre String
|
|
accion String
|
|
recursoTipo String
|
|
recursoId String?
|
|
ip String?
|
|
requestId String?
|
|
exito Boolean @default(true)
|
|
detalle Json?
|
|
createdAt DateTime @default(now())
|
|
|
|
@@index([createdAt])
|
|
@@index([accion])
|
|
@@index([recursoTipo, recursoId])
|
|
@@index([actorNombre])
|
|
}
|
|
|
|
model ApprovalToken {
|
|
id String @id @default(cuid())
|
|
cotizacionId String
|
|
tokenHash String @unique
|
|
expiraAt DateTime
|
|
usadoAt DateTime?
|
|
usadoIp String?
|
|
createdAt DateTime @default(now())
|
|
|
|
cotizacion Cotizacion @relation(fields: [cotizacionId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([cotizacionId])
|
|
}
|
|
|
|
model IdempotencyKey {
|
|
id String @id @default(cuid())
|
|
key String @unique
|
|
method String
|
|
path String
|
|
requestHash String
|
|
statusCode Int
|
|
responseBody Json
|
|
expiraAt DateTime
|
|
createdAt DateTime @default(now())
|
|
|
|
@@index([expiraAt])
|
|
}
|
|
|
|
model WebhookEndpoint {
|
|
id String @id @default(cuid())
|
|
nombre String @unique
|
|
url String
|
|
secret String
|
|
eventos String[]
|
|
activo Boolean @default(true)
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
|
|
entregas WebhookDelivery[]
|
|
}
|
|
|
|
model WebhookDelivery {
|
|
id String @id @default(cuid())
|
|
endpointId String?
|
|
url String
|
|
evento String
|
|
payload Json
|
|
statusCode Int?
|
|
exito Boolean @default(false)
|
|
intentos Int @default(1)
|
|
error String?
|
|
createdAt DateTime @default(now())
|
|
|
|
endpoint WebhookEndpoint? @relation(fields: [endpointId], references: [id], onDelete: SetNull)
|
|
|
|
@@index([evento])
|
|
@@index([createdAt])
|
|
}
|