# Survey Mapping Settings Fix Complete ## Summary Fixed all issues with the Survey Template Mappings page to properly manage survey-hospital-patient type relationships. ## Issues Fixed ### 1. Template Base Error - **Issue**: Template extended `layouts/app_base.html` which didn't exist - **Fix**: Changed to extend `layouts/base.html` ### 2. Empty Dropdowns - **Issue**: Hospital and survey template dropdowns were empty - **Fix**: - Updated `survey_mapping_settings` view to properly filter hospitals by user's organization - Updated `SurveyTemplateMappingViewSet` to filter by hospital - Fixed hospital field to use proper ForeignKey field (not hardcoded) ### 3. JavaScript Errors - **Issue**: `data.forEach is not a function` when loading survey templates - **Fix**: Added proper array handling for both direct array and paginated response formats ### 4. Save Button Not Working - **Issue**: Clicking save would refresh page without saving data - **Fix**: - Added `event.preventDefault()` to prevent form submission - Implemented proper error handling and user feedback - Added loading state to prevent double submission ### 5. CSRF Token Issues - **Issue**: CSRF token not properly retrieved, causing 403 errors - **Fix**: Implemented multiple fallback methods to get CSRF token: 1. From hidden input (most reliable) 2. From cookie (case-insensitive search) 3. From meta tag ### 6. Serializer Field Mismatch - **Issue**: `SurveyTemplateMapping` model fields didn't match serializer fields - **Fix**: - Updated model to use `patient_type` CharField (not `patient_type_code` and `patient_type_name`) - Removed `description` field from `SurveyTemplateSerializer` - Fixed all serializers to match actual model fields - Fixed `PublicSurveySerializer` to include `hospital` field ### 7. JavaScript Field Names - **Issue**: JavaScript was using old field names (`patient_type_code`, `patient_type_name`, `priority`) - **Fix**: Updated all JavaScript to use correct field names: - `patient_type` (string field) - Removed `patient_type_code`, `patient_type_name`, and `priority` - Updated edit mapping button data attributes - Updated save mapping function data object ### 8. Form Structure - **Issue**: Form had fields that didn't exist in the model - **Fix**: Updated form to match model structure: - Changed patient type from two fields (code + name) to single dropdown - Removed priority field - Added all patient type options: 1, 2, 3, 4, O, E, APPOINTMENT ## Model Changes ### SurveyTemplateMapping Model ```python patient_type = models.CharField(max_length=20) # Changed from patient_type_code/patient_type_name # Removed: priority field ``` ## Final Form Fields - **hospital**: Dropdown (required) - **survey_template**: Dropdown (required, filtered by hospital) - **patient_type**: Dropdown with options: - 1 - Inpatient (Type 1) - 2 - Outpatient (Type 2) - 3 - Emergency (Type 3) - 4 - Day Case (Type 4) - O - Outpatient (Type O) - E - Emergency (Type E) - APPOINTMENT - Appointment - **is_active**: Checkbox (default: true) ## Testing All functionality has been tested and verified: - ✅ Page loads correctly - ✅ Hospital dropdown populated with user's hospitals - ✅ Survey template dropdown filters by hospital - ✅ Can add new mapping - ✅ Can edit existing mapping - ✅ Can delete mapping - ✅ Proper error messages displayed - ✅ CSRF token handling works correctly - ✅ No page refresh on save ## Files Modified 1. `templates/integrations/survey_mapping_settings.html` - Complete rewrite with fixes 2. `apps/integrations/models.py` - Updated SurveyTemplateMapping model 3. `apps/integrations/serializers.py` - Fixed all serializers 4. `apps/surveys/serializers.py` - Fixed serializers 5. `apps/integrations/ui_views.py` - Fixed view logic 6. `apps/integrations/views.py` - Fixed API viewset ## Related Work This fix was part of the survey satisfaction options implementation where: - ✅ Inpatient, Outpatient, and Appointment surveys were created - ✅ All questions configured with 5 satisfaction options (Very Unsatisfied, Poor, Neutral, Good, Very Satisfied) - ✅ Questions configured as radio button type (not checkbox) - ✅ Bilingual labels added (English/Arabic) - ✅ Surveys successfully created and verified in database