{
  "id": "steinheim-w04-catalog-sync",
  "name": "W04 \u2014 Catalogue sync",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 4 * * *"
            }
          ]
        }
      },
      "id": "w04-cron",
      "name": "04:00 Cairo",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -200,
        0
      ],
      "notes": "Runs before W01 at 09:00 so the day's content is written against a catalogue read this morning, not yesterday's."
    },
    {
      "parameters": {
        "method": "POST",
        "url": "__APP_URL__/api/public/automation/catalog-sync",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-automation-timestamp",
              "value": "={{ Date.now() }}"
            },
            {
              "name": "x-automation-nonce",
              "value": "={{ $execution.id }}-{{ Date.now() }}"
            }
          ]
        },
        "options": {
          "timeout": 900000,
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        }
      },
      "id": "w04-sync",
      "name": "Sync catalogue",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        40,
        0
      ],
      "notes": "One page fetch per product, so the timeout is 15 minutes rather than the usual 5. A 409 means a manual sync already holds the single-active slot: that is a skip, not a failure.",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "version": 2
          },
          "conditions": [
            {
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 200,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "w04-check",
      "name": "Succeeded?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        280,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "version": 2
          },
          "conditions": [
            {
              "leftValue": "={{ $json.body.created + $json.body.updated + $json.body.archived + $json.body.failed.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "w04-changed",
      "name": "Anything change?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        520,
        -100
      ],
      "notes": "A sync that found nothing new is the normal case. Reporting it every morning trains people to ignore the channel, so only real changes are announced."
    },
    {
      "parameters": {
        "chatId": "__TELEGRAM_CHAT_ID__",
        "text": "={{ $json.statusCode === 409 ? '\u23ed <b>Catalogue sync skipped</b>\\n\\nA sync was already running.\\nNothing was lost \u2014 the run in progress owns the single-active slot.' : '\ud83d\udd34 <b>Catalogue sync failed</b>\\n\\nHTTP ' + $json.statusCode + '\\n' + JSON.stringify($json.body).slice(0, 500) + '\\n\\nThe Truth Layer still holds the last good snapshot \u2014 nothing was deleted.' }}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "id": "w04-alert",
      "name": "Report skip or failure",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        520,
        100
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "__APP_URL__/api/public/automation/build-plates",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-automation-timestamp",
              "value": "={{ Date.now() }}"
            },
            {
              "name": "x-automation-nonce",
              "value": "={{ $execution.id }}-plates-{{ Date.now() }}"
            }
          ]
        },
        "options": {
          "timeout": 900000,
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        }
      },
      "id": "w04-plates",
      "name": "Rebuild plates",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        760,
        -260
      ],
      "notes": "Runs only when the sync actually changed something. Plates are derived from the catalogue's photographs, so a changed photograph leaves a stale plate - and a campaign rendered from a stale plate shows last week's product without anyone being told. 15 minute timeout: one page fetch per product, one file per finish.",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "__TELEGRAM_CHAT_ID__",
        "text": "=\ud83d\udce6 <b>Catalogue updated</b>\\n\\nScanned: {{ $('Sync catalogue').item.json.body.scanned }}\\nNew: {{ $('Sync catalogue').item.json.body.created }} \u00b7 Changed: {{ $('Sync catalogue').item.json.body.updated }} \u00b7 Unchanged: {{ $('Sync catalogue').item.json.body.unchanged }}\\nArchived: {{ $('Sync catalogue').item.json.body.archived }}\\n\\nClaims written: {{ $('Sync catalogue').item.json.body.claimsWritten }}\\nClaims retired: {{ $('Sync catalogue').item.json.body.claimsStale }}\\n\\n{{ $json.statusCode === 200 ? '\ud83c\udfa8 Plates rebuilt: ' + $json.body.plates + ' across ' + $json.body.products + ' products' : '\u26a0\ufe0f Plate rebuild failed (HTTP ' + $json.statusCode + ') - campaigns may render last week\\\\'s photographs until it is re-run' }}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "id": "w04-notify-2",
      "name": "Report changes and plates",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1000,
        -260
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "04:00 Cairo": {
      "main": [
        [
          {
            "node": "Sync catalogue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sync catalogue": {
      "main": [
        [
          {
            "node": "Succeeded?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Succeeded?": {
      "main": [
        [
          {
            "node": "Anything change?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Report skip or failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Anything change?": {
      "main": [
        [
          {
            "node": "Rebuild plates",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Rebuild plates": {
      "main": [
        [
          {
            "node": "Report changes and plates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "Africa/Cairo"
  }
}