# EMR App Refactoring Plan ## Changes Required ### 1. Remove Encounter Model ❌ The entire `Encounter` class and `EncounterManager` need to be removed from `emr/models.py` since they now live in `core/models.py`. ### 2. Add Imports ✅ ```python # At the top of emr/models.py from core.models import Encounter, Patient, TenantScopedModel ``` ### 3. Update Patient References (8 models) All models currently reference `'patients.PatientProfile'` - these need to change to `'core.Patient'`: 1. **VitalSigns** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` - Keep `encounter = FK(Encounter)` (local reference, will become core reference) 2. **ProblemList** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` - `related_encounter = FK(Encounter)` → stays the same 3. **CarePlan** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` 4. **ClinicalNote** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` - `encounter = FK(Encounter)` → stays the same 5. **ClinicalRecommendation** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` - `related_encounter = FK(Encounter)` → stays the same 6. **AllergyAlert** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` 7. **CriticalAlert** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` - `related_encounter = FK(Encounter)` → stays the same 8. **DiagnosticSuggestion** - `patient = FK('patients.PatientProfile')` → `FK('core.Patient')` ### 4. Models That Don't Need Patient Changes These models don't have patient FKs: - NoteTemplate (tenant-scoped only) - Icd10 (global reference data) - TreatmentProtocol (tenant-scoped only) - ClinicalGuideline (tenant-scoped only) ### 5. Consider Using TenantScopedModel Many EMR models could inherit from `TenantScopedModel` instead of manually defining tenant/timestamps/etc. This is optional but recommended for consistency. ## Implementation Strategy Given the large file size (~1500 lines), we'll use a targeted approach: 1. **Add imports at top** 2. **Remove Encounter and EncounterManager classes** (lines ~20-300) 3. **Update each model's patient FK** using replace_in_file 4. **Test that all references work** ## Expected Benefits - ✅ Encounter available to all apps - ✅ Patient identity centralized - ✅ Consistent with core architecture - ✅ Cleaner separation of concerns