{
  "name": "VNTANA Hotspot Translations",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "vntana-translations",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api-platform.vntana.com/v1/products/{{ $json.body.productUuid }}/organizations/{{ $json.body.org }}/clients/{{ $json.body.workspace }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "vntanaApi",
        "options": {}
      },
      "credentials": {
        "vntanaApi": {
          "name": "VNTANA account 3"
        }
      },
      "id": "get-product",
      "name": "Get Product",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 300]
    },
    {
      "parameters": {
        "language": "javaScript",
        "jsCode": "// Product comes from platform API — use its shape directly for the PUT\nconst raw          = $('Get Product').item.json;\nconst translations = $('Webhook').item.json.body.translations;\nconst ATTR_KEY     = 'hotspot_translations';\n\n// The platform API may return the product at root or under .response\nconst product = raw.response ?? raw;\n\n// Upsert the hotspot_translations attribute\nconst existingAttrs = Array.isArray(product.attributes) ? product.attributes : [];\nconst attrs = existingAttrs.filter(a => (a.name ?? a.key) !== ATTR_KEY);\nattrs.push({ name: ATTR_KEY, value: JSON.stringify(translations) });\n\n// Build PUT body using whatever the platform API returned, with attributes replaced\nconst putBody = { ...product, attributes: attrs };\n\nreturn [{ json: { translations, putBody } }];"
      },
      "id": "merge",
      "name": "Merge Attributes",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "https://api-platform.vntana.com/v1/products",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "vntanaApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.putBody) }}",
        "options": {}
      },
      "credentials": {
        "vntanaApi": {
          "name": "VNTANA account 3"
        }
      },
      "id": "put-product",
      "name": "Write to VNTANA",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [900, 300]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($('Write to VNTANA').item.json.success === false ? { ok: false, error: $('Write to VNTANA').item.json.errors } : { ok: true, locales: Object.keys($('Merge Attributes').item.json.translations) }) }}",
        "options": {}
      },
      "id": "respond",
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1120, 300]
    }
  ],
  "connections": {
    "Webhook":          { "main": [[{ "node": "Get Product",      "type": "main", "index": 0 }]] },
    "Get Product":      { "main": [[{ "node": "Merge Attributes", "type": "main", "index": 0 }]] },
    "Merge Attributes": { "main": [[{ "node": "Write to VNTANA", "type": "main", "index": 0 }]] },
    "Write to VNTANA":  { "main": [[{ "node": "Respond",          "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
