{
  "id": "pgQtR2koohYLYz2J",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Send Daily Patient Condition Updates to Assigned Doctors",
  "tags": [],
  "nodes": [
    {
      "id": "4919ea3e-0d1f-425f-9f04-5b1fa5d56cf9",
      "name": "Daily Trigger (8 AM)",
      "type": "n8n-nodes-base.cron",
      "position": [
        -1500,
        -40
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "130907ef-736b-49af-b07e-0d0cd40d278f",
      "name": "Read Patient Data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -1280,
        -40
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultName": "Patients"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_GOOGLE_SHEET_ID"
        },
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "20cf609d-63d1-42cf-878e-40e40a37938a",
      "name": "Filter Active Patients",
      "type": "n8n-nodes-base.filter",
      "position": [
        -1060,
        -40
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "patient-filter",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.Status }}",
              "rightValue": "Active"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "72680bdb-08ea-45b2-a8e3-8f5c70d56b7e",
      "name": "Process Patient Data",
      "type": "n8n-nodes-base.code",
      "position": [
        -840,
        -40
      ],
      "parameters": {
        "jsCode": "const patients = $input.all();\nconst reports = [];\n\nfor (const patient of patients) {\n  const currentDate = new Date().toLocaleDateString('en-US', {\n    year: 'numeric',\n    month: 'long',\n    day: 'numeric'\n  });\n  \n  const reportData = {\n    patientId: patient.json['Patient ID'],\n    patientName: patient.json['Patient Name'],\n    age: patient.json['Age'],\n    condition: patient.json['Current Condition'],\n    vitals: {\n      temperature: patient.json['Temperature'] || 'N/A',\n      bloodPressure: patient.json['Blood Pressure'] || 'N/A',\n      heartRate: patient.json['Heart Rate'] || 'N/A'\n    },\n    medication: patient.json['Current Medication'] || 'None',\n    doctorEmail: patient.json['Assigned Doctor Email'],\n    doctorWhatsApp: patient.json['Doctor WhatsApp'],\n    lastUpdated: patient.json['Last Updated'] || currentDate,\n    reportDate: currentDate,\n    priority: patient.json['Priority'] || 'Normal'\n  };\n  \n  // Generate condition summary\n  let conditionStatus = 'Stable';\n  if (patient.json['Temperature'] && parseFloat(patient.json['Temperature']) > 100.4) {\n    conditionStatus = 'Attention Required';\n  }\n  if (patient.json['Priority'] === 'Critical') {\n    conditionStatus = 'Critical';\n  }\n  \n  reportData.conditionStatus = conditionStatus;\n  \n  // Create email content\n  reportData.emailSubject = `Daily Patient Report - ${reportData.patientName} (${reportData.patientId}) - ${conditionStatus}`;\n  \n  reportData.emailBody = `\nDear Doctor,\n\nDaily Patient Condition Report for ${currentDate}\n\n\ud83d\udccb PATIENT INFORMATION:\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\nPatient ID: ${reportData.patientId}\nName: ${reportData.patientName}\nAge: ${reportData.age}\nCondition Status: ${conditionStatus}\n\n\ud83c\udfe5 CURRENT CONDITION:\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n${reportData.condition}\n\n\ud83d\udcca VITAL SIGNS:\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n\u2022 Temperature: ${reportData.vitals.temperature}\u00b0F\n\u2022 Blood Pressure: ${reportData.vitals.bloodPressure} mmHg\n\u2022 Heart Rate: ${reportData.vitals.heartRate} bpm\n\n\ud83d\udc8a CURRENT MEDICATION:\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n${reportData.medication}\n\n\ud83d\udcc5 Last Updated: ${reportData.lastUpdated}\nReport Generated: ${currentDate}\n\nPlease review and take necessary action if required.\n\nBest regards,\nPatient Monitoring System\n  `;\n  \n  // Create WhatsApp message\n  reportData.whatsappMessage = `\ud83c\udfe5 *DAILY PATIENT REPORT*\\n\\n\ud83d\udc64 *Patient:* ${reportData.patientName} (${reportData.patientId})\\n\ud83d\udcca *Status:* ${conditionStatus}\\n\ud83c\udf21\ufe0f *Temp:* ${reportData.vitals.temperature}\u00b0F\\n\ud83d\udc93 *BP:* ${reportData.vitals.bloodPressure}\\n\ud83d\udc97 *HR:* ${reportData.vitals.heartRate} bpm\\n\\n\ud83d\udccb *Condition:* ${reportData.condition}\\n\ud83d\udc8a *Medication:* ${reportData.medication}\\n\\n\ud83d\udcc5 *Report Date:* ${currentDate}`;\n  \n  reports.push(reportData);\n}\n\nreturn reports.map(report => ({ json: report }));"
      },
      "typeVersion": 2
    },
    {
      "id": "c71e8ca4-b4d6-4e42-94d0-195d67701bfa",
      "name": "Send Email Report",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        -620,
        -340
      ],
      "parameters": {
        "options": {},
        "subject": "={{ $json.emailSubject }}",
        "toEmail": "={{ $json.doctorEmail }}",
        "fromEmail": "user@example.com"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "792824e8-ca46-4f2e-a4c8-95f8d3803ba2",
      "name": "Send WhatsApp Message",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -620,
        60
      ],
      "parameters": {
        "url": "https://api.whatsapp.com/send",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "phone",
              "value": "={{ $json.doctorWhatsApp }}"
            },
            {
              "name": "message",
              "value": "={{ $json.whatsappMessage }}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Authorization",
              "value": "Bearer YOUR_TOKEN_HERE"
            }
          ]
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "426ad1c8-9ea1-450a-b0db-43b4cabd8745",
      "name": "Filter Critical Patients",
      "type": "n8n-nodes-base.filter",
      "position": [
        -620,
        -140
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "critical-condition",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.conditionStatus }}",
              "rightValue": "Critical"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "86c379d4-90e8-451c-aa1e-4cbf7aca7aac",
      "name": "Send Critical Alert",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        -400,
        -140
      ],
      "parameters": {
        "options": {},
        "subject": "\ud83d\udea8 CRITICAL PATIENT ALERT - {{ $json.patientName }}",
        "toEmail": "user@example.com,user@example.com",
        "fromEmail": "user@example.com"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "cb4d6e31-b6f0-4002-aba8-6abab3d457ef",
      "name": "Log Report to Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -620,
        260
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=1",
          "cachedResultName": "Reports_Log"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_GOOGLE_SHEET_ID"
        },
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "a64546a8-35b2-4c4c-ab5b-837c35ed7dff",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1880,
        -480
      ],
      "parameters": {
        "color": 3,
        "width": 660,
        "height": 300,
        "content": "## How It Works\n- **Cron Trigger** - Schedules daily run at 8 AM.\n- **Google Sheets (Read)** - Fetches patient data from \"Patients\" sheet.\n- **Filter Node** - Selects active patients.\n- **Code Node** - Processes data, creates report content, checks for critical conditions.\n- **Email Send Node** - Sends reports to doctors via Gmail.\n- **HTTP Request Node** - Sends WhatsApp messages.\n- **Filter Critical** - Identifies critical patients.\n- **Critical Alert Email** - Notifies hospital staff.\n- **Google Sheets (Write)** - Logs reports to \"Reports_Log\" sheet."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "36797054-fa50-46b7-a02b-5417b8d49379",
  "connections": {
    "Read Patient Data": {
      "main": [
        [
          {
            "node": "Filter Active Patients",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Trigger (8 AM)": {
      "main": [
        [
          {
            "node": "Read Patient Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Patient Data": {
      "main": [
        [
          {
            "node": "Send Email Report",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send WhatsApp Message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter Critical Patients",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log Report to Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Active Patients": {
      "main": [
        [
          {
            "node": "Process Patient Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Critical Patients": {
      "main": [
        [
          {
            "node": "Send Critical Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}