{
  "id": "RiKOVQcW472o1Hfu",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Send Prescription to Patients via Email & WhatsApp",
  "tags": [],
  "nodes": [
    {
      "id": "368d24da-9d79-4bf5-9c56-1f70bf74ab1e",
      "name": "Filter New Prescriptions",
      "type": "n8n-nodes-base.filter",
      "position": [
        -1240,
        -120
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "1",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.prescription_status }}",
              "rightValue": "new"
            },
            {
              "id": "2",
              "operator": {
                "type": "string",
                "operation": "isNotEmpty"
              },
              "leftValue": "={{ $json.patient_email }}",
              "rightValue": ""
            },
            {
              "id": "3",
              "operator": {
                "type": "string",
                "operation": "isNotEmpty"
              },
              "leftValue": "={{ $json.patient_phone }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "7ca917ec-330c-4f53-90ae-3fa7b7178200",
      "name": "Format Prescription Data",
      "type": "n8n-nodes-base.code",
      "position": [
        -1020,
        -120
      ],
      "parameters": {
        "jsCode": "// Format prescription data for email and WhatsApp\nconst items = $input.all();\n\nreturn items.map(item => {\n  const data = item.json;\n  \n  // Format prescription text\n  const prescriptionText = `\n\ud83c\udfe5 PRESCRIPTION\n\nPatient: ${data.patient_name}\nDoctor: ${data.doctor_name}\nDate: ${data.prescription_date}\n\nPrescription Details:\n${data.prescription_details}\n\nDosage Instructions:\n${data.dosage_instructions}\n\nFollow-up Date: ${data.followup_date || 'Not specified'}\n\n\u26a0\ufe0f Important: Take medications as prescribed. Contact your doctor if you have any questions.\n  `;\n  \n  // Format email HTML\n  const emailHTML = `\n    <div style=\"font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;\">\n      <div style=\"background-color: #f8f9fa; padding: 20px; border-radius: 8px;\">\n        <h2 style=\"color: #2c3e50; text-align: center;\">\ud83d\udccb Medical Prescription</h2>\n        \n        <div style=\"background-color: white; padding: 20px; border-radius: 6px; margin: 20px 0;\">\n          <h3 style=\"color: #3498db; margin-top: 0;\">Patient Information</h3>\n          <p><strong>Name:</strong> ${data.patient_name}</p>\n          <p><strong>Doctor:</strong> ${data.doctor_name}</p>\n          <p><strong>Date:</strong> ${data.prescription_date}</p>\n        </div>\n        \n        <div style=\"background-color: white; padding: 20px; border-radius: 6px; margin: 20px 0;\">\n          <h3 style=\"color: #27ae60; margin-top: 0;\">Prescription Details</h3>\n          <p style=\"white-space: pre-line;\">${data.prescription_details}</p>\n        </div>\n        \n        <div style=\"background-color: white; padding: 20px; border-radius: 6px; margin: 20px 0;\">\n          <h3 style=\"color: #e74c3c; margin-top: 0;\">Dosage Instructions</h3>\n          <p style=\"white-space: pre-line;\">${data.dosage_instructions}</p>\n        </div>\n        \n        ${data.followup_date ? `\n        <div style=\"background-color: white; padding: 20px; border-radius: 6px; margin: 20px 0;\">\n          <h3 style=\"color: #f39c12; margin-top: 0;\">Follow-up</h3>\n          <p><strong>Next Appointment:</strong> ${data.followup_date}</p>\n        </div>\n        ` : ''}\n        \n        <div style=\"background-color: #fff3cd; border: 1px solid #ffeaa7; padding: 15px; border-radius: 6px; margin: 20px 0;\">\n          <p style=\"margin: 0; color: #856404;\">\u26a0\ufe0f <strong>Important:</strong> Take medications exactly as prescribed. Contact your doctor if you experience any side effects or have questions.</p>\n        </div>\n        \n        <div style=\"text-align: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd;\">\n          <p style=\"color: #7f8c8d; font-size: 12px;\">This prescription was sent automatically from our medical system.</p>\n        </div>\n      </div>\n    </div>\n  `;\n  \n  return {\n    json: {\n      ...data,\n      prescriptionText,\n      emailHTML,\n      whatsappMessage: prescriptionText.replace(/\ud83c\udfe5|\ud83d\udccb|\u26a0\ufe0f/g, ''), // Remove emojis for WhatsApp compatibility\n      timestamp: new Date().toISOString()\n    }\n  };\n});"
      },
      "typeVersion": 2
    },
    {
      "id": "73c4304b-ac16-4deb-aaae-d40c94c29de9",
      "name": "Send WhatsApp",
      "type": "n8n-nodes-base.whatsApp",
      "position": [
        -800,
        -20
      ],
      "parameters": {
        "textBody": "Hello [Patient Name], \ud83d\udc4b Your prescription from Dr. [Doctor Name] dated [Date] is ready. Please check the attached file for your medication details.  \ud83d\udcde For queries, contact us at [Contact Number]. [Clinic/Hospital Name]",
        "operation": "send",
        "phoneNumberId": "=+919988888888",
        "additionalFields": {},
        "recipientPhoneNumber": "=+91999999999999"
      },
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "cc159a2b-d6c2-414d-b3bc-05f4d0a16b29",
      "name": "Log Notification",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -580,
        -120
      ],
      "parameters": {
        "columns": {
          "value": {
            "timestamp": "={{ $json.timestamp }}",
            "doctor_name": "={{ $json.doctor_name }}",
            "email_status": "={{ $('Send email').item.json.success ? 'sent' : 'failed' }}",
            "patient_name": "={{ $json.patient_name }}",
            "patient_email": "={{ $json.patient_email }}",
            "patient_phone": "={{ $json.patient_phone }}",
            "prescription_id": "={{ $json.prescription_id }}",
            "whatsapp_status": "={{ $('Send WhatsApp').item.json.success ? 'sent' : 'failed' }}",
            "notification_type": "prescription_sent"
          },
          "schema": [
            {
              "id": "timestamp",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "patient_name",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "patient_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "patient_email",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "patient_email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "patient_phone",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "patient_phone",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "doctor_name",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "doctor_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "prescription_id",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "prescription_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "email_status",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "email_status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "whatsapp_status",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "whatsapp_status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "notification_type",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "notification_type",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "patient_name"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "appendOrUpdate",
        "sheetName": "Notification_Log",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "=YOUR_GOOGLE_SHEET"
        },
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "0c9e09cb-1d80-4f09-abfd-4d3d712833ef",
      "name": "Update Prescription Status",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -360,
        -120
      ],
      "parameters": {
        "columns": {
          "value": {
            "prescription_status": "sent"
          },
          "schema": [
            {
              "id": "prescription_id",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "prescription_id",
              "defaultMatch": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "prescription_status",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "prescription_status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "prescription_id"
          ]
        },
        "options": {},
        "operation": "update",
        "sheetName": "Prescriptions",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "=YOUR_GOOGLE_SHEET"
        },
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "5c2bf898-b7a2-43c9-a90d-e852fecf829e",
      "name": "Google Sheets Trigger",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "position": [
        -1460,
        -120
      ],
      "parameters": {
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "id",
          "value": "=cfrt5tgbhu8iujnjio90"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "=ojnbgt56ybb"
        }
      },
      "credentials": {
        "googleSheetsTriggerOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "530efe83-c80e-4b1e-8121-c508907cecd1",
      "name": "Send email",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        -800,
        -220
      ],
      "parameters": {
        "text": "Dear [Patient Name],\n\nPlease find attached your prescription from Dr. [Doctor Name] dated [Date].\nIf you have any questions about your medication, kindly contact our clinic.\n\nAttachment: Prescription PDF/Image\n\nBest regards,\n[Clinic/Hospital Name]\n[Contact Number] | [Email Address]",
        "options": {
          "appendAttribution": false,
          "allowUnauthorizedCerts": false
        },
        "subject": "Your Prescription from [Doctor/Hospital Name]",
        "toEmail": "={{ $json.patient_email }}",
        "fromEmail": "user@example.com"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "41b633a7-3641-4816-8b13-eb26a3d37686",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1580,
        -480
      ],
      "parameters": {
        "color": 3,
        "width": 660,
        "height": 260,
        "content": "## \ud83c\udf1f Features Included\n\n- **Automated Trigger**: Monitors Google Sheet for new prescriptions\n- **Smart Filtering**: Only processes prescriptions with status \"new\"\n- **Rich Email Format**: Professional HTML email with prescription details\n- **WhatsApp Integration**: Sends formatted prescription text\n- **Comprehensive Logging**: Tracks all sent notifications\n- **Status Updates**: Marks prescriptions as \"sent\" after processing\n- **Error Handling**: Logs success/failure status for both channels"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "62ea4e96-2323-44a0-8a1c-9726039672be",
  "connections": {
    "Send email": {
      "main": [
        [
          {
            "node": "Log Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp": {
      "main": [
        [
          {
            "node": "Log Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Notification": {
      "main": [
        [
          {
            "node": "Update Prescription Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets Trigger": {
      "main": [
        [
          {
            "node": "Filter New Prescriptions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter New Prescriptions": {
      "main": [
        [
          {
            "node": "Format Prescription Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Prescription Data": {
      "main": [
        [
          {
            "node": "Send email",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}