{
  "id": "kg1IhkAbGVnzrYOv",
  "createdAt": "2023-07-13T03:35:15.046Z",
  "updatedAt": "2023-07-13T06:25:59.994Z",
  "name": "AlwaysOn[CL]",
  "active": false,
  "nodes": [
    {
      "id": "2ffae247-6ca9-41e1-aec7-c1e8b6f8e2bf",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -200,
        880
      ],
      "parameters": {
        "path": "edb39e00-59a9-47f1-8973-ee6bf3bcbc21",
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "72dfad5e-a89b-4adc-bea6-eb05ffb14bb1",
      "name": "Search Hubspot Company",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        400,
        600
      ],
      "parameters": {
        "url": "=https://api.hubapi.com/crm/v3/objects/companies/search/",
        "method": "POST",
        "options": {},
        "jsonBody": "={\"filterGroups\":[{\"filters\":[{\"value\":\"{{ $json.document_number }}\",\"propertyName\":\"rut_empresa\",\"operator\":\"EQ\"}]}],\"sorts\":[\"string\"],\"properties\":[\"rut\"],\"limit\":1,\"after\":0}  ",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotAppToken"
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "e40e8781-c526-4d12-9bbe-639c787481cc",
      "name": "Parsear",
      "type": "n8n-nodes-base.set",
      "position": [
        -40,
        600
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "documento_tributario",
              "value": "={{ $json.query.rut_empresa }}"
            },
            {
              "name": "hs_object_id",
              "value": "={{ $json.query.hs_object_id }}"
            }
          ]
        },
        "options": {},
        "keepOnlySet": true
      },
      "typeVersion": 2
    },
    {
      "id": "c7721f4a-40f9-43f4-8891-5bc25a4e8474",
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "position": [
        580,
        900
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.total }}",
              "operation": "equal"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "37555916-db86-4541-9859-99243c9c1b41",
      "name": "HubSpot",
      "type": "n8n-nodes-base.hubspot",
      "position": [
        780,
        640
      ],
      "parameters": {
        "name": "={{ $node.Code.json.document_number }}",
        "resource": "company",
        "authentication": "appToken",
        "additionalFields": {
          "customPropertiesUi": {
            "customPropertiesValues": [
              {
                "value": "={{ $node.Code.json.document_number }}",
                "property": "documento_tributario"
              },
              {
                "value": "Always On",
                "property": "origen_de_cliente"
              },
              {
                "value": "[CL] Always On",
                "property": "registro_always_on"
              },
              {
                "value": "Disponible",
                "property": "estado_empresa"
              }
            ]
          }
        }
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "983f3252-d185-459a-a080-863b180109af",
      "name": "SetCompanyId",
      "type": "n8n-nodes-base.set",
      "position": [
        1020,
        640
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "CompanyId",
              "value": "={{ $json.companyId }}"
            }
          ]
        },
        "options": {},
        "keepOnlySet": true
      },
      "typeVersion": 2
    },
    {
      "id": "70e00516-eebe-463a-bcfc-9f5135092bda",
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "position": [
        820,
        1040
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "CompanyId",
              "value": "={{ $json.results[0].id }}"
            }
          ]
        },
        "options": {},
        "keepOnlySet": true
      },
      "typeVersion": 2
    },
    {
      "id": "7d3dcad0-a8e8-4f07-8ab9-fac44e625748",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        140,
        600
      ],
      "parameters": {
        "jsCode": "const documentoTributario = $input.first().json.documento_tributario;\nconst hsObjectId = $input.first().json.hs_object_id;\n\nconst rut = documentoTributario.replace(/\\./g, '').replace(/-/g, '');\n\nconsole.log(rut);\nreturn {\n  \"document_number\": rut,\n  \"hs_object_id\": hsObjectId\n};\n"
      },
      "typeVersion": 1
    },
    {
      "id": "dbd60996-34fe-4571-9bfd-ae349be98681",
      "name": "Link contact to company",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1260,
        640
      ],
      "parameters": {
        "url": "=https://api.hubapi.com/crm/v3/objects/contact/{{ $node.Parsear.json.hs_object_id }}/associations/company/{{ $node.SetCompanyId.json.CompanyId}}/contact_to_company  ",
        "method": "PUT",
        "options": {
          "response": {
            "response": {}
          }
        },
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotAppToken"
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "c3a04c0e-6440-471d-a7a7-1e462a26fbb5",
      "name": "Link contact to company1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1040,
        1040
      ],
      "parameters": {
        "url": "=https://api.hubapi.com/crm/v3/objects/contact/{{ $node.Parsear.json.hs_object_id }}/associations/company/{{ $node.IF.json.results[0].id }}/contact_to_company  ",
        "method": "PUT",
        "options": {
          "response": {
            "response": {}
          }
        },
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotAppToken"
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "e08425a0-efb4-4c1b-be29-9c37c20fe95e",
      "name": "HubSpot1",
      "type": "n8n-nodes-base.hubspot",
      "position": [
        1260,
        1040
      ],
      "parameters": {
        "resource": "company",
        "companyId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $node.Set.json.CompanyId }}"
        },
        "operation": "update",
        "updateFields": {
          "customPropertiesUi": {
            "customPropertiesValues": [
              {
                "value": "[CL] Always On",
                "property": "registro_always_on"
              }
            ]
          }
        },
        "authentication": "appToken"
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    }
  ],
  "connections": {
    "IF": {
      "main": [
        [
          {
            "node": "HubSpot",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Link contact to company1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Search Hubspot Company",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HubSpot": {
      "main": [
        [
          {
            "node": "SetCompanyId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parsear": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Parsear",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SetCompanyId": {
      "main": [
        [
          {
            "node": "Link contact to company",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Hubspot Company": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Link contact to company1": {
      "main": [
        [
          {
            "node": "HubSpot1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {},
  "staticData": null,
  "versionId": "70c6c2a4-877f-4db6-90f7-81ac828acf5d",
  "triggerCount": 0,
  "tags": []
}