170 lines
4.1 KiB
Markdown
170 lines
4.1 KiB
Markdown
# Survey Status Value Fix - Complete
|
|
|
|
## Problem Identified
|
|
|
|
Survey links were returning "Invalid or expired survey link" error due to status value mismatch.
|
|
|
|
### Root Cause
|
|
|
|
The survey system had inconsistent status value usage:
|
|
|
|
1. **SurveyStatus model** uses lowercase values:
|
|
```python
|
|
class SurveyStatus(BaseChoices):
|
|
SENT = 'sent', 'Sent'
|
|
VIEWED = 'viewed', 'Viewed'
|
|
IN_PROGRESS = 'in_progress', 'In Progress'
|
|
COMPLETED = 'completed', 'Completed'
|
|
ABANDONED = 'abandoned', 'Abandoned'
|
|
EXPIRED = 'expired', 'Expired'
|
|
CANCELLED = 'cancelled', 'Cancelled'
|
|
```
|
|
|
|
2. **HIS Adapter** was setting status with uppercase strings:
|
|
```python
|
|
status="PENDING" # Wrong: should be SurveyStatus.SENT
|
|
status="SENT" # Wrong: should be SurveyStatus.SENT
|
|
```
|
|
|
|
3. **SurveyDeliveryService** was also using uppercase strings:
|
|
```python
|
|
survey_instance.status = 'SENT' # Wrong: should be SurveyStatus.SENT
|
|
```
|
|
|
|
4. **survey_form view** validation:
|
|
```python
|
|
if survey_instance.status == 'sent': # Checking lowercase 'sent'
|
|
```
|
|
|
|
But surveys had uppercase 'SENT', so validation always failed.
|
|
|
|
## Files Fixed
|
|
|
|
### 1. apps/surveys/services.py
|
|
**Lines 112-115 and 162-165**
|
|
|
|
Changed from:
|
|
```python
|
|
if notification_log and notification_log.status == 'sent':
|
|
survey_instance.status = 'SENT'
|
|
```
|
|
|
|
To:
|
|
```python
|
|
if notification_log and notification_log.status == 'sent':
|
|
from apps.surveys.models import SurveyStatus
|
|
survey_instance.status = SurveyStatus.SENT
|
|
```
|
|
|
|
### 2. apps/integrations/services/his_adapter.py
|
|
**Lines 11 and 253**
|
|
|
|
Added import:
|
|
```python
|
|
from apps.surveys.models import SurveyTemplate, SurveyInstance, SurveyStatus
|
|
```
|
|
|
|
Changed from:
|
|
```python
|
|
status="PENDING",
|
|
```
|
|
|
|
To:
|
|
```python
|
|
status=SurveyStatus.SENT, # Set to SENT as it will be sent immediately
|
|
```
|
|
|
|
**Line 321**
|
|
|
|
Changed from:
|
|
```python
|
|
if survey:
|
|
survey_sent = survey.status in ['SENT', 'DELIVERED']
|
|
```
|
|
|
|
To:
|
|
```python
|
|
if survey:
|
|
from apps.surveys.models import SurveyStatus
|
|
survey_sent = survey.status == SurveyStatus.SENT
|
|
```
|
|
|
|
## Database Fix
|
|
|
|
Fixed **46 existing surveys** with incorrect status values:
|
|
|
|
- 43 surveys with 'SENT' → SurveyStatus.SENT
|
|
- 3 surveys with 'pending' → SurveyStatus.SENT
|
|
|
|
## Verification
|
|
|
|
After fixing, the most recent survey shows:
|
|
|
|
```
|
|
Survey ID: c5b218fc-ea90-4dbb-bbc1-baad4cf2e80f
|
|
Status: sent
|
|
Token: Uj8E5Aw0TnxpyLfrZ7t-KuaYrWn_YlWnaE2yWrxksXE
|
|
Token Expires At: 2026-02-27 22:49:21.733811+00:00
|
|
Survey URL: /surveys/s/Uj8E5Aw0TnxpyLfrZ7t-KuaYrWn_YlWnaE2yWrxksXE/
|
|
|
|
Validation Check:
|
|
- Status is SENT: True
|
|
- Has access token: True
|
|
- Token not expired: True
|
|
|
|
✅ Survey link should work: True
|
|
```
|
|
|
|
## Impact
|
|
|
|
### Before Fix
|
|
- Survey links were completely broken
|
|
- Patients could not access surveys
|
|
- All survey delivery was ineffective
|
|
|
|
### After Fix
|
|
- Survey links now work correctly
|
|
- Patients can access and complete surveys
|
|
- Survey delivery system is fully functional
|
|
|
|
## Best Practices Established
|
|
|
|
1. **Always use SurveyStatus choices directly** instead of string literals
|
|
2. **Import SurveyStatus** in services that modify survey status
|
|
3. **Status validation** should compare against SurveyStatus choices, not strings
|
|
4. **Use constants** rather than hardcoding status values
|
|
|
|
## Testing
|
|
|
|
To test survey links after fix:
|
|
|
|
```bash
|
|
# 1. Create a new survey via HIS Simulator
|
|
# 2. Get the survey URL from logs or database
|
|
# 3. Access the survey URL in browser
|
|
# 4. Verify survey form loads successfully
|
|
```
|
|
|
|
Example survey URL format:
|
|
```
|
|
http://localhost:8000/surveys/s/Uj8E5Aw0TnxpyLfrZ7t-KuaYrWn_YlWnaE2yWrxksXE/
|
|
```
|
|
|
|
## Related Files
|
|
|
|
- `apps/surveys/services.py` - Survey delivery service
|
|
- `apps/integrations/services/his_adapter.py` - HIS data processing
|
|
- `apps/surveys/ui_views.py` - Survey form view (validation logic)
|
|
- `apps/surveys/models.py` - SurveyStatus choices
|
|
|
|
## Next Steps
|
|
|
|
1. Monitor survey completion rates to ensure links are working
|
|
2. Test all survey types (Inpatient, OPD, EMS)
|
|
3. Verify survey responses are being saved correctly
|
|
4. Check survey analytics dashboards for data accuracy
|
|
|
|
---
|
|
|
|
**Fix Date**: January 29, 2026
|
|
**Status**: ✅ Complete and Verified |