{
  "name": "VNTANA Showroom Bulk Tool",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "vntana-showroom-bulk",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [
            {
              "id": "orders-check",
              "leftValue": "={{ $json.body.orders.length }}",
              "rightValue": 0,
              "operator": { "type": "number", "operation": "gt" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-has-orders",
      "name": "Has Orders?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [460, 200]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [
            {
              "id": "styling-check",
              "leftValue": "={{ $json.body.styling.length }}",
              "rightValue": 0,
              "operator": { "type": "number", "operation": "gt" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-has-styling",
      "name": "Has Styling?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [460, 400]
    },
    {
      "parameters": {
        "fieldToSplitOut": "body.orders",
        "options": {}
      },
      "id": "split-orders",
      "name": "Split Orders",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [680, 120]
    },
    {
      "parameters": {
        "fieldToSplitOut": "body.styling",
        "options": {}
      },
      "id": "split-styling",
      "name": "Split Styling",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [680, 400]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api-platform.vntana.com/v1/showrooms/sharelinks/order-item",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "vntanaApi",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            { "name": "shareLinkUuid", "value": "={{ $json['Share Link UUID'] }}" },
            { "name": "assetUuid",     "value": "={{ $json['Asset UUID'] }}" },
            { "name": "quantity",      "value": "={{ $json['Quantity'] }}" }
          ]
        },
        "options": {}
      },
      "id": "post-order",
      "name": "POST Order Item",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [900, 120]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api-platform.vntana.com/v2/showrooms/get-by-uuid/{{ $json['Showroom UUID'] }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "vntanaApi",
        "options": {}
      },
      "id": "get-showroom",
      "name": "GET Showroom",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [900, 400]
    },
    {
      "parameters": {
        "jsCode": "const row     = $('Split Styling').item.json;\nconst current = $input.item.json.payload;\n\nconst layout = { ...current.layoutAttributes };\n\nif (row['Font Family'])             layout.FONT_FAMILY        = row['Font Family'];\nif (row['Background Color (hex)'])  layout.BACKGROUND_COLOR   = row['Background Color (hex)'];\nif (row['Text Color (hex)'])        layout.TEXT_COLOR         = row['Text Color (hex)'];\nif (row['Divider Color (hex)'])     layout.DIVIDER_COLOR      = row['Divider Color (hex)'];\nif (row['Products Per Row (1-5)'])  layout.PRODUCTS_PER_ROW   = parseInt(row['Products Per Row (1-5)'], 10);\nif (row['Image Style'])             layout.IMAGE_STYLE        = row['Image Style'].toUpperCase();\n\nreturn [{ json: { ...current, layoutAttributes: layout, showroomUuid: current.uuid } }];"
      },
      "id": "merge-layout",
      "name": "Merge Layout",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1120, 400]
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://api-platform.vntana.com/v1/showrooms/{{ $json.showroomUuid }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "vntanaApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "put-showroom",
      "name": "PUT Showroom",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1340, 400]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "id": "agg-orders",
      "name": "Aggregate Orders",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [1120, 120]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "id": "agg-styling",
      "name": "Aggregate Styling",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [1560, 400]
    },
    {
      "parameters": {
        "jsCode": "const ordersData  = $('Aggregate Orders').first()?.json?.data  ?? [];\nconst stylingData = $('Aggregate Styling').first()?.json?.data ?? [];\n\nconst ordersProcessed  = ordersData.filter(r => r.status === 'success' || r.payload).length;\nconst stylingProcessed = stylingData.filter(r => r.status === 'success' || r.payload).length;\n\nreturn [{ json: { ordersProcessed, stylingProcessed, errors: [] } }];"
      },
      "id": "build-response",
      "name": "Build Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1340, 260]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": { "responseCode": 200 }
      },
      "id": "respond",
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [1560, 260]
    }
  ],
  "connections": {
    "Webhook":          { "main": [[{ "node": "Has Orders?",  "type": "main", "index": 0 }, { "node": "Has Styling?", "type": "main", "index": 0 }]] },
    "Has Orders?":      { "main": [[{ "node": "Split Orders", "type": "main", "index": 0 }], []] },
    "Has Styling?":     { "main": [[{ "node": "Split Styling","type": "main", "index": 0 }], []] },
    "Split Orders":     { "main": [[{ "node": "POST Order Item","type": "main", "index": 0 }]] },
    "POST Order Item":  { "main": [[{ "node": "Aggregate Orders","type": "main", "index": 0 }]] },
    "Split Styling":    { "main": [[{ "node": "GET Showroom", "type": "main", "index": 0 }]] },
    "GET Showroom":     { "main": [[{ "node": "Merge Layout", "type": "main", "index": 0 }]] },
    "Merge Layout":     { "main": [[{ "node": "PUT Showroom", "type": "main", "index": 0 }]] },
    "PUT Showroom":     { "main": [[{ "node": "Aggregate Styling","type": "main", "index": 0 }]] },
    "Aggregate Orders": { "main": [[{ "node": "Build Response","type": "main", "index": 0 }]] },
    "Aggregate Styling":{ "main": [[{ "node": "Build Response","type": "main", "index": 0 }]] },
    "Build Response":   { "main": [[{ "node": "Respond",     "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "staticData": null,
  "meta": { "templateCredsSetupCompleted": true }
}
