{
  "name": "Premium / Lead events (lead.created \u2192 notify owner)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-events",
        "responseMode": "responseNode",
        "options": {
          "responseHeaders": {
            "values": []
          },
          "rawBody": false
        }
      },
      "name": "Webhook (lead-events)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.headers['x-premium-secret']}}",
              "operation": "equals",
              "value2": "={{$env.PREMIUM_INBOUND_SECRET}}"
            }
          ]
        }
      },
      "name": "IF (verify secret)",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "Unauthorized",
        "options": {
          "responseCode": 401
        }
      },
      "name": "Respond 401",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        680,
        460
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.body.type}}",
              "operation": "equals",
              "value2": "lead.created"
            }
          ]
        }
      },
      "name": "Switch (event type)",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        240
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select l.id as lead_id, l.service_kind, l.area_value, l.area_unit, l.district, l.description, l.metadata,\n       c.id as contact_id, c.full_name, c.phone,\n       ci.channel, ci.external_id\n  from public.leads l\n  join public.contacts c on c.id = l.contact_id\n  left join public.contact_identities ci on ci.contact_id = c.id and ci.channel = l.channel\n where l.id = '{{$json.body.leadId}}'\n limit 1",
        "options": {}
      },
      "name": "Postgres (load lead)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        900,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "ownerText",
              "value": "\ud83c\udd95 \u041d\u043e\u0432\u044b\u0439 \u043b\u0438\u0434\n\u0423\u0441\u043b\u0443\u0433\u0430: {{$json.service_kind}}\n\u041a\u043b\u0438\u0435\u043d\u0442: {{$json.full_name || '\u2014'}}\n\u0422\u0435\u043b\u0435\u0444\u043e\u043d: {{$json.phone || '\u2014'}}\n\u041f\u043b\u043e\u0449\u0430\u0434\u044c: {{$json.area_value}} {{$json.area_unit}}\n\u0420\u0430\u0439\u043e\u043d: {{$json.district || '\u2014'}}\n\u041a\u0430\u043d\u0430\u043b: {{$json.channel}}\n\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435: {{$json.description || '\u2014'}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set (owner text)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [
        1120,
        200
      ]
    },
    {
      "parameters": {
        "chatId": "={{$env.TELEGRAM_OWNER_CHAT_ID}}",
        "text": "={{$json.ownerText}}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "Telegram (notify owner)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.1,
      "position": [
        1340,
        120
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "insert into public.events (type, contact_id, lead_id, payload)\nvalues ('owner.notified', '{{$json.contact_id}}', '{{$json.lead_id}}', '{}'::jsonb);"
      },
      "name": "Postgres (event: notified)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        1340,
        280
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true }"
      },
      "name": "Respond 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1560,
        200
      ]
    }
  ],
  "connections": {
    "Webhook (lead-events)": {
      "main": [
        [
          {
            "node": "IF (verify secret)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF (verify secret)": {
      "main": [
        [
          {
            "node": "Switch (event type)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond 401",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch (event type)": {
      "main": [
        [
          {
            "node": "Postgres (load lead)",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Postgres (load lead)": {
      "main": [
        [
          {
            "node": "Set (owner text)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set (owner text)": {
      "main": [
        [
          {
            "node": "Telegram (notify owner)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Postgres (event: notified)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram (notify owner)": {
      "main": [
        [
          {
            "node": "Respond 200",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "tags": [
    "premium",
    "leads"
  ]
}