#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Fase 4 — Renombres cosméticos del workflow Marca→Sucursal V2. Los nombres con sufijo "MARCA" o "SUCURSAL" en ese workflow son inconsistentes porque el JSON fue copy-pasted del workflow inverso. Esta fase renombra usando `rename_node` que actualiza también todas las referencias `$('OLD')` en parameters y `connections`. Idempotente: si ya están renombrados, skip por assert_idempotent inverso. """ import argparse import copy import os import 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, AlreadyExistsError # noqa: E402 WID = "4UMRwxJdHFfOGHBp" RENAMES = [ # (nombre actual, nombre nuevo) ("Crear Contacto - Cuenta Objetivo - MARCA", "Crear Contacto - Cuenta Objetivo - SUCURSAL"), ("Conseguir Custom Cuenta objetivo - MARCA1", "Conseguir Custom Cuenta objetivo - SUCURSAL (CREATE)"), ("Obtener Contacto Cuenta Origen - SUCURSAL", "Obtener Contacto Cuenta Origen - MARCA"), ("Conseguir Custom Cuenta Origen- SUCURSAL", "Conseguir Custom Cuenta Origen - MARCA"), ("Obtener datos completos de Contacto origen - SUCURSAL", "Obtener datos completos de Contacto origen - MARCA"), ("Obtener el body para crear Contacto - MARCA", "Obtener el body para crear Contacto - SUCURSAL"), ] def main(): parser = argparse.ArgumentParser() parser.add_argument("--apply", action="store_true") parser.add_argument("--activate", action="store_true") args = parser.parse_args() if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8") client = N8NClient(*load_credentials()) wf, backup = client.backup_workflow(WID, label="fase4_pre") print(f"backup: {backup}") prev = wf.get("versionId") print(f"versionId pre: {prev} active: {wf.get('active')}") wfm = copy.deepcopy(wf) applied = 0 expected_post = [] for old, new in RENAMES: # Si el nodo viejo no existe, asumimos que ya está renombrado (idempotente) if client.find_node(wfm, old) is None: if client.find_node(wfm, new) is not None: print(f" [skip] {old!r} → {new!r} (ya renombrado)") expected_post.append(new) continue else: raise SystemExit(f"FAIL: {old!r} no existe y {new!r} tampoco.") try: client.rename_node(wfm, old, new) print(f" [rename] {old!r} → {new!r}") applied += 1 expected_post.append(new) except AlreadyExistsError as e: raise SystemExit(f"FAIL: {e}") if applied == 0: print("Nada que renombrar.") return if not args.apply: res = client.put_workflow(WID, wfm, dry_run=True) print(f"DRY-RUN. {res['path']}") return client.put_workflow(WID, wfm, dry_run=False) wf2 = client.verify_post(WID, expected_node_names=expected_post, prev_version_id=prev) print(f"versionId nuevo: {wf2.get('versionId')} active: {wf2.get('active')}") if args.activate or not wf2.get("active"): client.activate(WID) print("activado") print(f"Fase 4 aplicada: {applied} renombres.") if __name__ == "__main__": main()