{
  "name": "RecruitFlow - Nurture Delivery",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "recruitflow/nurture",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-000000000001",
      "name": "Nurture webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -620,
        0
      ],
      "webhookId": "b2000000-0000-4000-8000-0000000000ff"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "version": 2
          },
          "conditions": [
            {
              "id": "secret-check",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.headers['x-recruitflow-secret'] }}",
              "rightValue": "={{ $env.RECRUITFLOW_WEBHOOK_SECRET }}"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-000000000002",
      "name": "Authenticated?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -400,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Quiet hours: never message a candidate outside 08:00-20:00 on a weekday.\n// Anything outside the window is held, not dropped.\nconst now = new Date();\nconst hour = now.getUTCHours();\nconst day = now.getUTCDay();\nconst withinHours = hour >= 8 && hour < 20;\nconst weekday = day >= 1 && day <= 5;\nconst deliverNow = withinHours && weekday;\n\nreturn $input.all().map((item) => ({\n  json: {\n    ...(item.json.body ?? item.json),\n    deliver_now: deliverNow,\n    held_reason: deliverNow ? null : 'outside the 08:00-20:00 weekday window',\n  },\n}));"
      },
      "id": "b2000000-0000-4000-8000-000000000003",
      "name": "Quiet hours gate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -180,
        -100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "version": 2
          },
          "conditions": [
            {
              "id": "deliver-now",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.deliver_now }}",
              "rightValue": ""
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-000000000004",
      "name": "Deliver now?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        40,
        -100
      ]
    },
    {
      "parameters": {
        "amount": 12,
        "unit": "hours"
      },
      "id": "b2000000-0000-4000-8000-000000000005",
      "name": "Hold until morning",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        40,
        100
      ],
      "webhookId": "b2000000-0000-4000-8000-0000000000fe"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "is-sms",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.channel }}",
                    "rightValue": "sms"
                  }
                ],
                "combinator": "and"
              },
              "outputKey": "sms"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "is-email",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.channel }}",
                    "rightValue": "email"
                  }
                ],
                "combinator": "and"
              },
              "outputKey": "email"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "id": "b2000000-0000-4000-8000-000000000006",
      "name": "Route by channel",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        280,
        -100
      ]
    },
    {
      "parameters": {
        "resource": "sms",
        "operation": "send",
        "from": "={{ $env.RECRUITFLOW_SMS_FROM }}",
        "to": "={{ $json.destination }}",
        "message": "={{ $json.message }}",
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-000000000007",
      "name": "Send SMS",
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        520,
        -240
      ],
      "notesInFlow": true,
      "notes": "Attach Twilio credentials before activating."
    },
    {
      "parameters": {
        "sendTo": "={{ $json.destination }}",
        "subject": "Following up on your application",
        "emailType": "text",
        "message": "={{ $json.message }}",
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-000000000008",
      "name": "Send email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        520,
        -100
      ],
      "notesInFlow": true,
      "notes": "Swap for the client's email provider node if not Gmail."
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.RECRUITFLOW_N8N_URL }}/webhook/recruitflow/alert",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "event_type",
              "value": "nurture_undeliverable"
            },
            {
              "name": "level",
              "value": "warning"
            },
            {
              "name": "detail",
              "value": "={{ 'No contact channel for candidate ' + $json.candidate_id }}"
            },
            {
              "name": "candidate_id",
              "value": "={{ $json.candidate_id }}"
            },
            {
              "name": "conversation_id",
              "value": "={{ $json.conversation_id }}"
            }
          ]
        },
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-000000000009",
      "name": "No channel - alert",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        520,
        40
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ ok: true }) }}",
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-00000000000a",
      "name": "Respond OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        780,
        -100
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseCode": 401,
        "responseBody": "={{ JSON.stringify({ ok: false, error: 'invalid secret' }) }}",
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-00000000000b",
      "name": "Respond 401",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        -180,
        120
      ]
    }
  ],
  "connections": {
    "Nurture webhook": {
      "main": [
        [
          {
            "node": "Authenticated?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Authenticated?": {
      "main": [
        [
          {
            "node": "Quiet hours gate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond 401",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Quiet hours gate": {
      "main": [
        [
          {
            "node": "Deliver now?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Deliver now?": {
      "main": [
        [
          {
            "node": "Route by channel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Hold until morning",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hold until morning": {
      "main": [
        [
          {
            "node": "Route by channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by channel": {
      "main": [
        [
          {
            "node": "Send SMS",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No channel - alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send SMS": {
      "main": [
        [
          {
            "node": "Respond OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send email": {
      "main": [
        [
          {
            "node": "Respond OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No channel - alert": {
      "main": [
        [
          {
            "node": "Respond OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}