{
  "name": "WF-TV-SMS-QUEUE: Delayed SMS Sender",
  "nodes": [
    {
      "id": "cron_trigger",
      "name": "Every 30 Minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 30
            }
          ]
        }
      }
    },
    {
      "id": "fetch_due",
      "name": "Fetch Due Items",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_CONVEX_DEPLOYMENT/api/sms-queue-due",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"product_slug\": \"voicesdr\", \"due_before\": {{ Date.now() }}}",
        "options": {}
      }
    },
    {
      "id": "has_items",
      "name": "Has Items?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "has_items_cond",
              "leftValue": "={{ $json.count }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ]
        },
        "looseTypeValidation": true,
        "options": {}
      }
    },
    {
      "id": "extract_items",
      "name": "Extract Items",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        240
      ],
      "parameters": {
        "jsCode": "var items=$json.leads||[]; return items.map(function(item){ return {json:item}; });"
      }
    },
    {
      "id": "loop_items",
      "name": "Loop Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        880,
        240
      ],
      "parameters": {
        "batchSize": 1,
        "options": {}
      }
    },
    {
      "id": "send_sms",
      "name": "Send Queued SMS",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/activity/sms/",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({lead_id: $json.closeLeadId, remote_phone: $json.phone, local_phone: \"+15555550100\", text: $json.smsText, status: \"outbox\"}) }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "closeApi",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "mark_complete",
      "name": "Mark Complete",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_CONVEX_DEPLOYMENT/api/sms-queue-complete",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({queue_id: $('Loop Items').first().json._id}) }}",
        "options": {}
      }
    },
    {
      "id": "sticky_note",
      "name": "WF-TV-SMS-QUEUE Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        100
      ],
      "parameters": {
        "content": "## WF-TV-SMS-QUEUE: Delayed SMS Sender\n\nSends SMS blocked by WF-OB-2's 6-hour cooldown. Cron (30min) -> Fetch due from Convex smsQueue -> Send via Close CRM -> Mark complete. Credential: title-close (Laura, +15555550100)."
      }
    },
    {
      "id": "log_sms_cost",
      "name": "Log SMS Cost to CC",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        450
      ],
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_CONVEX_DEPLOYMENT/api/service-expenses/create",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\"serviceName\": \"Close SMS\", \"productId\": \"voicesdr\", \"date\": \"{{ $now.format('yyyy-MM-dd') }}\", \"amount\": 0.02, \"units\": 1, \"unitType\": \"sms\", \"notes\": \"Queued SMS sent\", \"source\": \"n8n\"}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {
    "Every 30 Minutes": {
      "main": [
        [
          {
            "node": "Fetch Due Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Due Items": {
      "main": [
        [
          {
            "node": "Has Items?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Items?": {
      "main": [
        [
          {
            "node": "Extract Items",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Extract Items": {
      "main": [
        [
          {
            "node": "Loop Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Items": {
      "main": [
        [],
        [
          {
            "node": "Send Queued SMS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Queued SMS": {
      "main": [
        [
          {
            "node": "Mark Complete",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log SMS Cost to CC",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark Complete": {
      "main": [
        [
          {
            "node": "Loop Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false
}