{
  "name": "MDataAccess: ECB release quote preview",
  "nodes": [
    {
      "parameters": {},
      "id": "665773fc-8448-4b1e-8551-99a986ffc100",
      "name": "Manual test",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 */6 * * *"
            }
          ]
        }
      },
      "id": "665773fc-8448-4b1e-8551-99a986ffc101",
      "name": "Every six hours",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        200
      ]
    },
    {
      "parameters": {
        "url": "https://www.ecb.europa.eu/rss/press.html",
        "options": {}
      },
      "id": "665773fc-8448-4b1e-8551-99a986ffc102",
      "name": "Read official ECB feed",
      "type": "n8n-nodes-base.rssFeedRead",
      "typeVersion": 1.2,
      "position": [
        220,
        100
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const all = $input.all();\nconst sourcePattern = /^https:\\/\\/www\\.ecb\\.europa\\.eu\\/{1,2}press\\/pr\\/date\\/20[0-9]{2}\\/html\\/ecb\\.[a-zA-Z0-9_~-]+\\.en\\.html$/;\nconst list = [];\nfor (const { json } of all) {\n  const url = typeof json.link === 'string' ? json.link.trim() : '';\n  const publishedMs = Date.parse(json.isoDate ?? json.pubDate ?? '');\n  if (!sourcePattern.test(url) || !Number.isFinite(publishedMs) || publishedMs > Date.now() + 300000) continue;\n  const title = typeof json.title === 'string' ? json.title.slice(0, 250) : '';\n  const canonicalUrl = url.replace('https://www.ecb.europa.eu//', 'https://www.ecb.europa.eu/');\n  list.push({ sourceUrl: canonicalUrl, publishedMs, title });\n}\nlist.sort((a, b) => b.publishedMs - a.publishedMs || (a.sourceUrl < b.sourceUrl ? 1 : a.sourceUrl > b.sourceUrl ? -1 : 0));\nconst latest = list[0];\nif (!latest) return [];\nconst production = $execution.mode === 'production' && $('Every six hours').isExecuted;\nif (production) {\n  const state = $getWorkflowStaticData('global');\n  const previous = state.ecbReleaseCheckpoint;\n  if (!previous) {\n    // First scheduled run records a baseline; do not quote old releases.\n    state.ecbReleaseCheckpoint = { publishedMs: latest.publishedMs, sourceUrl: latest.sourceUrl };\n    return [];\n  }\n  if (latest.publishedMs < previous.publishedMs ||\n      (latest.publishedMs === previous.publishedMs && latest.sourceUrl <= previous.sourceUrl)) return [];\n}\nreturn [{ json: latest }];"
      },
      "id": "665773fc-8448-4b1e-8551-99a986ffc103",
      "name": "Select new ECB release",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        100
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.mdataaccess.com/read",
        "authentication": "none",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "url",
              "value": "={{ $json.sourceUrl }}"
            }
          ]
        },
        "options": {
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          },
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "text"
            }
          },
          "timeout": 10000
        }
      },
      "id": "665773fc-8448-4b1e-8551-99a986ffc104",
      "name": "Get unsigned MDataAccess quote",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        100
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const response = $input.first().json;\nconst header = Object.entries(response.headers ?? {}).find(([key]) => key.toLowerCase() === 'payment-required');\nif (response.statusCode !== 402 || typeof header?.[1] !== 'string' || !header[1].trim()) {\n  throw new Error('Expected an unsigned HTTP 402 with PAYMENT-REQUIRED. No checkpoint was advanced.');\n}\nconst source = $('Select new ECB release').first().json;\nif ($execution.mode === 'production' && $('Every six hours').isExecuted) {\n  $getWorkflowStaticData('global').ecbReleaseCheckpoint = {\n    publishedMs: source.publishedMs,\n    sourceUrl: source.sourceUrl,\n  };\n}\nreturn [{ json: {\n  status: 'unpaid_quote_only',\n  sourceUrl: source.sourceUrl,\n  title: source.title,\n  publishedAt: new Date(source.publishedMs).toISOString(),\n  paymentRequired: header[1],\n  buyerGuide: 'https://mdataaccess.com/agents',\n} }];"
      },
      "id": "665773fc-8448-4b1e-8551-99a986ffc105",
      "name": "Report quote",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        100
      ]
    }
  ],
  "connections": {
    "Manual test": {
      "main": [
        [
          {
            "node": "Read official ECB feed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Every six hours": {
      "main": [
        [
          {
            "node": "Read official ECB feed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read official ECB feed": {
      "main": [
        [
          {
            "node": "Select new ECB release",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Select new ECB release": {
      "main": [
        [
          {
            "node": "Get unsigned MDataAccess quote",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get unsigned MDataAccess quote": {
      "main": [
        [
          {
            "node": "Report quote",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "UTC"
  },
  "pinData": {},
  "tags": []
}
