{
  "name": "Birthday Retry Runner",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 10
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ],
      "id": "retry-trigger",
      "name": "Every 10 Minutes"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "backend-url",
              "name": "backend_url",
              "value": "http://127.0.0.1:4000",
              "type": "string"
            },
            {
              "id": "service-token",
              "name": "service_token",
              "value": "dev_n8n_service_token_0123456789abcdef0123456789abcdef",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        460,
        300
      ],
      "id": "set-vars",
      "name": "Set Variables"
    },
    {
      "parameters": {
        "url": "={{ $('Set Variables').first().json.backend_url }}/api/system/config",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-service-token",
              "value": "={{ $('Set Variables').first().json.service_token }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        680,
        300
      ],
      "id": "check-system",
      "name": "Check System Status"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "paused",
              "leftValue": "={{ $json.system_paused }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        300
      ],
      "id": "system-paused",
      "name": "System Paused?"
    },
    {
      "parameters": {
        "errorMessage": "System is paused, retry worker stopped"
      },
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        1120,
        200
      ],
      "id": "stop-paused",
      "name": "Stop (Paused)"
    },
    {
      "parameters": {
        "url": "={{ $('Set Variables').first().json.backend_url }}/api/retry-queue/due",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-service-token",
              "value": "={{ $('Set Variables').first().json.service_token }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1120,
        400
      ],
      "id": "fetch-due-retries",
      "name": "Fetch Due Retries"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "has-retries",
              "leftValue": "={{ $json.retries?.length || 0 }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1340,
        400
      ],
      "id": "has-retries",
      "name": "Has Retries?"
    },
    {
      "parameters": {
        "jsCode": "const retries = $json.retries || [];\nreturn retries.map(r => ({ json: r }));"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1560,
        400
      ],
      "id": "split-retries",
      "name": "Split Retries"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "is-email",
              "leftValue": "={{ $json.channel }}",
              "rightValue": "EMAIL",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1780,
        400
      ],
      "id": "channel-split",
      "name": "Channel?"
    },
    {
      "parameters": {
        "fromEmail": "info@diplomaticresidences.in",
        "toEmail": "={{ $json.payload.email }}",
        "subject": "={{ $json.payload.subject || 'Birthday Greetings' }}",
        "message": "={{ $json.payload.message || 'Happy Birthday!' }}",
        "options": {}
      },
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        2000,
        300
      ],
      "id": "retry-email",
      "name": "Retry Email Send",
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $json.payload.whatsapp_url || 'https://live-mt-server.wati.io/304793/api/v1/sendTemplateMessage' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.payload.whatsapp_body || {}) }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2000,
        500
      ],
      "id": "retry-whatsapp",
      "name": "Retry WhatsApp Send",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "success",
              "leftValue": "={{ $json.messageId || $json.result }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2220,
        400
      ],
      "id": "retry-success",
      "name": "Retry Success?"
    },
    {
      "parameters": {
        "method": "DELETE",
        "url": "={{ $('Set Variables').first().json.backend_url }}/api/retry-queue/{{ $json.id }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-service-token",
              "value": "={{ $('Set Variables').first().json.service_token }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        2440,
        300
      ],
      "id": "delete-retry",
      "name": "Delete Retry Job"
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "={{ $('Set Variables').first().json.backend_url }}/api/retry-queue/{{ $json.id }}/increment",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-service-token",
              "value": "={{ $('Set Variables').first().json.service_token }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        2440,
        500
      ],
      "id": "increment-retry",
      "name": "Increment Retry Count"
    }
  ],
  "connections": {
    "Every 10 Minutes": {
      "main": [
        [
          {
            "node": "Set Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Variables": {
      "main": [
        [
          {
            "node": "Check System Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check System Status": {
      "main": [
        [
          {
            "node": "System Paused?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "System Paused?": {
      "main": [
        [
          {
            "node": "Stop (Paused)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fetch Due Retries",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Due Retries": {
      "main": [
        [
          {
            "node": "Has Retries?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Retries?": {
      "main": [
        [
          {
            "node": "Split Retries",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Retries": {
      "main": [
        [
          {
            "node": "Channel?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Channel?": {
      "main": [
        [
          {
            "node": "Retry Email Send",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Retry WhatsApp Send",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retry Email Send": {
      "main": [
        [
          {
            "node": "Retry Success?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retry WhatsApp Send": {
      "main": [
        [
          {
            "node": "Retry Success?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retry Success?": {
      "main": [
        [
          {
            "node": "Delete Retry Job",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Increment Retry Count",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  }
}