{
  "nodes": [
    {
      "id": "92b2057e-e458-4f30-9115-897bebe5570c",
      "name": "On Listen Event",
      "type": "n8n-nodes-base.webhook",
      "parameters": {
        "path": "98e20ba4-3a3a-4186-bf1a-1b51ee2075e0",
        "httpMethod": "POST"
      }
    },
    {
      "id": "0a2b0669-3996-4f64-930d-898504a15945",
      "name": "Set Fields",
      "type": "n8n-nodes-base.set",
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "event",
              "type": "string",
              "value": "={ { $json.headers.event } }"
            },
            {
              "name": "code",
              "type": "string",
              "value": "={ { $json.body.id } }"
            },
            {
              "name": "name",
              "type": "string",
              "value": "={ { $json.body.name } }"
            },
            {
              "name": "lastName",
              "type": "string",
              "value": "={ { $json.body.lastName } }"
            },
            {
              "name": "countryCode",
              "type": "string",
              "value": "={ { $json.body.phone.split(' ')[0].slice(1) } }"
            },
            {
              "name": "phone",
              "type": "string",
              "value": "={ { $json.body.phone.split(' ')[1] } }"
            },
            {
              "name": "email",
              "type": "string",
              "value": "={ { $json.body.email } }"
            },
            {
              "name": "portfolio_id",
              "type": "string",
              "value": "81"
            },
            {
              "name": "sequence_id",
              "type": "string",
              "value": "26"
            }
          ]
        }
      }
    },
    {
      "id": "0287c5be-737a-49be-8bc1-9d88a9381284",
      "name": "Routing",
      "type": "n8n-nodes-base.switch",
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "CREATED",
              "conditions": {
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={ { $json.event } }",
                    "rightValue": "insert"
                  }
                ]
              }
            },
            {
              "outputKey": "UPDATED",
              "conditions": {
                "conditions": [
                  {
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={ { $json.event } }",
                    "rightValue": "update"
                  }
                ]
              }
            }
          ]
        }
      }
    },
    {
      "id": "5fe454e8-3c55-451e-a822-33df71636b25",
      "name": "Filter Phone",
      "type": "n8n-nodes-base.filter",
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={ { $json.phone } }",
              "rightValue": ""
            },
            {
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={ { $json.phone } }",
              "rightValue": ""
            }
          ]
        }
      }
    },
    {
      "id": "cccd7822-bf56-4033-9f10-61d647fe5e36",
      "name": "Create Contact",
      "type": "n8n-nodes-beex.beex",
      "parameters": {
        "email": "={ { $json.email } }",
        "priority": 1,
        "resource": "leads",
        "code_client": "={ { $json.code } }",
        "sequence_id": "={ { $json.sequence_id } }",
        "code_country": "={ { $json.countryCode } }",
        "phone_number": "={ { $json.phone } }",
        "portfolio_id": "={ { $json.portfolio_id } }",
        "additionalParameters": {
          "text_01": "Example",
          "first_name": "={ { $json.name } }",
          "paternal_surname": "={ { $json.lastName } }"
        }
      }
    },
    {
      "id": "22cba1b3-8d23-4c33-8e1f-f56166956277",
      "name": "Get Client",
      "type": "n8n-nodes-beex.beex",
      "parameters": {
        "code": "={ { $json.code } }",
        "operation": "get-one"
      }
    },
    {
      "id": "a4704514-b369-4be3-8306-79b96d263c61",
      "name": "Update Client",
      "type": "n8n-nodes-beex.beex",
      "parameters": {
        "id": "={ { $json.results[0].id } }",
        "operation": "put",
        "first_name": "={ { $('Routing').item.json.name } }",
        "paternal_surname": "={ { $('Routing').item.json.lastName } }"
      }
    }
  ],
  "connections": {
    "Routing": {
      "main": [
        [
          {
            "node": "Filter Phone",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Client",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Client": {
      "main": [
        [
          {
            "node": "Update Client",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Fields": {
      "main": [
        [
          {
            "node": "Routing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Phone": {
      "main": [
        [
          {
            "node": "Create Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Contact": {
      "main": []
    },
    "On Listen Event": {
      "main": [
        [
          {
            "node": "Set Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}