4.2 KiB
4.2 KiB
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.htmlwhich 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_settingsview to properly filter hospitals by user's organization - Updated
SurveyTemplateMappingViewSetto filter by hospital - Fixed hospital field to use proper ForeignKey field (not hardcoded)
- Updated
3. JavaScript Errors
- Issue:
data.forEach is not a functionwhen 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
- Added
5. CSRF Token Issues
- Issue: CSRF token not properly retrieved, causing 403 errors
- Fix: Implemented multiple fallback methods to get CSRF token:
- From hidden input (most reliable)
- From cookie (case-insensitive search)
- From meta tag
6. Serializer Field Mismatch
- Issue:
SurveyTemplateMappingmodel fields didn't match serializer fields - Fix:
- Updated model to use
patient_typeCharField (notpatient_type_codeandpatient_type_name) - Removed
descriptionfield fromSurveyTemplateSerializer - Fixed all serializers to match actual model fields
- Fixed
PublicSurveySerializerto includehospitalfield
- Updated model to use
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, andpriority - 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
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
templates/integrations/survey_mapping_settings.html- Complete rewrite with fixesapps/integrations/models.py- Updated SurveyTemplateMapping modelapps/integrations/serializers.py- Fixed all serializersapps/surveys/serializers.py- Fixed serializersapps/integrations/ui_views.py- Fixed view logicapps/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