{
  "name": "Sincronizacion Gmail & Baserow CRM",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "simple": true,
        "filters": {
          "q": "to:direccion@mariomojica.com -from:mariomojica.com"
        }
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        250,
        300
      ],
      "id": "gmail-trigger-id",
      "name": "Gmail Trigger: Nuevo Correo",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "getAll",
        "databaseId": 144,
        "tableId": 600,
        "returnAll": false,
        "limit": 1,
        "additionalOptions": {
          "filters": {
            "fields": [
              {
                "field": "Correo",
                "operator": "equal",
                "value": "={{ $json.from.value[0].address }}"
              }
            ]
          }
        }
      },
      "type": "n8n-nodes-base.baserow",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "id": "baserow-buscar-lead",
      "name": "Baserow: Buscar Lead",
      "credentials": {
        "baserowApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "cond-1",
              "leftValue": "={{ $json.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "larger",
                "name": "filter.operator.larger"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        750,
        300
      ],
      "id": "if-existe-lead",
      "name": "IF: \u00bfExiste Lead?"
    },
    {
      "parameters": {
        "operation": "create",
        "databaseId": 144,
        "tableId": 988,
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": 9461,
              "fieldValue": "={{ [ $('Baserow: Buscar Lead').item.json[0].id ] }}"
            },
            {
              "fieldId": 9458,
              "fieldValue": "={{ $('Gmail Trigger: Nuevo Correo').item.json.subject }}"
            },
            {
              "fieldId": 9463,
              "fieldValue": "={{ $('Gmail Trigger: Nuevo Correo').item.json.text }}"
            },
            {
              "fieldId": 9464,
              "fieldValue": "Entrante"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.baserow",
      "typeVersion": 1,
      "position": [
        1000,
        150
      ],
      "id": "baserow-registrar-interaccion",
      "name": "Baserow: Registrar Interacci\u00f3n",
      "credentials": {
        "baserowApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "databaseId": 144,
        "tableId": 600,
        "rowId": "={{ $('Baserow: Buscar Lead').item.json[0].id }}",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": 9457,
              "fieldValue": "En Seguimiento"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.baserow",
      "typeVersion": 1,
      "position": [
        1250,
        150
      ],
      "id": "baserow-actualizar-lead-estado",
      "name": "Baserow: Actualizar Estado CRM",
      "credentials": {
        "baserowApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "phoneNumberId": "1072994099224530",
        "recipientPhoneNumber": "+573117646907",
        "template": "plantilla_06|es",
        "components": {
          "component": [
            {
              "bodyParameters": {
                "parameter": [
                  {
                    "text": "={{ $('Baserow: Buscar Lead').item.json[0].Nombre }} {{ $('Baserow: Buscar Lead').item.json[0].Apellido }}"
                  },
                  {
                    "text": "Ha respondido a tu correo."
                  },
                  {
                    "text": "={{ $('Gmail Trigger: Nuevo Correo').item.json.subject }}"
                  },
                  {
                    "text": "CRM"
                  },
                  {
                    "text": "En Seguimiento"
                  }
                ]
              }
            }
          ]
        }
      },
      "type": "n8n-nodes-base.whatsApp",
      "typeVersion": 1.1,
      "position": [
        1500,
        150
      ],
      "id": "whatsapp-alerta-respuesta",
      "name": "WhatsApp: Alerta de Respuesta",
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "cond-negocio",
              "leftValue": "={{ $node[\"Gmail Trigger: Nuevo Correo\"].json.from.value[0].address }}",
              "rightValue": "^(noreply|no-reply|notification|billing|support|newsletter|spam|hello|info|welcome|alert|alerts|update|updates|job|jobs|verify|verify-email|verify-account|accounts|security|admin)@",
              "operator": {
                "type": "string",
                "operation": "notRegex",
                "name": "filter.operator.notRegex"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1000,
        450
      ],
      "id": "if-correo-negocio",
      "name": "IF: \u00bfEs Correo de Negocio?"
    },
    {
      "parameters": {
        "operation": "create",
        "databaseId": 144,
        "tableId": 600,
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": 5554,
              "fieldValue": "={{ $node[\"Gmail Trigger: Nuevo Correo\"].json.from.value[0].name ? $node[\"Gmail Trigger: Nuevo Correo\"].json.from.value[0].name.split(' ')[0] : 'Nuevo' }}"
            },
            {
              "fieldId": 5557,
              "fieldValue": "={{ $node[\"Gmail Trigger: Nuevo Correo\"].json.from.value[0].name ? $node[\"Gmail Trigger: Nuevo Correo\"].json.from.value[0].name.split(' ').slice(1).join(' ') : 'Lead' }}"
            },
            {
              "fieldId": 5558,
              "fieldValue": "={{ $node[\"Gmail Trigger: Nuevo Correo\"].json.from.value[0].address }}"
            },
            {
              "fieldId": 9457,
              "fieldValue": "Prospecto"
            },
            {
              "fieldId": 9455,
              "fieldValue": "Lead creado autom\u00e1ticamente desde Gmail. Asunto: {{ $node[\"Gmail Trigger: Nuevo Correo\"].json.subject }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.baserow",
      "typeVersion": 1,
      "position": [
        1250,
        400
      ],
      "id": "baserow-crear-lead",
      "name": "Baserow: Crear Lead",
      "credentials": {
        "baserowApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "phoneNumberId": "1072994099224530",
        "recipientPhoneNumber": "+573117646907",
        "template": "plantilla_06|es",
        "components": {
          "component": [
            {
              "bodyParameters": {
                "parameter": [
                  {
                    "text": "={{ $node[\"Gmail Trigger: Nuevo Correo\"].json.from.value[0].name || 'Nuevo Lead' }}"
                  },
                  {
                    "text": "Nuevo Prospecto por Correo"
                  },
                  {
                    "text": "={{ $node[\"Gmail Trigger: Nuevo Correo\"].json.subject }}"
                  },
                  {
                    "text": "CRM"
                  },
                  {
                    "text": "Nuevo"
                  }
                ]
              }
            }
          ]
        }
      },
      "type": "n8n-nodes-base.whatsApp",
      "typeVersion": 1.1,
      "position": [
        1500,
        400
      ],
      "id": "whatsapp-alerta-nuevo-lead",
      "name": "WhatsApp: Alerta de Nuevo Lead",
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Gmail Trigger: Nuevo Correo": {
      "main": [
        [
          {
            "node": "Baserow: Buscar Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Baserow: Buscar Lead": {
      "main": [
        [
          {
            "node": "IF: \u00bfExiste Lead?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: \u00bfExiste Lead?": {
      "main": [
        [
          {
            "node": "Baserow: Registrar Interacci\u00f3n",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "IF: \u00bfEs Correo de Negocio?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Baserow: Registrar Interacci\u00f3n": {
      "main": [
        [
          {
            "node": "Baserow: Actualizar Estado CRM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Baserow: Actualizar Estado CRM": {
      "main": [
        [
          {
            "node": "WhatsApp: Alerta de Respuesta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: \u00bfEs Correo de Negocio?": {
      "main": [
        [
          {
            "node": "Baserow: Crear Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Baserow: Crear Lead": {
      "main": [
        [
          {
            "node": "WhatsApp: Alerta de Nuevo Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}