{
  "id": "TXNuuI0iCYthPWX9",
  "name": "Low-Stock Alerts to Slack",
  "tags": [],
  "nodes": [
    {
      "id": "c6f72eec-9aae-49c6-9abd-eb8cd04358e9",
      "name": "Format Low Stock Message",
      "type": "n8n-nodes-base.code",
      "position": [
        -192,
        1296
      ],
      "parameters": {
        "jsCode": "const inputData = $input.first().json.low_stock;\n\n// Check if there are any low stock items\nif (!inputData || inputData.length === 0) {\n  return [];\n}\n\n// Function to generate the output JSON structure\nconst generateOutputJson = (inputData) => {\n  const elements = inputData.map(item => {\n    return [\n      {\n        \"type\": \"emoji\",\n        \"name\": \"receipt\",\n        \"unicode\": \"1f9fe\"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \" \"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \"Product : \",\n        \"style\": {\n          \"bold\": true\n        }\n      },\n      {\n        \"type\": \"text\",\n        \"text\": item.product + \"\\n\"\n      },\n      {\n        \"type\": \"emoji\",\n        \"name\": \"package\",\n        \"unicode\": \"1f4e6\"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \" \"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \"Current Stock : \",\n        \"style\": {\n          \"bold\": true\n        }\n      },\n      {\n        \"type\": \"text\",\n        \"text\": item.current_stock === null ? \"Out of Stock\" : item.current_stock.toString() + \"\\n\"\n      },\n      {\n        \"type\": \"emoji\",\n        \"name\": \"label\",\n        \"unicode\": \"1f3f7-fe0f\"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \" \"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \"Category : \",\n        \"style\": {\n          \"bold\": true\n        }\n      },\n      {\n        \"type\": \"text\",\n        \"text\": item.Category + \"\\n\\n\\n\\n\"\n      }\n    ];\n  });\n\n  return [\n    {\n      \"text\": \"Daily low stock alert\",\n      \"blocks\":[\n        {\n      \"type\": \"rich_text\",\n      \"elements\": [\n        {\n          \"type\": \"rich_text_section\",\n          \"elements\": [\n            {\n            \"type\": \"emoji\",\n            \"name\": \"red_circle\",\n            \"unicode\": \"1f534\",\n            \"style\": {\n              \"bold\": true\n            }\n          },\n          {\n            \"type\": \"text\",\n            \"text\": \" Daily Low Stock Alert Messages :\",\n            \"style\": {\n              \"bold\": true\n            }\n          },\n          {\n            \"type\": \"text\",\n            \"text\": \"\\n\\nThe following products are running low on inventory and may require restocking:\\n\\n\"\n          },\n            ...elements.flat(),\n          {\n            \"type\": \"emoji\",\n            \"name\": \"warning\",\n            \"unicode\": \"26a0-fe0f\"\n          },\n          {\n            \"type\": \"text\",\n            \"text\": \" Please review these items and take action to avoid stockouts.\"\n          }\n          ]\n        }\n      ]\n    }\n      ]\n    }\n  ];\n};\n\n// Generate the output JSON\nconst outputJson = generateOutputJson(inputData);\n\nreturn outputJson;"
      },
      "typeVersion": 2
    },
    {
      "id": "edabdcc0-e9c2-489f-bc4f-14c7129caefa",
      "name": "Format Urgent Low Stock Message",
      "type": "n8n-nodes-base.code",
      "position": [
        192,
        1872
      ],
      "parameters": {
        "jsCode": "const inputData = $('Separate products to low stock and very low stock').first().json.uregent_low_stock;\n\n// Check if there are any urgent low stock items\nif (!inputData || inputData.length === 0) {\n  return [];\n}\n\n// Function to generate the output JSON structure\nconst generateOutputJson = (inputData) => {\n  const elements = inputData.map(item => {\n    return [\n      {\n        \"type\": \"emoji\",\n        \"name\": \"receipt\",\n        \"unicode\": \"1f9fe\"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \" \"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \"Product : \",\n        \"style\": {\n          \"bold\": true\n        }\n      },\n      {\n        \"type\": \"text\",\n        \"text\": item.product + \"\\n\"\n      },\n      {\n        \"type\": \"emoji\",\n        \"name\": \"package\",\n        \"unicode\": \"1f4e6\"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \" \"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \"Current Stock : \",\n        \"style\": {\n          \"bold\": true\n        }\n      },\n      {\n        \"type\": \"text\",\n        \"text\": item.current_stock === null ? \"Out of Stock\" : item.current_stock.toString() + \"\\n\"\n      },\n      {\n        \"type\": \"emoji\",\n        \"name\": \"label\",\n        \"unicode\": \"1f3f7-fe0f\"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \" \"\n      },\n      {\n        \"type\": \"text\",\n        \"text\": \"Category : \",\n        \"style\": {\n          \"bold\": true\n        }\n      },\n      {\n        \"type\": \"text\",\n        \"text\": item.Category + \"\\n\\n\\n\\n\"\n      }\n    ];\n  });\n\n  return [\n    {\n      \"text\": \"Daily urgent low stock alert\",\n      \"blocks\":[\n        {\n      \"type\": \"rich_text\",\n      \"elements\": [\n        {\n          \"type\": \"rich_text_section\",\n          \"elements\": [\n            {\n            \"type\": \"emoji\",\n            \"name\": \"red_circle\",\n            \"unicode\": \"1f534\",\n            \"style\": {\n              \"bold\": true\n            }\n          },\n          {\n            \"type\": \"text\",\n            \"text\": \" Daily Very Low Stock Alert Messages :\",\n            \"style\": {\n              \"bold\": true\n            }\n          },\n          {\n            \"type\": \"text\",\n            \"text\": \"\\n\\nThe following products are running very low on inventory and may require restocking:\\n\\n\"\n          },\n            ...elements.flat(),\n          {\n            \"type\": \"emoji\",\n            \"name\": \"warning\",\n            \"unicode\": \"26a0-fe0f\"\n          },\n          {\n            \"type\": \"text\",\n            \"text\": \" Please review the Jira ticket created and take action to avoid stockouts.\" + '\\n\\n'\n          },\n          {\n            \"type\": \"text\",\n            \"text\": \" Jira ticket id : \" + $input.first().json.id + '\\n\\n' + \"Jira ticket key : \" + $input.first().json.key,\n            \"style\": {\n              \"bold\": true\n            }\n          }\n          ]\n        }\n      ]\n    }\n      ]\n    }\n  ];\n};\n\n// Generate the output JSON\nconst outputJson = generateOutputJson(inputData);\n\nreturn outputJson;"
      },
      "typeVersion": 2
    },
    {
      "id": "5ad562cc-fe26-454c-9569-461a5b258c63",
      "name": "Get many products from WooCommerce",
      "type": "n8n-nodes-base.wooCommerce",
      "position": [
        -784,
        1568
      ],
      "parameters": {
        "options": {},
        "operation": "getAll"
      },
      "credentials": {
        "wooCommerceApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "bb41f575-f1c0-4d84-9294-9abe592df4f8",
      "name": "Separate products to low stock and very low stock",
      "type": "n8n-nodes-base.code",
      "position": [
        -576,
        1568
      ],
      "parameters": {
        "jsCode": "// Get all products\nconst products = $input.all();\n\n// Initialize arrays\nconst low_stock = [];\nconst urgent_low_stock = [];\n\n// Define thresholds\nconst thresholds = {\n  \"headphones\": 10,\n  \"gaming-laptops\": 7,\n  \"laptops\": 10,\n  \"fashion\": 15,\n  \"mobile-phones\": 10,\n  \"monitors\": 8,\n  \"default\": 7\n};\n\nconst urgentThresholds = {\n  \"headphones\": 5,\n  \"gaming-laptops\": 3,\n  \"laptops\": 4,\n  \"fashion\": 8,\n  \"mobile-phones\": 5,\n  \"monitors\": 4,\n  \"default\": 3\n};\n\n// Process each product\nfor (const item of products) {\n  const product = item.json;\n  \n  // Skip if stock is not managed or stock quantity is null\n  if (!product.manage_stock || product.stock_quantity === null) {\n    continue;\n  }\n  \n  const categories = product.categories;\n  \n  // Determine category\n  let category = \"default\";\n  if (Array.isArray(categories) && categories.length === 1) {\n    category = categories[0].slug.toLowerCase();\n  }\n  \n  // Get thresholds\n  const threshold = thresholds[category] || thresholds[\"default\"];\n  const urgentThreshold = urgentThresholds[category] || urgentThresholds[\"default\"];\n  \n  // Create product object\n  const productData = {\n    \"product\": product.name,\n    \"current_stock\": product.stock_quantity,\n    \"Category\": product.categories.map(cat => cat.name).join(', ')\n  };\n  \n  // Check stock levels and add to appropriate array\n  if (product.stock_quantity <= urgentThreshold) {\n    urgent_low_stock.push(productData);\n  } else if (product.stock_quantity <= threshold) {\n    low_stock.push(productData);\n  }\n}\n\n// Return the accumulated results\nreturn {\n  json: {\n    low_stock: low_stock,\n    uregent_low_stock: urgent_low_stock\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "63f53062-3a39-484d-bc4e-9400f8be31f9",
      "name": "Format Urgent Low Stock Message for Jira",
      "type": "n8n-nodes-base.code",
      "position": [
        -208,
        1872
      ],
      "parameters": {
        "jsCode": "// Extracting the urgent low stock items\nconst urgentLowStock = $input.first().json.uregent_low_stock;\n\n// Building the message\nlet message = \"Below are the list of products whose stock is very low and we need to refill it as soon as possible,\\n\\n\";\n\nurgentLowStock.forEach((item, index) => {\n  message += `${index + 1}. ${item.product} -> current stock: ${item.current_stock}\\n`;\n});\n\nmessage += \"\\nPlease take action to avoid stockouts.\";\n\n// Output the message\nreturn { description : message };\n"
      },
      "typeVersion": 2
    },
    {
      "id": "e63258d7-1ea0-4b67-bea3-7dae80ff85ac",
      "name": "Create an Issue in Jira",
      "type": "n8n-nodes-base.jira",
      "position": [
        -16,
        1872
      ],
      "parameters": {
        "project": {
          "__rl": true,
          "mode": "list",
          "value": "10000",
          "cachedResultName": "My Software Team"
        },
        "summary": "=Urgent Low Stock - {{ $now.format('yyyy-MM-dd') }}",
        "issueType": {
          "__rl": true,
          "mode": "list",
          "value": "10007",
          "cachedResultName": "Bug"
        },
        "additionalFields": {
          "assignee": {
            "__rl": true,
            "mode": "list",
            "value": "712020:35d1b5a5-a39d-48db-bf55-466b59246de9",
            "cachedResultName": "Fullstacktech wli"
          },
          "priority": {
            "__rl": true,
            "mode": "list",
            "value": "2",
            "cachedResultName": "High"
          },
          "description": "={{ $json.description }}\n"
        }
      },
      "credentials": {
        "jiraSoftwareCloudApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0af43599-f69e-41ab-95a9-8672e6cbb473",
      "name": "Send Urgent Low Stock Alert to Slack",
      "type": "n8n-nodes-base.slack",
      "position": [
        384,
        1872
      ],
      "parameters": {
        "select": "channel",
        "blocksUi": "={{ $json }}",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C0A57J97MCK",
          "cachedResultName": "daily_urgent_low_stock_update"
        },
        "messageType": "block",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": false,
      "retryOnFail": true,
      "typeVersion": 2.3,
      "alwaysOutputData": false
    },
    {
      "id": "58c3b42b-cc26-47be-a6bc-fdacafafd7e4",
      "name": "Send Low Stock Alert to Slack",
      "type": "n8n-nodes-base.slack",
      "position": [
        0,
        1296
      ],
      "parameters": {
        "select": "channel",
        "blocksUi": "={{ $json }}",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C0A5DV7FCH0",
          "cachedResultName": "daily_low_stock_update"
        },
        "messageType": "block",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 2.3
    },
    {
      "id": "cdd6a207-7719-467a-940c-d9169c5db387",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1856,
        848
      ],
      "parameters": {
        "width": 624,
        "height": 1344,
        "content": "## How It Works\n\nThis workflow automatically monitors product inventory levels in WooCommerce on a scheduled basis and sends alerts when stock falls below defined thresholds.\n\n-> Every Monday at midnight, the workflow:\n\n1. Fetches all products from WooCommerce.\n\n2. Evaluates each product\u2019s stock quantity based on category-specific thresholds.\n\n3. Separates products into:\n     - Low stock\n     - Very low (urgent) stock\n\n4. Sends:\n     - A low stock alert to a dedicated Slack channel.\n     - A very low stock alert that:\n          - Creates a Jira issue\n          - Sends a Slack alert including the Jira ticket details.\n\n\nThis ensures teams are proactively notified and urgent stock shortages are tracked and resolved quickly.\n\n\n\n\n\n## Setup Steps\n\n1. Configure Schedule Trigger\n   - Set the Schedule Trigger to run every Monday at midnight.\n\n2. Connect WooCommerce\n   - Add WooCommerce credentials.\n   - Use the Get many products operation to fetch all products.\n\n3. Define Stock Thresholds\n   - In the \u201cSeparate products to low stock and very low stock\u201d Code node:\n       - Set category-wise thresholds for:\n           - Low stock\n           - Urgent (very low) stock\n\n4. Configure Slack Integration\n   - Connect your Slack workspace.\n   - Select:\n     - One channel for low stock alerts\n     - One channel for urgent low stock alerts\n\n5. Configure Jira Integration\n   - Connect Jira Software Cloud credentials.\n   - Select:\n     - Project\n     - Issue type\n     - Assignee\n     - Priority\n\n6. Activate Workflow\n   - Once activated, alerts and Jira issues will be generated automatically every week."
      },
      "typeVersion": 1
    },
    {
      "id": "a08c3dfc-25d0-4dc6-8551-69d202f9b41a",
      "name": "Inventory Check Scheduler",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1120,
        1568
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ]
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "5e5c7e59-b8a0-47dd-a9f5-b4318fea17d8",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1184,
        1472
      ],
      "parameters": {
        "color": 7,
        "height": 272,
        "content": "Runs the workflow automatically every Monday at midnight without manual intervention."
      },
      "typeVersion": 1
    },
    {
      "id": "dc8b14cf-a874-4eec-9457-1d6787b576ff",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -896,
        1312
      ],
      "parameters": {
        "color": 7,
        "width": 512,
        "height": 432,
        "content": "This part fetches all products from WooCommerce and analyzes their inventory levels.\n\nOnly products with managed stock are considered.\nEach product\u2019s stock quantity is compared against category-wise thresholds to determine whether it falls under:\n - Low stock\n - Very low (urgent) stock\n\n\nThe products are then separated into two arrays for further processing."
      },
      "typeVersion": 1
    },
    {
      "id": "d22ca891-b33e-4aab-8b0a-930a298c56c4",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -288,
        1056
      ],
      "parameters": {
        "color": 7,
        "width": 560,
        "height": 416,
        "content": "This section prepares a formatted Slack message listing all products with low stock levels.\n\nThe alert includes:\n - Product name\n - Current stock\n - Category\n\n\nThe message is sent to the low stock Slack channel to help the team monitor inventory before it becomes critical."
      },
      "typeVersion": 1
    },
    {
      "id": "20930bc8-f1b7-4894-95eb-311db10ee411",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -288,
        1568
      ],
      "parameters": {
        "color": 7,
        "width": 832,
        "height": 480,
        "content": "This part handles products with very low (urgent) stock.\n\nFirst, a Jira issue is created with:\n - A clear summary\n - High priority\n - Detailed product list in the description\n\nAfter the Jira issue is created, a Slack alert is sent to the urgent stock channel, including:\n - Urgent low stock product details\n - Jira ticket ID\n - Jira ticket key\n\n\nThis ensures critical inventory issues are both tracked and immediately visible."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "d00ca3df-d3d0-4754-8209-5862f8ac1e92",
  "connections": {
    "Create an Issue in Jira": {
      "main": [
        [
          {
            "node": "Format Urgent Low Stock Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Low Stock Message": {
      "main": [
        [
          {
            "node": "Send Low Stock Alert to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inventory Check Scheduler": {
      "main": [
        [
          {
            "node": "Get many products from WooCommerce",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Urgent Low Stock Message": {
      "main": [
        [
          {
            "node": "Send Urgent Low Stock Alert to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many products from WooCommerce": {
      "main": [
        [
          {
            "node": "Separate products to low stock and very low stock",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Urgent Low Stock Message for Jira": {
      "main": [
        [
          {
            "node": "Create an Issue in Jira",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Separate products to low stock and very low stock": {
      "main": [
        [
          {
            "node": "Format Low Stock Message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Format Urgent Low Stock Message for Jira",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}