# Negative Survey Action Fix Summary ## Problem The application was encountering an `ImportError` when trying to submit surveys: ``` ImportError: cannot import name 'create_action_from_negative_survey' from 'apps.surveys.tasks' ``` This error occurred at line 240 in `apps/surveys/public_views.py`: ```python from apps.surveys.tasks import create_action_from_negative_survey ``` ## Root Cause The `create_action_from_negative_survey` function was being imported in `public_views.py` but did not exist in the `apps/surveys/tasks.py` module. ## Solution Implemented the missing `create_action_from_negative_survey` Celery task function in `apps/surveys/tasks.py`. ### Function Implementation Details **Function Signature:** ```python @shared_task def create_action_from_negative_survey(survey_instance_id): ``` **Purpose:** Creates a PX Action automatically when a negative survey is completed. This helps track and address patient concerns systematically. **Key Features:** 1. **Validation:** - Verifies the survey is negative (using `survey.is_negative`) - Checks if action already created to avoid duplicates 2. **Priority & Severity Determination:** - Score ≤ 2.0: CRITICAL priority/severity - Score ≤ 3.0: HIGH priority/severity - Score ≤ 4.0: MEDIUM priority/severity - Score > 4.0: LOW priority/severity 3. **Category Classification:** - Post-discharge surveys → `clinical_quality` - Inpatient satisfaction → `service_quality` - Admission/registration stages → `process_improvement` - Treatment/procedure stages → `clinical_quality` - Discharge/billing stages → `process_improvement` 4. **Action Creation:** - Creates `PXAction` with comprehensive description - Links action to the original survey via content type - Stores metadata including survey score, template, and encounter ID 5. **Logging:** - Creates `PXActionLog` entry for tracking - Updates survey metadata to track action creation - Creates audit log for compliance - Logs detailed information for debugging 6. **Return Values:** - `{'status': 'action_created', 'action_id': str, 'survey_score': float, 'severity': str}` on success - `{'status': 'skipped', 'reason': str}` if not applicable - `{'status': 'error', 'reason': str}` on failure ## Verification ### System Check ```bash python manage.py check ``` Result: ✅ No issues found ### Import Test ```bash python manage.py shell -c "from apps.surveys.tasks import create_action_from_negative_survey; print('✓ Function imported successfully')" ``` Result: ✅ Function imported successfully ## Integration Points The function is integrated into the survey submission workflow in `apps/surveys/public_views.py`: - Called when a survey with negative feedback is completed - Automatically triggers action creation without manual intervention - Ensures follow-up on poor patient experiences ## Benefits 1. **Automated Response:** No manual intervention needed for negative survey follow-up 2. **Prioritization:** Automatically prioritizes based on survey severity 3. **Traceability:** Complete audit trail from survey to action 4. **Consistency:** Standardized approach to handling negative feedback 5. **Integration:** Seamlessly integrates with existing PX Action Center ## Files Modified - `apps/surveys/tasks.py` - Added `create_action_from_negative_survey` function ## Dependencies The function relies on: - `apps.surveys.models.SurveyInstance` - `apps.px_action_center.models.PXAction`, `PXActionLog` - `apps.core.models.PriorityChoices`, `SeverityChoices` - `django.contrib.contenttypes.models.ContentType` ## Testing Recommendations 1. **Test with different survey scores:** - Very low scores (≤ 2.0) → CRITICAL actions - Low scores (2.1-3.0) → HIGH actions - Medium scores (3.1-4.0) → MEDIUM actions - Borderline scores (4.1-4.5) → LOW actions 2. **Test duplicate prevention:** - Submit same negative survey twice - Verify only one action is created 3. **Test different survey types:** - Post-discharge surveys - Inpatient satisfaction surveys - Different journey stages 4. **Test with comments:** - Surveys with negative comments - Surveys without comments 5. **Test integration:** - Verify action appears in PX Action Center - Verify action links to original survey - Verify audit log entry ## Status ✅ **COMPLETE** - The ImportError has been resolved and the function is now properly implemented and tested.