{
  "name": "[SMS] Dispatch Queue",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 10
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Every 10 Minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://dabbahwala-latest.onrender.com/api/sms/pending",
        "options": {}
      },
      "id": "fetch-pending-sms",
      "name": "Fetch Pending SMS Contacts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "has-sms",
              "leftValue": "={{ $json.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "has-pending",
      "name": "Has Pending SMS?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const contacts = $input.first().json;\nif (!Array.isArray(contacts) || contacts.length === 0) return [];\nreturn contacts.map(item => ({ json: item }));"
      },
      "id": "split-contacts",
      "name": "Split Contacts",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        240
      ]
    },
    {
      "parameters": {
        "jsCode": "// Build SMS payload based on sms_level and lifecycle\nconst contact = $input.item.json;\nconst smsLevel = contact.sms_level || 1;\nconst lifecycle = contact.lifecycle || 'cold';\n\nlet messageBody = '';\n\nif (lifecycle === 'new_customer') {\n  messageBody = `Hi ${contact.contact_email.split('@')[0]}! Thanks for your recent order with DabbahWala. Reply YES for exclusive offers.`;\n} else if (smsLevel >= 3) {\n  messageBody = `We miss you! Your favorites are ready for reorder. Reply ORDER or call us \u2014 DabbahWala`;\n} else if (smsLevel >= 2) {\n  messageBody = `Special offer just for you from DabbahWala! Check your email for details. Reply STOP to opt out.`;\n} else {\n  messageBody = `DabbahWala: Fresh items available this week! Reply STOP to opt out.`;\n}\n\nreturn {\n  json: {\n    contact_id: contact.contact_id,\n    phone: contact.phone,\n    email: contact.contact_email,\n    message_body: messageBody,\n    sms_level: smsLevel\n  }\n};"
      },
      "id": "build-message",
      "name": "Build SMS Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        240
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telnyx.com/v2/messages",
        "authentication": "none",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"from\": \"{{ $('Get Credentials').first().json.TELNYX_FROM_NUMBER }}\",\n  \"to\": \"{{ $json.phone }}\",\n  \"text\": \"{{ $json.message_body }}\"\n}",
        "options": {},
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Get Credentials').first().json.TELNYX_API_KEY }}"
            }
          ]
        }
      },
      "id": "send-telnyx-sms",
      "name": "Send via Telnyx",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        240
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://dabbahwala-latest.onrender.com/api/sms/{{ $('Build SMS Message').item.json.contact_id }}/sent",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"telnyx_msg_id\": \"{{ $json.data?.id || '' }}\"\n}",
        "options": {}
      },
      "id": "mark-sms-sent",
      "name": "Mark SMS Sent",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1540,
        240
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://dabbahwala-latest.onrender.com/api/telnyx/message",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"contact_email\": \"{{ $('Build SMS Message').item.json.email }}\",\n  \"direction\": \"outbound\",\n  \"from_number\": \"{{ $('Get Credentials').first().json.TELNYX_FROM_NUMBER }}\",\n  \"to_number\": \"{{ $('Build SMS Message').item.json.phone }}\",\n  \"body\": \"{{ $('Build SMS Message').item.json.message_body }}\",\n  \"telnyx_msg_id\": \"{{ $json.data?.id || '' }}\",\n  \"status\": \"sent\",\n  \"is_delivery_staff\": false,\n  \"metadata\": {}\n}",
        "options": {}
      },
      "id": "store-message",
      "name": "Store in Telnyx Messages",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1760,
        240
      ]
    },
    {
      "parameters": {},
      "id": "no-pending",
      "name": "No Pending \u2014 Skip",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        880,
        400
      ]
    },
    {
      "id": "get-credentials",
      "name": "Get Credentials",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        470,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "https://dabbahwala-latest.onrender.com/api/credentials",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every 10 Minutes": {
      "main": [
        [
          {
            "node": "Get Credentials",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Pending SMS Contacts": {
      "main": [
        [
          {
            "node": "Has Pending SMS?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Pending SMS?": {
      "main": [
        [
          {
            "node": "Split Contacts",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Pending \u2014 Skip",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Contacts": {
      "main": [
        [
          {
            "node": "Build SMS Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build SMS Message": {
      "main": [
        [
          {
            "node": "Send via Telnyx",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send via Telnyx": {
      "main": [
        [
          {
            "node": "Mark SMS Sent",
            "type": "main",
            "index": 0
          },
          {
            "node": "Store in Telnyx Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Get Credentials",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Credentials": {
      "main": [
        [
          {
            "node": "Fetch Pending SMS Contacts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "DabbahWala",
      "id": "dabbahwala"
    },
    {
      "name": "Telnyx",
      "id": "tag-telnyx"
    },
    {
      "name": "Action",
      "id": "tag-action"
    }
  ]
}