221 lines
8.6 KiB
Markdown
221 lines
8.6 KiB
Markdown
# Admin Complaint Notification Email Fix
|
||
|
||
## Problem
|
||
When a new complaint was created, the admin notification email was being sent as plain text without using the branded HTML email template system.
|
||
|
||
## Root Cause
|
||
The `notify_admins_new_complaint()` function in `apps/complaints/tasks.py` was generating plain text emails with bilingual content (English/Arabic) but not rendering any HTML template.
|
||
|
||
**Location:** `apps/complaints/tasks.py` lines 2408-2500
|
||
|
||
## Solution
|
||
|
||
### 1. Created New Email Template
|
||
**File:** `templates/emails/new_complaint_admin_notification.html`
|
||
|
||
**Features:**
|
||
- ✅ Extends `emails/base_email_template.html` for consistent branding
|
||
- ✅ Hospital logo header with gradient background
|
||
- ✅ Priority/severity badges with color coding:
|
||
- 🔴 Critical
|
||
- 🟠 High
|
||
- 🟡 Medium
|
||
- 🟢 Low
|
||
- ✅ Complaint details grid (reference, title, priority, severity, status)
|
||
- ✅ Patient information section (name, MRN, phone, email)
|
||
- ✅ Hospital/department section
|
||
- ✅ Description preview
|
||
- ✅ Action required notice
|
||
- ✅ View complaint CTA button
|
||
- ✅ Professional footer with hospital branding
|
||
|
||
### 2. Updated Task Function
|
||
**File:** `apps/complaints/tasks.py`
|
||
|
||
**Changes:**
|
||
1. Added import: `from django.template.loader import render_to_string`
|
||
2. Updated `notify_admins_new_complaint()` function (line 2436)
|
||
3. Now renders HTML template with complaint context
|
||
4. Sends both HTML and plain text versions
|
||
|
||
**Code Changes:**
|
||
```python
|
||
# Before: Plain text only
|
||
message_en = f"""Dear {admin.get_full_name() or 'Admin'},
|
||
A new complaint has been submitted..."""
|
||
|
||
NotificationService.send_email(
|
||
email=admin.email,
|
||
subject=subject_en,
|
||
message=message_en, # Plain text only
|
||
...
|
||
)
|
||
|
||
# After: HTML + Plain text
|
||
context = {
|
||
'admin_name': admin.get_full_name() or 'Admin',
|
||
'priority_badge': priority_badge,
|
||
'is_high_priority': is_high_priority,
|
||
'reference_number': complaint.reference_number,
|
||
'complaint_title': complaint.title,
|
||
'priority': complaint.priority,
|
||
'severity': complaint.severity,
|
||
# ... more context variables
|
||
}
|
||
|
||
html_message = render_to_string(
|
||
'emails/new_complaint_admin_notification.html',
|
||
context
|
||
)
|
||
|
||
NotificationService.send_email(
|
||
email=admin.email,
|
||
subject=subject_en,
|
||
message=message_text, # Plain text fallback
|
||
html_message=html_message, # HTML template ⭐ NEW
|
||
...
|
||
)
|
||
```
|
||
|
||
## Email Template Structure
|
||
|
||
```
|
||
┌─────────────────────────────────────────────┐
|
||
│ Al Hammadi Hospital Logo (Gradient Header) │
|
||
├─────────────────────────────────────────────┤
|
||
│ 📋 New Complaint Notification │
|
||
│ A new complaint has been submitted... │
|
||
├─────────────────────────────────────────────┤
|
||
│ [🚨 URGENT: High Priority Complaint] │
|
||
│ (Shown only for high priority) │
|
||
├─────────────────────────────────────────────┤
|
||
│ Complaint Details │
|
||
│ ┌──────────────┬──────────────┐ │
|
||
│ │ Reference │ Title │ │
|
||
│ │ Priority │ Severity │ │
|
||
│ │ Status │ Submitted │ │
|
||
│ └──────────────┴──────────────┘ │
|
||
├─────────────────────────────────────────────┤
|
||
│ 👤 Patient Information │
|
||
│ Name | MRN | Phone | Email │
|
||
├─────────────────────────────────────────────┤
|
||
│ 🏥 Hospital & Department │
|
||
│ Hospital Name | Department Name │
|
||
├─────────────────────────────────────────────┤
|
||
│ 📝 Description │
|
||
│ (Complaint description preview) │
|
||
├─────────────────────────────────────────────┤
|
||
│ ✓ Action Required │
|
||
│ Please review and activate... │
|
||
├─────────────────────────────────────────────┤
|
||
│ [View Complaint Button] │
|
||
├─────────────────────────────────────────────┤
|
||
│ ℹ️ Notification Information │
|
||
│ Type: Working Hours / After Hours │
|
||
│ Time: 2026-03-12 10:30:00 │
|
||
├─────────────────────────────────────────────┤
|
||
│ PX360 Complaint Management System │
|
||
│ Al Hammadi Hospital │
|
||
└─────────────────────────────────────────────┘
|
||
```
|
||
|
||
## Testing
|
||
|
||
### To Test:
|
||
1. Create a new complaint via the complaint form
|
||
2. Check admin email inbox
|
||
3. Verify email displays:
|
||
- ✅ Hospital branding (logo, colors)
|
||
- ✅ Priority/severity badges
|
||
- ✅ Complaint details grid
|
||
- ✅ Patient information
|
||
- ✅ Hospital/department info
|
||
- ✅ Description preview
|
||
- ✅ Clickable "View Complaint" button
|
||
- ✅ Professional footer
|
||
|
||
### Email Clients to Test:
|
||
- Gmail (Web, iOS, Android)
|
||
- Outlook (Desktop, Web)
|
||
- Apple Mail
|
||
- Yahoo Mail
|
||
|
||
## Files Modified
|
||
|
||
| File | Changes |
|
||
|------|---------|
|
||
| `templates/emails/new_complaint_admin_notification.html` | ⭐ NEW - Created |
|
||
| `apps/complaints/tasks.py` | ✏️ Updated - Added HTML rendering |
|
||
| `templates/emails/README_EMAIL_TEMPLATES.md` | 📝 Updated - Added documentation |
|
||
| `EMAIL_TEMPLATE_SYSTEM_SUMMARY.md` | 📝 Updated - Added template info |
|
||
|
||
## Context Variables
|
||
|
||
The template receives the following context:
|
||
|
||
```python
|
||
{
|
||
'admin_name': str, # Admin's full name
|
||
'priority_badge': str, # e.g., '🚨 URGENT' or '📋 New'
|
||
'is_high_priority': bool, # True for high/critical priority
|
||
'reference_number': str, # Complaint reference
|
||
'complaint_title': str, # Complaint title
|
||
'priority': str, # low/medium/high/critical
|
||
'severity': str, # low/medium/high/critical
|
||
'status': str, # e.g., 'New'
|
||
'patient_name': str, # Patient name or 'N/A'
|
||
'mrn': str, # Medical record number or 'N/A'
|
||
'contact_phone': str, # Phone or 'N/A'
|
||
'contact_email': str, # Email or 'N/A'
|
||
'hospital_name': str, # Hospital name or 'N/A'
|
||
'department_name': str, # Department or 'N/A'
|
||
'description': str, # Complaint description
|
||
'complaint_url': str, # Full URL to complaint
|
||
'notification_type': str, # 'Working Hours' or 'After Hours'
|
||
'current_time': str, # Formatted timestamp
|
||
}
|
||
```
|
||
|
||
## Benefits
|
||
|
||
### Before:
|
||
- ❌ Plain text email
|
||
- ❌ No visual hierarchy
|
||
- ❌ No branding
|
||
- ❌ Hard to scan quickly
|
||
- ❌ No color-coded priority
|
||
|
||
### After:
|
||
- ✅ Professional HTML email with hospital branding
|
||
- ✅ Clear visual hierarchy
|
||
- ✅ Al Hammadi Hospital colors and logo
|
||
- ✅ Easy to scan with sections and badges
|
||
- ✅ Color-coded priority/severity indicators
|
||
- ✅ Prominent CTA button
|
||
- ✅ Mobile-responsive design
|
||
- ✅ Consistent with other email templates
|
||
|
||
## Integration Points
|
||
|
||
This template integrates with:
|
||
1. **Complaint Creation Signal** - Triggered when new complaint is created
|
||
2. **On-Call Admin System** - Respects working hours and on-call schedules
|
||
3. **Notification Service** - Uses `NotificationService.send_email()`
|
||
4. **Audit Logging** - Email sends are logged in database
|
||
|
||
## Future Enhancements
|
||
|
||
Potential improvements:
|
||
1. Add QR code for quick complaint access
|
||
2. Include complaint attachment previews
|
||
3. Add "Quick Actions" buttons (Assign, Escalate, Acknowledge)
|
||
4. Bilingual support (English/Arabic toggle)
|
||
5. Add complaint timeline preview
|
||
6. Include assigned staff member info
|
||
|
||
---
|
||
|
||
**Fixed:** March 12, 2026
|
||
**Version:** 1.0
|
||
**Status:** ✅ Complete
|