{
  "name": "Regiondo Polling Sync",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "seconds",
              "secondsInterval": 15
            }
          ]
        }
      },
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "operation": "getAll",
        "projectId": "mozarthaus-6cf51",
        "collection": "apps/mozarthaus_new_buchungssystem_mozarthaus_v1/actions",
        "returnAll": true,
        "filters": {
          "conditions": [
            {
              "field": "status",
              "operator": "EQUAL",
              "value": "pending"
            },
            {
              "field": "action_type",
              "operator": "EQUAL",
              "value": "sync_regiondo_events"
            }
          ]
        }
      },
      "name": "Fetch Pending Sync",
      "type": "n8n-nodes-base.googleFirebaseCloudFirestore",
      "typeVersion": 1,
      "position": [
        200,
        300
      ],
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const crypto = require('crypto');\n\nconst publicKey = 'MO43f21d011626';\nconst privateKey = '43f21d0f4ba8f05b111626d551fd8509160d0';\nconst timestamp = Math.floor(Date.now() / 1000).toString();\n\nconst d1 = new Date();\nconst dt_from = d1.toISOString().split('T')[0];\nconst d2 = new Date();\nd2.setMonth(d2.getMonth() + 6);\nconst dt_to = d2.toISOString().split('T')[0];\n\nconst queryString = `dt_from=${dt_from}&dt_to=${dt_to}`;\nconst stringToHash = timestamp + publicKey + queryString;\nconst hash = crypto.createHmac('sha256', privateKey).update(stringToHash).digest('hex');\n\nreturn {\n  json: {\n    ...$input.item.json,\n    'X-API-ID': publicKey,\n    'X-API-TIME': timestamp,\n    'X-API-HASH': hash,\n    'dt_from': dt_from,\n    'dt_to': dt_to\n  }\n};"
      },
      "name": "Generate Regiondo Auth HMAC",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.regiondo.com/v1/products/availabilities/31903",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-ID",
              "value": "={{$json[\"X-API-ID\"]}}"
            },
            {
              "name": "X-API-TIME",
              "value": "={{$json[\"X-API-TIME\"]}}"
            },
            {
              "name": "X-API-HASH",
              "value": "={{$json[\"X-API-HASH\"]}}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "dt_from",
              "value": "={{$json[\"dt_from\"]}}"
            },
            {
              "name": "dt_to",
              "value": "={{$json[\"dt_to\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Regiondo",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.item.json.data || {};\nconst mappedEvents = [];\n\nfor (const [date, timesArray] of Object.entries(data)) {\n  for (const timeObj of timesArray) {\n    const timeString = String(Array.isArray(timeObj) ? timeObj[0] : timeObj);\n    \n    const eventId = `event_${date.replace(/-/g, '')}_${timeString.replace(/:/g, '')}`;\n    \n    mappedEvents.push({\n      json: {\n        id: eventId,\n        date: date,\n        time: timeString.substring(0, 5),\n        title: 'Mozart Ensemble',\n        status: 'active',\n        updatedAt: new Date().toISOString()\n      }\n    });\n  }\n}\n\nreturn mappedEvents;"
      },
      "name": "Data Mapping",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "operation": "upsert",
        "projectId": "mozarthaus-6cf51",
        "collection": "apps/mozarthaus_new_buchungssystem_mozarthaus_v1/events",
        "documentId": "={{$json.id}}",
        "columns": "date,time,title,status,updatedAt"
      },
      "name": "Firestore Upsert",
      "type": "n8n-nodes-base.googleFirebaseCloudFirestore",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ],
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "projectId": "mozarthaus-6cf51",
        "collection": "apps/mozarthaus_new_buchungssystem_mozarthaus_v1/actions",
        "documentId": "={{ $('Fetch Pending Sync').first().json.id }}",
        "columns": "status,result",
        "dataToUpdate": "={\n  \"status\": \"completed\",\n  \"result\": \"success\"\n}"
      },
      "name": "Mark Completed",
      "type": "n8n-nodes-base.googleFirebaseCloudFirestore",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ],
      "executeOnce": true,
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Fetch Pending Sync",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Pending Sync": {
      "main": [
        [
          {
            "node": "Generate Regiondo Auth HMAC",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Regiondo Auth HMAC": {
      "main": [
        [
          {
            "node": "HTTP Regiondo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Regiondo": {
      "main": [
        [
          {
            "node": "Data Mapping",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Data Mapping": {
      "main": [
        [
          {
            "node": "Firestore Upsert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Firestore Upsert": {
      "main": [
        [
          {
            "node": "Mark Completed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false
}