{
  "name": "order-export-digest",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 7 * * 1"
            }
          ]
        }
      },
      "id": "weekly-trigger",
      "name": "Weekly Trigger (Mon 07:00)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.SHOP_API_BASE_URL }}/api/v1/orders/export",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpHeaderAuth",
        "options": {
          "timeout": 30000
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "days",
              "value": "7"
            },
            {
              "name": "format",
              "value": "json"
            }
          ]
        }
      },
      "id": "fetch-orders",
      "name": "Fetch Order Export",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        440,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst orders = data.orders || [];\n\nconst bySku = {};\nlet total = 0;\nfor (const order of orders) {\n  for (const line of order.line_items || []) {\n    const sku = line.sku || 'unknown';\n    bySku[sku] = (bySku[sku] || 0) + (line.quantity || 0);\n    total += line.quantity || 0;\n  }\n}\n\nconst rows = Object.entries(bySku)\n  .sort((a, b) => b[1] - a[1])\n  .map(([sku, qty]) => `- ${sku}: ${qty} units`);\n\nconst lines = [];\nlines.push(`## Order Digest - Week of ${new Date().toISOString().split('T')[0]}`);\nlines.push('');\nlines.push(`Total units across ${orders.length} orders: ${total}`);\nlines.push('');\nlines.push('### Units by SKU');\nlines.push(...rows);\nlines.push('');\nlines.push('_Digest generated by the order-export-digest flow._');\n\nreturn [{ json: { digest: lines.join('\\n'), date: new Date().toISOString() } }];"
      },
      "id": "summarize-by-sku",
      "name": "Summarize Counts by SKU",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "fromEmail": "={{ $env.DIGEST_FROM_EMAIL }}",
        "toEmail": "={{ $env.DIGEST_TO_EMAIL }}",
        "subject": "=Emberline weekly order digest {{ $json.date.split('T')[0] }}",
        "text": "={{ $json.digest }}",
        "options": {}
      },
      "id": "send-digest-email",
      "name": "Email Digest",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        840,
        300
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Weekly Trigger (Mon 07:00)": {
      "main": [
        [
          {
            "node": "Fetch Order Export",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Order Export": {
      "main": [
        [
          {
            "node": "Summarize Counts by SKU",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize Counts by SKU": {
      "main": [
        [
          {
            "node": "Email Digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateId": "order-export-digest"
  },
  "tags": [
    "emberline-studio",
    "orders",
    "weekly"
  ]
}