Mono-tenant: resolver id una vez al boot, eliminar lookups por turno

El sistema nunca fue realmente multi-tenant en la práctica. El esquema
DB conserva las columnas tenant_id (queda lista para escalar más adelante
sin migración), pero la app ahora resuelve el tenant una sola vez al
arranque y todas las capas leen de un único punto.

- src/modules/shared/tenant.js: nuevo módulo. setTenant() en boot,
  getTenantId() lo lee desde cualquier lado.
- index.js: ensureTenant() → setTenant({ id, key }). Sin cambios externos.
- pipeline.resolveTenantId(): pasa de hacer 1-2 queries a DB por turno
  a un return sincrónico del id cacheado. Mantiene firma async para no
  romper callers.
- intake handlers (sim.js, evolution.js): usan getTenantId() directo,
  sin parsing de tenant_key del chat_id ni lookup por canal.
- wooWebhooks: ya no requiere ?tenant_key=... en la query string.
  El webhook va al único tenant configurado.
- repo.js: eliminados getTenantByKey() y getTenantIdByChannel() (no más
  callers).

Plumbing del parámetro tenantId en signatures de handlers/repos/machine
queda intacto — bajar eso es ruido de alto riesgo y no aporta hoy.
188 tests pasando.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lucas Tettamanti
2026-05-01 21:00:22 -03:00
parent 17cea4aa9e
commit 6b7889ef4e
9 changed files with 56 additions and 76 deletions

View File

@@ -1,6 +1,6 @@
import crypto from "crypto";
import { resolveTenantId } from "../../2-identity/services/pipeline.js";
import { processMessage } from "../../2-identity/services/pipeline.js";
import { getTenantId } from "../../shared/tenant.js";
export async function handleSimSend(body) {
const { chat_id, from_phone, text } = body || {};
@@ -10,11 +10,7 @@ export async function handleSimSend(body) {
const provider = "sim";
const message_id = crypto.randomUUID();
const tenantId = await resolveTenantId({
chat_id,
tenant_key: body?.tenant_key,
to_phone: body?.to_phone,
});
const tenantId = getTenantId();
const result = await processMessage({
tenantId,
@@ -27,4 +23,3 @@ export async function handleSimSend(body) {
return { status: 200, payload: { ok: true, run_id: result.run_id, reply: result.reply } };
}