{
  "name": "Premium / Seasonal pool open (cron)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 10 1,15 5 *"
            }
          ]
        }
      },
      "name": "Cron (May 1 & 15, 10:00)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select * from public.segment_for_seasonal_service('pool_cleaning', 'seasonal_pool_open', null);"
      },
      "name": "Postgres (segment)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        460,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "batchSize": 15,
        "options": {}
      },
      "name": "SplitInBatches (15)",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "amount": 2,
        "unit": "seconds"
      },
      "name": "Wait (2s)",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.channel}}",
              "operation": "equals",
              "value2": "telegram"
            }
          ]
        }
      },
      "name": "IF (telegram?)",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "={{$json.external_id}}",
        "text": "\ud83c\udfca {{$json.full_name || '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435'}}, \u043b\u0435\u0442\u043e \u043d\u0430 \u043f\u043e\u0440\u043e\u0433\u0435!\n\u041f\u043e\u0440\u0430 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u0430\u0448 \u0431\u0430\u0441\u0441\u0435\u0439\u043d: \u0447\u0438\u0441\u0442\u043a\u0430 \u0447\u0430\u0448\u0438, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f, \u0445\u0438\u043c\u0438\u044f \u2014 \u0432\u0441\u0451 \u00ab\u043f\u043e\u0434 \u043a\u043b\u044e\u0447\u00bb.\n\u0425\u043e\u0442\u0438\u0442\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f? /start",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "Telegram (send)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.1,
      "position": [
        1340,
        200
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.MAX_API_URL}}/messages?access_token={{$env.MAX_BOT_TOKEN}}&user_id={{$json.external_id}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"text\": \"\ud83c\udfca \u041b\u0435\u0442\u043e \u043d\u0430 \u043f\u043e\u0440\u043e\u0433\u0435 \u2014 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0432\u0430\u0448 \u0431\u0430\u0441\u0441\u0435\u0439\u043d \u00ab\u043f\u043e\u0434 \u043a\u043b\u044e\u0447\u00bb! \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c?\" }"
      },
      "name": "HTTP (MAX send)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        400
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "insert into public.campaign_recipients (campaign_id, contact_id, channel, status, sent_at)\nselect c.id, '{{$json.contact_id}}'::uuid, '{{$json.channel}}'::messenger_channel, 'sent', now()\n  from public.campaigns c where c.code = 'seasonal_pool_open'\non conflict (campaign_id, contact_id, channel) do update set status='sent', sent_at=now();"
      },
      "name": "Postgres (mark sent)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        1560,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron (May 1 & 15, 10:00)": {
      "main": [
        [
          {
            "node": "Postgres (segment)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres (segment)": {
      "main": [
        [
          {
            "node": "SplitInBatches (15)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches (15)": {
      "main": [
        [
          {
            "node": "Wait (2s)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait (2s)": {
      "main": [
        [
          {
            "node": "IF (telegram?)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF (telegram?)": {
      "main": [
        [
          {
            "node": "Telegram (send)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP (MAX send)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram (send)": {
      "main": [
        [
          {
            "node": "Postgres (mark sent)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP (MAX send)": {
      "main": [
        [
          {
            "node": "Postgres (mark sent)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "tags": [
    "premium",
    "campaigns",
    "pool"
  ]
}