{
  "name": "B2B Toplu CSV Kampanya ve GSM-7 Optimizasyonu (Başlıklı SMS)",
  "nodes": [
    {
      "parameters": {},
      "id": "csv-manual-trigger",
      "name": "CSV Kampanya Başlat",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [180, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://basliklisms.com/api/v1/opt-outs?page=1&limit=100",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "X-API-Key", "value": "={{ $env.BASLIKLISMS_API_KEY }}" }
          ]
        }
      },
      "id": "fetch-opt-outs",
      "name": "Opt-Out (RET) Listesini Çek",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [420, 300]
    },
    {
      "parameters": {
        "jsCode": "const map = {'ç':'c','Ç':'C','ğ':'g','Ğ':'G','ı':'i','İ':'I','ö':'o','Ö':'O','ş':'s','Ş':'S','ü':'u','Ü':'U'};\nconst rawRows = Array.isArray($input.first().json.contacts) ? $input.first().json.contacts : ['05321234567', '+90 532 123 45 68'];\nconst optOutRows = Array.isArray($input.first().json.data?.items) ? $input.first().json.data.items.map((x) => String(x.phone || '')) : [];\nconst normalize = (p) => {\n  const d = String(p).replace(/\\D/g, '');\n  if (d.startsWith('90') && d.length === 12) return '0' + d.slice(2);\n  if (d.length === 10 && d.startsWith('5')) return '0' + d;\n  return d.length === 11 && d.startsWith('05') ? d : '';\n};\nconst optSet = new Set(optOutRows.map(normalize).filter(Boolean));\nconst uniqueRecipients = [...new Set(rawRows.map(normalize).filter((p) => p && !optSet.has(p)))];\nconst rawMessage = 'Değerli müşterimiz, B2B toplu alım indirimi başladı. İptal için RET yazabilirsiniz.';\nconst message = rawMessage.replace(/[çÇğĞıİöÖşŞüÜ]/g, (c) => map[c]);\nconst batches = [];\nfor (let i = 0; i < uniqueRecipients.length; i += 1000) {\n  batches.push({\n    json: {\n      recipients: uniqueRecipients.slice(i, i + 1000),\n      message,\n      idempotencyKey: `b2b-csv-batch-${Math.floor(i / 1000) + 1}`,\n    },\n  });\n}\nreturn batches;"
      },
      "id": "clean-gsm7-batch",
      "name": "Numara Temizle ve GSM-7 Optimize Et",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://basliklisms.com/api/v1/sms/send",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "X-API-Key", "value": "={{ $env.BASLIKLISMS_API_KEY }}" },
            { "name": "Idempotency-Key", "value": "={{ $json.idempotencyKey }}" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ header: 'MARKANIZ', message: $json.message, recipients: $json.recipients, commercial: true }) }}"
      },
      "id": "send-b2b-bulk-sms",
      "name": "Başlıklı SMS Toplu Gönder",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [940, 300]
    }
  ],
  "connections": {
    "CSV Kampanya Başlat": {
      "main": [[{ "node": "Opt-Out (RET) Listesini Çek", "type": "main", "index": 0 }]]
    },
    "Opt-Out (RET) Listesini Çek": {
      "main": [[{ "node": "Numara Temizle ve GSM-7 Optimize Et", "type": "main", "index": 0 }]]
    },
    "Numara Temizle ve GSM-7 Optimize Et": {
      "main": [[{ "node": "Başlıklı SMS Toplu Gönder", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" }
}
