{
  "name": "FlowPantry - Auto-fill cancelled slots from a waitlist",
  "nodes": [
    {
      "id": "6f6cb7f0-6b06-4cb6-aead-d6205a2516d3",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "appointment-cancelled",
        "responseMode": "onReceived",
        "options": {}
      },
      "webhookId": "d39bbcd8-3bfd-4c56-bd16-eecff44db54b"
    },
    {
      "id": "cecf47f7-a311-448c-bbd2-3259de6ec063",
      "name": "Slot details",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "return $input.all().map(i=>{const b=i.json.body||i.json;const p=b.payload||b;const start=(p.scheduled_event&&p.scheduled_event.start_time)||p.start||p.start_time;return {json:{when:new Date(start).toLocaleString('en-US',{weekday:'short',month:'short',day:'numeric',hour:'numeric',minute:'2-digit'})}}});"
      }
    },
    {
      "id": "3c5f2045-4e1a-4e9d-a620-a859705a6f96",
      "name": "Get waitlist",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        300
      ],
      "parameters": {
        "url": "={{$env.WAITLIST_URL}}",
        "options": {}
      }
    },
    {
      "id": "2c5bed75-b509-4abf-97f3-9eb0d95d7a39",
      "name": "First 3 opted-in",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const out=[];for(const i of $input.all()){const ws=Array.isArray(i.json)?i.json:(i.json.waitlist||[i.json]);for(const w of ws){if(w.phone&&w.sms_opt_in!==false&&out.length<3)out.push({json:{...w,first:(w.name||'there').split(' ')[0],when:$('Slot details').first().json.when}});}}return out;"
      }
    },
    {
      "id": "744e0f02-a94a-4b51-a60a-18d403b029b4",
      "name": "Offer the slot",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        960,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "=https://api.twilio.com/2010-04-01/Accounts/{{$env.TWILIO_ACCOUNT_SID}}/Messages.json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "contentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "={{$json.phone}}"
            },
            {
              "name": "From",
              "value": "={{$env.TWILIO_FROM_NUMBER}}"
            },
            {
              "name": "Body",
              "value": "=Hi {{$json.first}}! A spot just opened at {{$env.BUSINESS_NAME}}: {{$json.when}}. First to book gets it: {{$env.BOOKING_URL}} Reply STOP to opt out."
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Slot details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slot details": {
      "main": [
        [
          {
            "node": "Get waitlist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get waitlist": {
      "main": [
        [
          {
            "node": "First 3 opted-in",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "First 3 opted-in": {
      "main": [
        [
          {
            "node": "Offer the slot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}