{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "7d9139ff-f51e-4017-8831-5f8c6c273f51",
      "name": "Daily Inventory Check",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1860,
        500
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "50 8 * * *"
            }
          ]
        }
      },
      "notesInFlow": false,
      "typeVersion": 1.2
    },
    {
      "id": "bc8d384a-c5d8-4864-a157-cb21f3643d84",
      "name": "Check if Alerts Needed",
      "type": "n8n-nodes-base.if",
      "position": [
        -780,
        500
      ],
      "parameters": {
        "options": {
          "looseTypeValidation": true
        },
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "condition-001",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $node[\"Process Stock Data and Identify Low Stock\"].json[\"lowStockAlerts\"].length > 0 }}",
              "rightValue": 0
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "c42fabc3-2180-49e2-8ea5-d24500dbdea0",
      "name": "Send Inventory Alert",
      "type": "n8n-nodes-base.slack",
      "position": [
        -20,
        340
      ],
      "parameters": {
        "text": "={{ $json.slackMessage }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#magento-notifications"
        },
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "d0278e03-e2af-48c0-af7c-2be43a504398",
      "name": "Workflow Info",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1960,
        -140
      ],
      "parameters": {
        "width": 420,
        "height": 300,
        "content": "         \ud83d\udce6 MAGENTO 2 STOCK ALERTS\n**Author: Kanaka Kishore Kandregula **\n\nWORKFLOW PURPOSE:\nAutomatically monitor Magento 2 (Adobe Commerce) inventory levels and alert when products are running low\n\nSETUP REQUIREMENTS:\n- Magento 2 store with product inventory tracking\n- Slack & Gmail channels for alerts (#magento-notifications)\n- Define low stock threshold (default: 5 units)\n"
      },
      "typeVersion": 1
    },
    {
      "id": "f9c6eb47-2f17-4039-b1f0-48ea84ce8b09",
      "name": "Features & Config",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -600,
        -140
      ],
      "parameters": {
        "color": 4,
        "width": 400,
        "height": 300,
        "content": "                KEY FEATURES\n\n\u2705 MSI Support\n\u2705 Daily automated inventory checks\n\u2705 Customisable stock thresholds\n\u2705 Multiple SKU Reporting\n\u2705 Proactive restocking alerts\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "accd7ae8-f4d7-4bc3-8d99-2e9ecac7ece2",
      "name": "Business Value",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        -140
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 300,
        "content": "             BUSINESS VALUE\n\n\ud83d\udeab Prevent stockouts\n\ud83d\udcb0 Maintain sales revenue\n\ud83d\udcca Better inventory management\n\u23f0 Proactive restocking\n\nTIME TO BUILD: 20 minutes\nDIFFICULTY: Intermediate\nROI: High - prevents lost sales"
      },
      "typeVersion": 1
    },
    {
      "id": "f3651bdf-d90e-42f5-ba94-3bf4e4c55511",
      "name": "Get All Product Skus",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1420,
        320
      ],
      "parameters": {
        "url": "=https://magekwik.com/rest/V1/products?searchCriteria[pageSize]=0&searchCriteria[filterGroups][0][filters][0][field]=type_id&searchCriteria[filterGroups][0][filters][0][conditionType]=nin&searchCriteria[filterGroups][0][filters][0][value]=configurable,virtual,downloadable&fields=items[sku,extension_attributes[stock_item[manage_stock]]]",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth"
      },
      "typeVersion": 4.2
    },
    {
      "id": "c20eaa02-c1ce-4999-9aef-8c6a3fd959ee",
      "name": "Extract SKUs",
      "type": "n8n-nodes-base.code",
      "position": [
        -1180,
        320
      ],
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nconst skus = [];\nfor (const item of $input.first().json.items) {\n  skus.push(item.sku);\n}\nreturn [{ json: { skus: skus } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "a3fe994f-e782-4c55-8cdb-b8a256acdf37",
      "name": "Fetch MSI Stock Status",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1420,
        680
      ],
      "parameters": {
        "url": "=https://magekwik.com/rest/V1/inventory/source-items?searchCriteria[filterGroups][0][filters][0][field]=sku&searchCriteria[filterGroups][0][filters][0][conditionType]=in&searchCriteria[filterGroups][0][filters][0][value]={{ encodeURIComponent($node[\"Extract SKUs\"].json[\"skus\"].join(',')) }}",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth"
      },
      "typeVersion": 4.2
    },
    {
      "id": "2cf2065b-f235-4582-b439-4902a0097e10",
      "name": "Process Stock Data and Identify Low Stock",
      "type": "n8n-nodes-base.code",
      "position": [
        -1180,
        680
      ],
      "parameters": {
        "jsCode": "const lowStockItems = [];\nconst lowStockThreshold = 10; // Define your low stock threshold here\nconst minSourceQuantity = 5; // Define minimum quantity per source if applicable\n\n// Assuming the MSI stock data is in $json.items\nfor (const item of  $input.first().json.items) {\n  const sku = item.sku;\n  const sourceCode = item.source_code;\n  const quantity = item.quantity;\n  const status = item.status; // 1 for In Stock, 0 for Out of Stock\n\n  // Check if the item is in stock and below the overall low stock threshold\n  // Or if a specific source is very low\n  if (status === 1 && (quantity <= lowStockThreshold || quantity <= minSourceQuantity)) {\n    lowStockItems.push({\n      sku: sku,\n      source: sourceCode,\n      quantity: quantity,\n      status: status === 1 ? 'In Stock' : 'Out of Stock'\n    });\n  }\n}\n\n// You can further process lowStockItems to group by SKU or format as needed\n// For now, we'll return a list of individual low stock entries.\nreturn [{ json: { lowStockAlerts: lowStockItems } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "c92398cf-70f4-4d93-a273-693bc3c1bbd5",
      "name": "Send a message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -20,
        620
      ],
      "parameters": {
        "sendTo": "user@example.com",
        "message": "={{ $json.gmailHtml }}",
        "options": {
          "appendAttribution": false
        },
        "subject": "={{ $json.subjectLine }}"
      },
      "typeVersion": 2.1
    },
    {
      "id": "55ffa452-f2c5-43c2-996d-0f0fb7c9bed4",
      "name": "Format Message",
      "type": "n8n-nodes-base.code",
      "position": [
        -480,
        500
      ],
      "parameters": {
        "jsCode": "const lowStockAlerts = Array.isArray($input.first().json.lowStockAlerts) ? $input.first().json.lowStockAlerts : [];\n\nlet plainText = \"*\ud83d\udce6 Low Stock Alert for Magento 2 Multi-Store (MSI)*\\n\\n\";\nlet htmlText = \"<b>\ud83d\udce6 Low Stock Alert for Magento 2 Multi-Store (MSI)</b><br><br>\";\n\nif (lowStockAlerts.length === 0) {\n  plainText += \"_No low stock items found at this time._\";\n  htmlText += \"<i>No low stock items found at this time.</i>\";\n} else {\n  plainText += \"*The following products have low stock:*\\n\\n\";\n  htmlText += \"<b>The following products have low stock:</b><br><br>\";\n\n  // Text version\n  plainText += \"```\\nSKU       Source     Quantity  Status\\n\";\n  plainText += \"--------  ---------  --------  --------\\n\";\n\n  // HTML table version\n  htmlText += `<table border=\"1\" cellpadding=\"6\" cellspacing=\"0\" style=\"border-collapse: collapse;\">\n    <thead>\n      <tr>\n        <th align=\"left\">SKU</th>\n        <th align=\"left\">Source</th>\n        <th align=\"left\">Quantity</th>\n        <th align=\"left\">Status</th>\n      </tr>\n    </thead>\n    <tbody>\n  `;\n\n  lowStockAlerts.forEach(item => {\n    const sku = item.sku || 'N/A';\n    const source = item.source || 'N/A';\n    const quantity = item.quantity || 0;\n    const status = item.status || 'Unknown';\n\n    // Plain text table row\n    plainText += `${sku.padEnd(10)}${source.padEnd(11)}${String(quantity).padEnd(9)}${status}\\n`;\n\n    // HTML table row\n    htmlText += `<tr>\n      <td>${sku}</td>\n      <td>${source}</td>\n      <td>${quantity}</td>\n      <td>${status}</td>\n    </tr>`;\n  });\n\n  plainText += \"```\\n\\n\ud83d\udd0d Please check your Magento 2 inventory.\";\n  htmlText += \"</tbody></table><br><br>\ud83d\udd0d <b>Please check your Magento 2 inventory.</b>\";\n}\n\n// Output both formats\nreturn [{\n  json: {\n    slackMessage: plainText,\n    gmailHtml: htmlText,\n    subjectLine: `\ud83d\udd14 Magento 2 Low Stock Alert \u2013 ${new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}`\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "974a2ff6-768b-44b4-bf14-7b8534c99eac",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1500,
        -140
      ],
      "parameters": {
        "color": 5,
        "width": 420,
        "height": 300,
        "content": "                 WORK FLOW\n\n1. Schedule Trigger (daily 8:50 AM check)\n2. Magento 2 (get all products)\n3. Code Node (filter low stock items)\n4. If Node (check if any alerts needed)\n5. Format alert message\n6. Slack & Gmail (send inventory alert)"
      },
      "typeVersion": 1
    },
    {
      "id": "0d19935c-9078-435a-a940-49fdfef93bef",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        -140
      ],
      "parameters": {
        "color": 3,
        "width": 400,
        "height": 300,
        "content": "           CONFIGURATION NOTES\n\n- Adjust lowStockThreshold variable in Process Stock Data node\n- Modify daily schedule timing as needed\n- You Bearer YOUR_TOKEN_HERE from Magento\n- Slack & Gmail API details"
      },
      "typeVersion": 1
    },
    {
      "id": "cde3e885-dad8-4467-840a-52dbfd0997e1",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1940,
        400
      ],
      "parameters": {
        "color": 7,
        "width": 280,
        "height": 300,
        "content": "     Trigs Daily 8:50am"
      },
      "typeVersion": 1
    },
    {
      "id": "ca9ffce4-f84c-4da1-aa0c-5b50812fe8ff",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1520,
        220
      ],
      "parameters": {
        "color": 5,
        "width": 540,
        "height": 660,
        "content": "                      Magento Logic"
      },
      "typeVersion": 1
    },
    {
      "id": "a563ff82-2b81-468b-a33f-0780c9522a4c",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -120,
        220
      ],
      "parameters": {
        "color": 4,
        "width": 320,
        "height": 660,
        "content": "           Notifications"
      },
      "typeVersion": 1
    },
    {
      "id": "70c8982e-ea87-4d75-8a69-ee6ad174b4c1",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -820,
        400
      ],
      "parameters": {
        "color": 6,
        "width": 520,
        "height": 300,
        "content": "              Checks & Message Formatting"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Extract SKUs": {
      "main": [
        [
          {
            "node": "Fetch MSI Stock Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Message": {
      "main": [
        [
          {
            "node": "Send Inventory Alert",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get All Product Skus": {
      "main": [
        [
          {
            "node": "Extract SKUs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Inventory Check": {
      "main": [
        [
          {
            "node": "Get All Product Skus",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if Alerts Needed": {
      "main": [
        [
          {
            "node": "Format Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch MSI Stock Status": {
      "main": [
        [
          {
            "node": "Process Stock Data and Identify Low Stock",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Stock Data and Identify Low Stock": {
      "main": [
        [
          {
            "node": "Check if Alerts Needed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}