{
  "id": "kwCv7lUv3ddhMm2n",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Auto-Monitor Construction Stock & Get Low Inventory Alerts",
  "tags": [],
  "nodes": [
    {
      "id": "c6572d25-217a-4703-9405-33bf85124b20",
      "name": "Daily Stock Check",
      "type": "n8n-nodes-base.cron",
      "position": [
        -1000,
        440
      ],
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "hour": 8
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "176bd950-8573-4757-9aba-00265646233e",
      "name": "Fetch Stock Data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -780,
        440
      ],
      "parameters": {
        "range": "StockInventory!A:E",
        "options": {},
        "sheetId": "{{your_google_sheet_id}}",
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "f8494b35-8f50-4725-b00e-4ad8a3d2bf28",
      "name": "Update Stock Levels",
      "type": "n8n-nodes-base.code",
      "position": [
        -560,
        440
      ],
      "parameters": {
        "jsCode": "// Process stock additions or deductions\nconst items = $input.all();\nconst updatedStock = [];\n\n// Example: Assume input contains stock updates (e.g., from a form or API)\nconst stockUpdates = [\n  { item: 'Cement', action: 'add', quantity: 50 },\n  { item: 'Paint', action: 'use', quantity: 10 }\n  // Replace with dynamic input from a form or API\n];\n\nfor (const item of items) {\n  let stock = item.json;\n  const update = stockUpdates.find(u => u.item === stock.item);\n  if (update) {\n    if (update.action === 'add') {\n      stock.quantity += update.quantity;\n    } else if (update.action === 'use') {\n      stock.quantity = Math.max(0, stock.quantity - update.quantity);\n    }\n    stock.last_updated = new Date().toISOString();\n  }\n  updatedStock.push(stock);\n}\n\nreturn updatedStock.map(s => ({ json: s }));"
      },
      "typeVersion": 2
    },
    {
      "id": "93d0e02b-0906-4d04-906a-94bbebddc3d9",
      "name": "Check Low Stock",
      "type": "n8n-nodes-base.code",
      "position": [
        -340,
        440
      ],
      "parameters": {
        "jsCode": "// Check for low stock levels\nconst items = $input.all();\nconst lowStockItems = [];\n\nfor (const item of items) {\n  const stock = item.json;\n  // Assume threshold is defined in Google Sheet or set here (e.g., 20)\n  const threshold = stock.threshold || 20;\n  if (stock.quantity <= threshold) {\n    lowStockItems.push({\n      item: stock.item,\n      quantity: stock.quantity,\n      threshold: threshold,\n      alert_message: `Low stock alert: ${stock.item} has ${stock.quantity} units remaining (threshold: ${threshold})`\n    });\n  }\n}\n\nreturn lowStockItems.map(item => ({ json: item }));"
      },
      "typeVersion": 2
    },
    {
      "id": "cdc063f5-2eb6-4fd3-a04f-517bc145d7bc",
      "name": "Update Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -120,
        440
      ],
      "parameters": {
        "range": "StockInventory!A:E",
        "options": {},
        "sheetId": "{{your_google_sheet_id}}",
        "operation": "update",
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "7ad3a082-40a3-4baf-9d1f-0b26bfe38234",
      "name": "Send Email Alert",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        100,
        440
      ],
      "parameters": {
        "text": "=Hello Team,\n\nThis is an automated notification from the Stock Maintenance System.\n\nThe following items are below the defined stock threshold as of {{ $now.format('YYYY-MM-DD') }}:\n\n{{ $json.lowStockList }}\n\nPlease take necessary action to restock these items at the earliest to avoid any disruptions.\n\nIf you have any questions or need help updating the stock sheet, feel free to reach out.\n\nThank you,\nStock Monitoring Bot\nReal Estate Operations\n\n",
        "options": {},
        "subject": "Low Stock Alert - {{ $now.format('YYYY-MM-DD') }}",
        "toEmail": "user@example.com",
        "fromEmail": "user@example.com",
        "emailFormat": "text"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "f3585868-bd02-4d66-8efc-260d757de587",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -980,
        -20
      ],
      "parameters": {
        "width": 1000,
        "height": 240,
        "content": "## \ud83d\udccc Workflow Overview:\n- **Daily Stock Check**: Triggers daily to monitor stock levels.\n- **Fetch Stock Data**: Retrieves current stock levels from Google Sheets.\n- **Update Stock Levels**: Processes additions or deductions (e.g., new stock or usage).\n- **Check Low Stock**: Identifies items below the defined threshold.\n- **Update Google Sheet**: Saves updated stock levels back to Google Sheets.\n- **Send Email Alert**: Sends low-stock alerts to the inventory team."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "06121193-c363-4c50-bc64-e6b6c0255b59",
  "connections": {
    "Check Low Stock": {
      "main": [
        [
          {
            "node": "Update Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Stock Data": {
      "main": [
        [
          {
            "node": "Update Stock Levels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Stock Check": {
      "main": [
        [
          {
            "node": "Fetch Stock Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Google Sheet": {
      "main": [
        [
          {
            "node": "Send Email Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Stock Levels": {
      "main": [
        [
          {
            "node": "Check Low Stock",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}