{
  "name": "00 - [FREE LITE] Shopify Order to Google Sheets (Safe Deduplicated)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "shopify-order-lite",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "node-webhook-lite",
      "name": "Shopify Order Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [220, 300]
    },
    {
      "parameters": {
        "jsCode": "const body = $input.first().json.body || $input.first().json;\nconst lineItems = Array.isArray(body.line_items) ? body.line_items : [];\nconst summary = lineItems.map(i => `${i.title || 'Item'} x${i.quantity || 1} (${i.price || '0'})`).join(' | ');\nconst orderId = String(body.id || '').trim();\n\nif (!orderId) {\n  throw new Error('Missing Shopify order ID in webhook payload');\n}\n\nreturn [{\n  json: {\n    order_id: orderId,\n    order_number: String(body.name || body.order_number || orderId).replace(/^#/, ''),\n    created_at: body.created_at || new Date().toISOString(),\n    customer_email: body.email || (body.customer && body.customer.email) || 'N/A',\n    currency: body.currency || 'USD',\n    total_price: Number(body.total_price || 0),\n    financial_status: body.financial_status || 'unknown',\n    line_items_summary: summary || 'No line items',\n    items_count: lineItems.length,\n    sync_source: 'webhook_lite',\n    synced_at: new Date().toISOString()\n  }\n}];"
      },
      "id": "node-normalize-lite",
      "name": "Normalize & Flatten Line Items",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [460, 300]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Orders_Ledger",
          "mode": "name"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "order_id",
              "lookupValue": "={{ $json.order_id }}"
            }
          ]
        },
        "options": {}
      },
      "id": "node-lookup-lite",
      "name": "Check Existing Order ID",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [700, 300],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "cond-not-exists",
              "leftValue": "={{ $json.order_id || '' }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "node-if-new-lite",
      "name": "Is New Order?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [940, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Orders_Ledger",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "order_id": "={{ $('Normalize & Flatten Line Items').item.json.order_id }}",
            "order_number": "={{ $('Normalize & Flatten Line Items').item.json.order_number }}",
            "created_at": "={{ $('Normalize & Flatten Line Items').item.json.created_at }}",
            "customer_email": "={{ $('Normalize & Flatten Line Items').item.json.customer_email }}",
            "currency": "={{ $('Normalize & Flatten Line Items').item.json.currency }}",
            "total_price": "={{ $('Normalize & Flatten Line Items').item.json.total_price }}",
            "financial_status": "={{ $('Normalize & Flatten Line Items').item.json.financial_status }}",
            "line_items_summary": "={{ $('Normalize & Flatten Line Items').item.json.line_items_summary }}",
            "items_count": "={{ $('Normalize & Flatten Line Items').item.json.items_count }}",
            "sync_source": "={{ $('Normalize & Flatten Line Items').item.json.sync_source }}",
            "synced_at": "={{ $('Normalize & Flatten Line Items').item.json.synced_at }}"
          }
        },
        "options": {}
      },
      "id": "node-append-lite",
      "name": "Append Order to Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [1180, 220]
    }
  ],
  "connections": {
    "Shopify Order Webhook": {
      "main": [[{ "node": "Normalize & Flatten Line Items", "type": "main", "index": 0 }]]
    },
    "Normalize & Flatten Line Items": {
      "main": [[{ "node": "Check Existing Order ID", "type": "main", "index": 0 }]]
    },
    "Check Existing Order ID": {
      "main": [[{ "node": "Is New Order?", "type": "main", "index": 0 }]]
    },
    "Is New Order?": {
      "main": [[{ "node": "Append Order to Sheet", "type": "main", "index": 0 }], []]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "kitVersion": "1.0.0-lite"
  }
}
