{
  "id": "gwxWo0IhhGanHkLX",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Monitor Blood Bank Stock & Send Low Blood Group Alerts",
  "tags": [],
  "nodes": [
    {
      "id": "629d6117-e428-43ca-ba31-91bef4bed7b8",
      "name": "Check Stock Availability",
      "type": "n8n-nodes-base.code",
      "position": [
        -340,
        360
      ],
      "parameters": {
        "jsCode": "// Check for low blood stock levels\nconst items = $input.all();\nconst lowBloodStock = [];\n\nfor (const item of items) {\n  const bloodStock = item.json;\n  // Threshold per blood group, can be set in data or default to 10 units\n  const threshold = bloodStock.threshold || 10;\n  if (bloodStock.quantity <= threshold) {\n    lowBloodStock.push({\n      blood_group: bloodStock.blood_group,\n      quantity: bloodStock.quantity,\n      threshold: threshold,\n      alert_message: `Low blood stock alert: ${bloodStock.blood_group} has only ${bloodStock.quantity} units left (threshold: ${threshold})`\n    });\n  }\n}\n\nreturn lowBloodStock.map(blood => ({ json: blood }));\n"
      },
      "typeVersion": 2
    },
    {
      "id": "edc2d357-9e97-47f4-b81d-b5152cf26d1a",
      "name": "Send Alert Message",
      "type": "n8n-nodes-base.whatsApp",
      "position": [
        -120,
        515
      ],
      "parameters": {
        "textBody": "=\u26a0\ufe0f Blood Stock Alert \u2013 {{ $now }}\n\nBlood group(s) low or unavailable:\n{{ $json.lowBloodList }}\n\nPlease arrange urgent replenishment to ensure availability for patients in need.\n\nFor help, contact the blood bank team.\n\nThank you!",
        "operation": "send",
        "phoneNumberId": "=+919998887765",
        "additionalFields": {},
        "recipientPhoneNumber": "=+919988223377"
      },
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "96e4c613-da63-4e26-9d49-4155837b6ade",
      "name": "Fetch Blood Stock",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -780,
        515
      ],
      "parameters": {
        "range": "Stock!A:E",
        "options": {},
        "sheetId": "{{your_google_sheet_id}}",
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "cf195b8e-8bd0-401e-8ea7-23af2e6eca09",
      "name": "Daily Check Blood Stock ",
      "type": "n8n-nodes-base.cron",
      "position": [
        -1000,
        515
      ],
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "hour": 8
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5e38254b-2b2d-4aa4-b44a-0d7b20a7f2ed",
      "name": "Get All Stock ",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -560,
        515
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "0189887f-a549-46fb-a92f-f045f7737209",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1140,
        40
      ],
      "parameters": {
        "color": 4,
        "width": 520,
        "height": 360,
        "content": "## Workflow Process\n1. **Daily Check Blood Stock**: Triggers the workflow daily.\n2. **Fetch Blood Stock**: Reads data from a Google Sheet.\n3. **Get All Stock**: Collects all available blood stock details.\n4. **Check Stock Availability**: Analyzes stock levels for low thresholds.\n5. **Send Alert Message**: Sends WhatsApp alerts if stock is low.\n\n## Sheet Columns\n- **Blood Type**: Type of blood (e.g., A+, O-).\n- **Quantity**: Current stock amount.\n- **Threshold**: Minimum acceptable stock level.\n- **Last Updated**: Date and time of last update.\n- **Status**: Current status (e.g., Low, Sufficient)."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "8bf5da17-7278-4dba-887f-9bc593416aac",
  "connections": {
    "Get All Stock ": {
      "main": [
        [],
        [
          {
            "node": "Check Stock Availability",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Blood Stock": {
      "main": [
        [
          {
            "node": "Get All Stock ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Alert Message": {
      "main": [
        [
          {
            "node": "Get All Stock ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Stock Availability": {
      "main": [
        [
          {
            "node": "Send Alert Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Check Blood Stock ": {
      "main": [
        [
          {
            "node": "Fetch Blood Stock",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}