{
  "name": "FlowPantry - Service-due reminders (tune-ups, cleanings, check-ups)",
  "nodes": [
    {
      "id": "b066cc43-c895-47c3-b1f5-1fb37b7c79fc",
      "name": "Schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "daysInterval": 1,
              "triggerAtHour": 9
            }
          ]
        }
      }
    },
    {
      "id": "95328061-937b-4794-85ba-ac2d00983e0d",
      "name": "Get customers",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        240,
        300
      ],
      "parameters": {
        "url": "={{$env.CUSTOMERS_URL}}",
        "options": {}
      }
    },
    {
      "id": "d15737f5-2b93-4b69-8768-e2746549180b",
      "name": "Due in next 14 days",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const out=[];for(const i of $input.all()){const cs=Array.isArray(i.json)?i.json:(i.json.customers||[i.json]);for(const c of cs){const due=new Date(new Date(c.last_service).getTime()+(c.interval_days||180)*864e5);const days=(due-Date.now())/864e5;const key=due.toISOString().slice(0,10);if(days>=0&&days<=14&&c.reminded_for!==key)out.push({json:{...c,due:key,first:(c.name||'there').split(' ')[0]}});}}return out;"
      }
    },
    {
      "id": "abb82c37-16fd-40ee-a444-c0fc6aab4402",
      "name": "Text reminder",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        720,
        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}}, you're due for your next service with {{$env.BUSINESS_NAME}} around {{$json.due}}. Book a time: {{$env.BOOKING_URL}} Reply STOP to opt out."
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "8c685ef4-fc77-4627-82f6-2fda120e2a32",
      "name": "Email reminder",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        960,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.resend.com/emails",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({from: $env.FROM_EMAIL, to: $('Due in next 14 days').item.json.email, subject: 'Time for your next service', html: '<p>Hi '+$('Due in next 14 days').item.json.first+', you are due around '+$('Due in next 14 days').item.json.due+'. <a href=\"'+$env.BOOKING_URL+'\">Book here</a>.</p>'}) }}",
        "options": {}
      }
    }
  ],
  "connections": {
    "Schedule": {
      "main": [
        [
          {
            "node": "Get customers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get customers": {
      "main": [
        [
          {
            "node": "Due in next 14 days",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Due in next 14 days": {
      "main": [
        [
          {
            "node": "Text reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Text reminder": {
      "main": [
        [
          {
            "node": "Email reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}