{
  "nodes": [
    {
      "name": "Form Trigger",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 1,
      "position": [
        200,
        300
      ],
      "parameters": {
        "formTitle": "Shopify CSV Order Import",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Channel Name",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Store_A"
                  },
                  {
                    "option": "Store_B"
                  }
                ]
              },
              "required": true
            },
            {
              "fieldLabel": "Orders CSV",
              "fieldType": "file",
              "required": true
            }
          ]
        }
      }
    },
    {
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        420,
        300
      ],
      "parameters": {
        "mode": "binaryToJson",
        "binaryPropertyName": "Orders CSV",
        "options": {
          "destinationKey": "data"
        }
      }
    },
    {
      "name": "Read CSV",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "parameters": {
        "operation": "read",
        "fileFormat": "csv",
        "binaryPropertyName": "data",
        "options": {
          "headerRow": true
        }
      }
    },
    {
      "name": "Get Shopify Channels",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        650,
        520
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "parameters": {
        "operation": "read",
        "sheetId": "YOUR_SHEET_ID",
        "range": "Shopify_Channels!A:D"
      }
    },
    {
      "name": "Match Channel",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        880,
        520
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.channel_name}}",
              "value2": "={{$node['Form Trigger'].json['Channel Name']}}"
            }
          ]
        }
      }
    },
    {
      "name": "Merge Order + Auth",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        1080,
        300
      ],
      "parameters": {
        "mode": "mergeByPosition"
      }
    },
    {
      "name": "Check Duplicate",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1280,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "=https://{{$json.store_url}}/admin/api/{{$json.api_version}}/orders.json?status=any&email={{$json['Email']}}",
        "authentication": "none",
        "headerParametersJson": "{ \"X-Shopify-Access-Token\": \"{{$json.admin_api_token}}\" }"
      }
    },
    {
      "name": "IF Duplicate Found",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1480,
        300
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json.orders.length}}",
              "operation": "larger",
              "value2": 0
            }
          ]
        }
      }
    },
    {
      "name": "Log Duplicate (Failed)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        1680,
        180
      ],
      "parameters": {
        "operation": "append",
        "sheetId": "YOUR_SHEET_ID",
        "range": "Shopify_Import_Failed!A:F",
        "valueInputMode": "RAW",
        "data": [
          [
            "={{new Date().toISOString()}}",
            "={{$node['Form Trigger'].json['Channel Name']}}",
            "={{$json['Email']}}",
            "={{$json['Lineitem sku']}}",
            "DUPLICATE_ORDER",
            "={{JSON.stringify($json)}}"
          ]
        ]
      }
    },
    {
      "name": "Rate Limit Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1680,
        420
      ],
      "parameters": {
        "amount": 500,
        "unit": "milliseconds"
      }
    },
    {
      "name": "Create Shopify Order",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1880,
        420
      ],
      "parameters": {
        "method": "POST",
        "url": "=https://{{$json.store_url}}/admin/api/{{$json.api_version}}/orders.json",
        "jsonParameters": true,
        "headerParametersJson": "{ \"X-Shopify-Access-Token\": \"{{$json.admin_api_token}}\" }",
        "bodyParametersJson": "{ \"order\": { \"email\": \"{{$json['Email']}}\", \"financial_status\": \"{{$json['Financial Status'].toLowerCase()}}\", \"line_items\": [{ \"title\": \"{{$json['Lineitem name']}}\", \"sku\": \"{{$json['Lineitem sku']}}\", \"quantity\": {{$json['Lineitem quantity']}}, \"price\": \"{{$json['Lineitem price']}}\" }], \"shipping_address\": { \"first_name\": \"{{$json['Shipping First Name']}}\", \"last_name\": \"{{$json['Shipping Last Name']}}\", \"address1\": \"{{$json['Shipping Address1']}}\", \"address2\": \"{{$json['Shipping Address2']}}\", \"city\": \"{{$json['Shipping City']}}\", \"province\": \"{{$json['Shipping Province']}}\", \"zip\": \"{{$json['Shipping Zip']}}\", \"country\": \"{{$json['Shipping Country']}}\", \"phone\": \"{{$json['Shipping Phone']}}\" }, \"tags\": \"CSV_IMPORT\" }}"
      }
    },
    {
      "name": "Log Success",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        2100,
        420
      ],
      "parameters": {
        "operation": "append",
        "sheetId": "YOUR_SHEET_ID",
        "range": "Shopify_Import_Success!A:G",
        "valueInputMode": "RAW",
        "data": [
          [
            "={{new Date().toISOString()}}",
            "={{$node['Form Trigger'].json['Channel Name']}}",
            "={{$json.order.email}}",
            "={{$json.order.line_items[0].sku}}",
            "={{$json.order.id}}",
            "={{$json.order.name}}",
            "SUCCESS"
          ]
        ]
      }
    }
  ],
  "connections": {
    "Form Trigger": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Read CSV",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read CSV": {
      "main": [
        [
          {
            "node": "Merge Order + Auth",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Shopify Channels": {
      "main": [
        [
          {
            "node": "Match Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Match Channel": {
      "main": [
        [
          {
            "node": "Merge Order + Auth",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Order + Auth": {
      "main": [
        [
          {
            "node": "Check Duplicate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Duplicate": {
      "main": [
        [
          {
            "node": "IF Duplicate Found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Duplicate Found": {
      "main": [
        [
          {
            "node": "Log Duplicate (Failed)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Rate Limit Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rate Limit Wait": {
      "main": [
        [
          {
            "node": "Create Shopify Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Shopify Order": {
      "main": [
        [
          {
            "node": "Log Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}