62 lines
2.5 KiB
Python
62 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Fix post-Fase 2/4: el code `Obtener datos completos de Contacto objetivo -
|
|
SUCURSAL` referencia a `Obtener Contacto Cuenta objetivo - SUCURSAL` directamente,
|
|
pero en la rama de match por CF ese nodo no se ejecuta (el contacto viene de
|
|
`Match directo por id_contacto_sucursal`). Hacemos el code defensivo: probamos
|
|
ambos nodos y usamos el que tenga datos.
|
|
|
|
Idempotente.
|
|
"""
|
|
import argparse, os, sys
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, ROOT); sys.path.insert(0, os.path.join(ROOT, "scripts"))
|
|
from n8n_workflow_lib import load_credentials, N8NClient # noqa: E402
|
|
|
|
WID = "4UMRwxJdHFfOGHBp"
|
|
TARGET = "Obtener datos completos de Contacto objetivo - SUCURSAL"
|
|
MARKER = "// FIX FASE 2: input from match by CF or cascade"
|
|
|
|
NEW_PREAMBLE = (
|
|
MARKER + "\n"
|
|
"var contactData;\n"
|
|
"try {\n"
|
|
" contactData = $('Match directo por id_contacto_sucursal').first().json.contact;\n"
|
|
"} catch (e) { contactData = null; }\n"
|
|
"if (!contactData || !contactData.id) {\n"
|
|
" contactData = $('Obtener Contacto Cuenta objetivo - SUCURSAL').first().json.contact;\n"
|
|
"}\n"
|
|
)
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("--apply", action="store_true")
|
|
args = p.parse_args()
|
|
if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8")
|
|
c = N8NClient(*load_credentials())
|
|
wf, b = c.backup_workflow(WID, label="fase2_fix_code_ref")
|
|
print(f"backup: {b}")
|
|
prev = wf.get("versionId")
|
|
n = c.find_node(wf, TARGET)
|
|
if not n: raise SystemExit(f"nodo {TARGET!r} no encontrado")
|
|
code = n["parameters"].get("jsCode") or ""
|
|
if MARKER in code:
|
|
print("ya parchado (skip)"); return
|
|
old_line = "const contactData = $('Obtener Contacto Cuenta objetivo - SUCURSAL').first().json.contact;"
|
|
if old_line not in code:
|
|
print("línea esperada no encontrada; revisa manualmente"); print(code[:400]); return
|
|
new_code = code.replace(old_line, NEW_PREAMBLE)
|
|
n["parameters"]["jsCode"] = new_code
|
|
if not args.apply:
|
|
res = c.put_workflow(WID, wf, dry_run=True); print(f"DRY-RUN: {res['path']}"); return
|
|
c.put_workflow(WID, wf, dry_run=False)
|
|
wf2 = c.verify_post(WID, expected_node_names=[TARGET], prev_version_id=prev)
|
|
print(f"versionId nuevo: {wf2.get('versionId')} active: {wf2.get('active')}")
|
|
if not wf2.get("active"): c.activate(WID); print("activado")
|
|
print("OK")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|