separated in modules

This commit is contained in:
Lucas Tettamanti
2026-01-15 22:45:33 -03:00
parent eedd16afdb
commit ea62385e3d
41 changed files with 1116 additions and 2918 deletions

View File

@@ -0,0 +1,51 @@
import crypto from "crypto";
import { parseEvolutionWebhook } from "../services/evolutionParser.js";
import { resolveTenantId, processMessage } from "../../2-identity/services/pipeline.js";
import { debug as dbg } from "../../shared/debug.js";
export async function handleEvolutionWebhook(body) {
const t0 = Date.now();
const parsed = parseEvolutionWebhook(body);
if (!parsed.ok) {
return { status: 200, payload: { ok: true, ignored: parsed.reason } };
}
if (dbg.perf || dbg.evolution) {
console.log("[perf] evolution.webhook.start", {
tenant_key: parsed.tenant_key || null,
chat_id: parsed.chat_id,
message_id: parsed.message_id || null,
ts: parsed.ts || null,
});
}
const tenantId = await resolveTenantId({
chat_id: parsed.chat_id,
tenant_key: parsed.tenant_key,
to_phone: null,
});
const pm = await processMessage({
tenantId,
chat_id: parsed.chat_id,
from: parsed.chat_id.replace("@s.whatsapp.net", ""),
displayName: parsed.from_name || null,
text: parsed.text,
provider: "evolution",
message_id: parsed.message_id || crypto.randomUUID(),
meta: { pushName: parsed.from_name, ts: parsed.ts, instance: parsed.tenant_key, source: parsed.source },
});
if (dbg.perf || dbg.evolution) {
console.log("[perf] evolution.webhook.end", {
tenantId,
chat_id: parsed.chat_id,
message_id: parsed.message_id || null,
run_id: pm?.run_id || null,
webhook_ms: Date.now() - t0,
});
}
return { status: 200, payload: { ok: true } };
}