{
  "name": "Shopify Low-Stock Email Alert",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 1
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -420,
        -220
      ],
      "id": "932c5c08-59b6-4148-b56e-99b18e5576f3",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "url": "https://myshopify.com/admin/api/2023-04/products.json",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Shopify-Access-Token",
              "value": "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"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -240,
        -220
      ],
      "id": "7df5fcb9-b97c-4806-acae-87b7f8ccf1d2",
      "name": "Get Products"
    },
    {
      "parameters": {
        "jsCode": "const output = [];\n\nfor (const item of items) {\n  const data = item.json;\n\n  const products = data.products || [];\n\n  for (const product of products) {\n    const productName = product.title || 'Unknown Product';\n    const variants = product.variants || [];\n\n    for (const variant of variants) {\n      const inventoryQuantity = variant.inventory_quantity ?? 0;\n\n      if (inventoryQuantity < 5) {\n        output.push({\n          product_name: productName,\n          current_inventory: inventoryQuantity,\n        });\n      }\n    }\n  }\n}\n\n// Now convert output array to plain text\nlet text = 'Low Stock:\\n\\n';\nfor (const product of output) {\n  text += `- ${product.product_name}: ${product.current_inventory} in stock\\n`;\n}\n\nreturn [\n  {\n    json: {\n      email_body: text\n    }\n  }\n];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -60,
        -220
      ],
      "id": "c6d53cf2-d5fe-4458-959c-f9ebc5b0e169",
      "name": "Filter Products"
    },
    {
      "parameters": {
        "sendTo": "=team@forgentsystems.com",
        "subject": "=🚨 Low Stock Alert 🚨",
        "emailType": "text",
        "message": "=Dear ,\n\nWe would like to inform you that the following products are currently running low on stock:\n\n{{ $json.email_body }}\n\nWe kindly request that you take immediate action to restock these items to avoid any potential disruptions in availability.\n\nThank you for your attention to this matter. We appreciate your cooperation.\n\nBest regards,",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        140,
        -220
      ],
      "id": "e4f3a745-6c12-4498-b78f-df5fb7b5e0a5",
      "name": "Send Notification Email",
      "webhookId": "1222ff58-fdba-4a21-bba8-a5765c0c34c4",
      "credentials": {
        "gmailOAuth2": {
          "id": "GXe14RV89PrT2yZB",
          "name": "Gmail account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Products",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Products": {
      "main": [
        [
          {
            "node": "Filter Products",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Products": {
      "main": [
        [
          {
            "node": "Send Notification Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Notification Email": {
      "main": [
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "310e1022-f509-4317-b4ff-df5953d307b3",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "GBfzqFLqwXFjmQYr"
}