{
  "name": "08 Bedside Quick Record - Google Sheets",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "bedside-quick-record-demo",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        220,
        320
      ],
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0081",
      "name": "Webhook",
      "webhookId": "bedside-quick-record-demo"
    },
    {
      "parameters": {
        "keepOnlySet": false,
        "values": {
          "string": [
            {
              "name": "patient_id",
              "value": "={{$json.body.patient_id || ''}}"
            },
            {
              "name": "bed_no",
              "value": "={{$json.body.bed_no || ''}}"
            },
            {
              "name": "shift",
              "value": "={{$json.body.shift || ''}}"
            },
            {
              "name": "nurse_id",
              "value": "={{$json.body.nurse_id || ''}}"
            },
            {
              "name": "recorded_at",
              "value": "={{$json.body.recorded_at || $now}}"
            },
            {
              "name": "consciousness",
              "value": "={{$json.body.consciousness || ''}}"
            },
            {
              "name": "line_status",
              "value": "={{$json.body.line_status || ''}}"
            },
            {
              "name": "wound_status",
              "value": "={{$json.body.wound_status || ''}}"
            },
            {
              "name": "interventions",
              "value": "={{$json.body.interventions || ''}}"
            },
            {
              "name": "abnormal_notes",
              "value": "={{$json.body.abnormal_notes || ''}}"
            },
            {
              "name": "follow_up_needed",
              "value": "={{$json.body.follow_up_needed || ''}}"
            },
            {
              "name": "created_at",
              "value": "={{$now}}"
            }
          ],
          "number": [
            {
              "name": "temperature",
              "value": "={{$json.body.temperature || 0}}"
            },
            {
              "name": "pulse",
              "value": "={{$json.body.pulse || 0}}"
            },
            {
              "name": "respiration",
              "value": "={{$json.body.respiration || 0}}"
            },
            {
              "name": "bp_sys",
              "value": "={{$json.body.bp_sys || 0}}"
            },
            {
              "name": "bp_dia",
              "value": "={{$json.body.bp_dia || 0}}"
            },
            {
              "name": "spo2",
              "value": "={{$json.body.spo2 || 0}}"
            },
            {
              "name": "pain_score",
              "value": "={{$json.body.pain_score || 0}}"
            },
            {
              "name": "intake_ml",
              "value": "={{$json.body.intake_ml || 0}}"
            },
            {
              "name": "urine_ml",
              "value": "={{$json.body.urine_ml || 0}}"
            },
            {
              "name": "stool_count",
              "value": "={{$json.body.stool_count || 0}}"
            }
          ],
          "boolean": [
            {
              "name": "has_required_fields",
              "value": "={{!!($json.body.patient_id && $json.body.bed_no && $json.body.shift && $json.body.nurse_id)}}"
            }
          ]
        },
        "options": {}
      },
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0082",
      "name": "Normalize Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        320
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.has_required_fields}}",
              "operation": "isTrue"
            }
          ]
        }
      },
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0083",
      "name": "Has Required Fields",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        760,
        320
      ]
    },
    {
      "parameters": {
        "jsCode": "const item = $input.first().json;\nconst alertReasons = [];\n\nif (item.spo2 > 0 && item.spo2 < 92) alertReasons.push('low_spo2');\nif (item.temperature >= 38) alertReasons.push('high_temperature');\nif (item.pain_score >= 7) alertReasons.push('high_pain_score');\nif ((item.line_status || '').toLowerCase() === 'abnormal') alertReasons.push('line_issue');\nif ((item.wound_status || '').toLowerCase() === 'worsening') alertReasons.push('wound_issue');\n\nconst summary = `${item.recorded_at} 床號 ${item.bed_no}，T ${item.temperature} / P ${item.pulse} / R ${item.respiration} / BP ${item.bp_sys}/${item.bp_dia} / SpO2 ${item.spo2}%，疼痛 ${item.pain_score} 分，意識 ${item.consciousness || '未填'}，處置：${item.interventions || '未填'}。`;\n\nreturn [{\n  json: {\n    ...item,\n    alert_needed: alertReasons.length > 0,\n    alert_reason: alertReasons.join(','),\n    record_summary: summary,\n  }\n}];"
      },
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0084",
      "name": "Evaluate Alert and Build Summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1020,
        220
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "https://docs.google.com/spreadsheets/d/YOUR_SPREADSHEET_ID/edit#gid=0",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": "BedsideRecords",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "created_at": "={{$json.created_at}}",
            "patient_id": "={{$json.patient_id}}",
            "bed_no": "={{$json.bed_no}}",
            "shift": "={{$json.shift}}",
            "nurse_id": "={{$json.nurse_id}}",
            "recorded_at": "={{$json.recorded_at}}",
            "temperature": "={{$json.temperature}}",
            "pulse": "={{$json.pulse}}",
            "respiration": "={{$json.respiration}}",
            "bp_sys": "={{$json.bp_sys}}",
            "bp_dia": "={{$json.bp_dia}}",
            "spo2": "={{$json.spo2}}",
            "pain_score": "={{$json.pain_score}}",
            "consciousness": "={{$json.consciousness}}",
            "line_status": "={{$json.line_status}}",
            "wound_status": "={{$json.wound_status}}",
            "intake_ml": "={{$json.intake_ml}}",
            "urine_ml": "={{$json.urine_ml}}",
            "stool_count": "={{$json.stool_count}}",
            "interventions": "={{$json.interventions}}",
            "abnormal_notes": "={{$json.abnormal_notes}}",
            "follow_up_needed": "={{$json.follow_up_needed}}",
            "alert_needed": "={{$json.alert_needed}}",
            "alert_reason": "={{$json.alert_reason}}",
            "record_summary": "={{$json.record_summary}}"
          },
          "matchingColumns": [],
          "schema": [
            { "id": "created_at", "displayName": "created_at", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "patient_id", "displayName": "patient_id", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "bed_no", "displayName": "bed_no", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "shift", "displayName": "shift", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "nurse_id", "displayName": "nurse_id", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "recorded_at", "displayName": "recorded_at", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "temperature", "displayName": "temperature", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "pulse", "displayName": "pulse", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "respiration", "displayName": "respiration", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "bp_sys", "displayName": "bp_sys", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "bp_dia", "displayName": "bp_dia", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "spo2", "displayName": "spo2", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "pain_score", "displayName": "pain_score", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "consciousness", "displayName": "consciousness", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "line_status", "displayName": "line_status", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "wound_status", "displayName": "wound_status", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "intake_ml", "displayName": "intake_ml", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "urine_ml", "displayName": "urine_ml", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "stool_count", "displayName": "stool_count", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "interventions", "displayName": "interventions", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "abnormal_notes", "displayName": "abnormal_notes", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "follow_up_needed", "displayName": "follow_up_needed", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "alert_needed", "displayName": "alert_needed", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "alert_reason", "displayName": "alert_reason", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true },
            { "id": "record_summary", "displayName": "record_summary", "type": "string", "display": true, "required": false, "defaultMatch": false, "canBeUsedToMatch": true }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1280,
        220
      ],
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0085",
      "name": "Append row in sheet"
    },
    {
      "parameters": {
        "keepOnlySet": false,
        "values": {
          "boolean": [
            {
              "name": "ok",
              "value": true
            }
          ],
          "string": [
            {
              "name": "message",
              "value": "Bedside record saved"
            },
            {
              "name": "patient_id",
              "value": "={{$node[\"Evaluate Alert and Build Summary\"].json.patient_id}}"
            },
            {
              "name": "bed_no",
              "value": "={{$node[\"Evaluate Alert and Build Summary\"].json.bed_no}}"
            },
            {
              "name": "record_summary",
              "value": "={{$node[\"Evaluate Alert and Build Summary\"].json.record_summary}}"
            },
            {
              "name": "alert_reason",
              "value": "={{$node[\"Evaluate Alert and Build Summary\"].json.alert_reason}}"
            }
          ],
          "boolean": [
            {
              "name": "alert_needed",
              "value": "={{$node[\"Evaluate Alert and Build Summary\"].json.alert_needed}}"
            }
          ]
        },
        "options": {}
      },
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0086",
      "name": "Success Response",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1540,
        220
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "boolean": [
            {
              "name": "ok",
              "value": false
            }
          ],
          "string": [
            {
              "name": "message",
              "value": "Missing required fields. Required: patient_id, bed_no, shift, nurse_id"
            }
          ]
        },
        "options": {}
      },
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0087",
      "name": "Error Response",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1020,
        420
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{$json}}",
        "options": {
          "responseCode": 200
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1800,
        320
      ],
      "id": "79917335-58f7-4dcb-9bb1-b0ba6bde0088",
      "name": "Respond to Webhook"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Normalize Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Input": {
      "main": [
        [
          {
            "node": "Has Required Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Required Fields": {
      "main": [
        [
          {
            "node": "Evaluate Alert and Build Summary",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evaluate Alert and Build Summary": {
      "main": [
        [
          {
            "node": "Append row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet": {
      "main": [
        [
          {
            "node": "Success Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Success Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Error Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {},
  "meta": {}
}
