{
  "name": "Premium / Referral notifications & loyalty hints",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      },
      "name": "Cron (every 5m)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select e.id, e.payload, e.contact_id as referrer_contact_id, e.created_at,\n       r.invitee_contact_id, inv.full_name as invitee_name,\n       ref_ci.channel as referrer_channel, ref_ci.external_id as referrer_external_id,\n       inv_ci.channel as invitee_channel,  inv_ci.external_id as invitee_external_id\n  from public.events e\n  join public.referrals r on r.referrer_contact_id = e.contact_id\n  join public.bot_contacts inv on inv.id = r.invitee_contact_id\n  left join public.bot_contact_identities ref_ci on ref_ci.contact_id = e.contact_id and ref_ci.is_blocked = false\n  left join public.bot_contact_identities inv_ci on inv_ci.contact_id = r.invitee_contact_id and inv_ci.is_blocked = false\n where e.type = 'referral.qualified'\n   and e.created_at > now() - interval '6 minutes'\n order by e.created_at asc"
      },
      "name": "Postgres (qualified referrals)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        460,
        220
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches (1)",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        680,
        220
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "referrerText",
              "value": "\u2705 {{$json.invitee_name || '\u0412\u0430\u0448 \u0434\u0440\u0443\u0433'}}, \u0432\u0430\u0448 \u0434\u0440\u0443\u0433 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b \u0437\u0430\u043a\u0430\u0437!\n\u041d\u0430 \u0432\u0430\u0448 \u0441\u0447\u0451\u0442 \u043d\u0430\u0447\u0438\u0441\u043b\u0435\u043d\u043e 500 \u20bd \u2014 \u0441\u043a\u0438\u0434\u043a\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0437\u0430\u043a\u0430\u0437\u0435."
            },
            {
              "name": "inviteeText",
              "value": "\ud83c\udf81 \u0413\u043e\u0442\u043e\u0432\u043e! \u0417\u0430\u043a\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u2014 \u043d\u0430 \u0432\u0430\u0448 \u0441\u0447\u0451\u0442 \u043d\u0430\u0447\u0438\u0441\u043b\u0435\u043d\u043e 500 \u20bd \u0441\u043a\u0438\u0434\u043a\u0438 \u0437\u0430 \u0442\u043e, \u0447\u0442\u043e \u0432\u044b \u043f\u0440\u0438\u0448\u043b\u0438 \u043f\u043e \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u044e. \u0421\u043a\u0438\u0434\u043a\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0437\u0430\u043a\u0430\u0437\u0435."
            },
            {
              "name": "ownerText",
              "value": "\ud83c\udf89 \u0420\u0435\u0444\u0435\u0440\u0430\u043b \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b!\n\u041a\u0442\u043e \u043f\u0440\u0438\u0432\u0451\u043b: referrer #{{$json.referrer_contact_id}}\n\u041a\u0442\u043e \u043f\u0440\u0438\u0448\u0451\u043b: {{$json.invitee_name}}\n\u041a\u0430\u0436\u0434\u043e\u043c\u0443 \u043d\u0430\u0447\u0438\u0441\u043b\u0435\u043d\u043e 500 \u20bd."
            }
          ]
        }
      },
      "name": "Set (texts)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [
        900,
        220
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.referrer_channel}}",
              "operation": "equals",
              "value2": "telegram"
            }
          ]
        }
      },
      "name": "IF referrer telegram",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1120,
        120
      ]
    },
    {
      "parameters": {
        "chatId": "={{$json.referrer_external_id}}",
        "text": "={{$json.referrerText}}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "Telegram (referrer)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.1,
      "position": [
        1340,
        60
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.MAX_API_URL}}/messages?access_token={{$env.MAX_BOT_TOKEN}}&user_id={{$json.referrer_external_id}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"text\": \"{{$json.referrerText}}\" }"
      },
      "name": "MAX (referrer)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        180
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.invitee_channel}}",
              "operation": "equals",
              "value2": "telegram"
            }
          ]
        }
      },
      "name": "IF invitee telegram",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1120,
        320
      ]
    },
    {
      "parameters": {
        "chatId": "={{$json.invitee_external_id}}",
        "text": "={{$json.inviteeText}}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "Telegram (invitee)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.1,
      "position": [
        1340,
        260
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.MAX_API_URL}}/messages?access_token={{$env.MAX_BOT_TOKEN}}&user_id={{$json.invitee_external_id}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"text\": \"{{$json.inviteeText}}\" }"
      },
      "name": "MAX (invitee)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        380
      ]
    },
    {
      "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,
        500
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron (every 5m)": {
      "main": [
        [
          {
            "node": "Postgres (qualified referrals)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres (qualified referrals)": {
      "main": [
        [
          {
            "node": "SplitInBatches (1)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches (1)": {
      "main": [
        [
          {
            "node": "Set (texts)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set (texts)": {
      "main": [
        [
          {
            "node": "IF referrer telegram",
            "type": "main",
            "index": 0
          },
          {
            "node": "IF invitee telegram",
            "type": "main",
            "index": 0
          },
          {
            "node": "Telegram (notify owner)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF referrer telegram": {
      "main": [
        [
          {
            "node": "Telegram (referrer)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "MAX (referrer)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF invitee telegram": {
      "main": [
        [
          {
            "node": "Telegram (invitee)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "MAX (invitee)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "tags": [
    "premium",
    "referral",
    "loyalty"
  ]
}