8.0 KiB
Hospital Management System - Refactoring Progress
Overview
This document tracks the progress of the comprehensive model refactoring based on the analysis from tools/analyze_models.py.
✅ COMPLETED PHASES
Phase 1: Name Collision Resolution
1.1 IntegrationLog Collision ✅ COMPLETED
Status: Resolved
Date: 2025-10-06
Actions Taken:
- ✅ Removed
IntegrationLogmodel fromcore/models.py(line 710+) - ✅ Added comment directing users to import from
integration.models - ✅ Canonical model remains in
integration/models.py
Impact:
- Eliminated duplicate model definition
- Centralized integration logging in the integration app
- No breaking changes to existing code using
integration.IntegrationLog
1.2 InventoryLocation Collision ✅ COMPLETED
Status: Resolved
Date: 2025-10-06
Actions Taken:
- ✅ Removed
InventoryLocationmodel fromblood_bank/models.py(line 437+) - ✅ Added import statement:
from inventory.models import InventoryLocation - ✅ Added explanatory comment at end of file
- ✅ Canonical model remains in
inventory/models.py
Impact:
- Blood bank now uses centralized inventory location management
- Consistent location tracking across all departments
- Blood bank can still reference InventoryLocation via import
1.3 QualityControl Collision ✅ COMPLETED
Status: Resolved
Date: 2025-10-06
Actions Taken:
- ✅ Removed
QualityControlmodel fromblood_bank/models.py(line 469+) - ✅ Added import statement:
from laboratory.models import QualityControl - ✅ Added explanatory comment at end of file
- ✅ Canonical model remains in
laboratory/models.py
Impact:
- Unified quality control system across laboratory and blood bank
- Consistent QC tracking and CAPA management
- Blood bank can still use QualityControl via import
🔄 IN PROGRESS
Phase 1.4: InsuranceClaim Collision
Status: Not Started
Priority: High
Required Actions:
- Analyze both
InsuranceClaimmodels:patients/models.py(line 784+)billing/models.py(line 606+)
- Merge unique fields from patients app into billing app
- Move
ClaimDocumentandClaimStatusHistoryto billing app - Remove models from patients app
- Update all imports across the project
- Create data migration if needed
Estimated Impact:
- Major: Affects billing and patient management workflows
- Requires careful field mapping and data migration
- Multiple files will need import updates
📋 PENDING PHASES
Phase 2: Fix Inventory Responsibility Leaks
Status: Not Started
Priority: Medium
Identified Leaks:
pharmacy.Prescription- quantity fieldspharmacy.MedicationInventoryItem- already correct (linking table)pharmacy.DispenseRecord- quantity_remaining fieldoperating_theatre.EquipmentUsage- quantity_used fieldinsurance_approvals.InsuranceApprovalRequest- quantity fields (authorization, not stock)
Required Actions:
- Review and clarify purpose of quantity fields
- Ensure no stock tracking logic in clinical apps
- Add proper FK relationships to inventory app
- Create service layer methods for inventory operations
Phase 3: Move Encounter to Core
Status: Not Started
Priority: High (Breaking Change)
Required Actions:
- Copy
Encountermodel fromemr/models.pytocore/models.py - Copy
EncounterManagerto core - Update all ForeignKey references across apps:
- emr (VitalSigns, ProblemList, CarePlan, ClinicalNote, etc.)
- pharmacy (Prescription)
- laboratory (LabOrder)
- radiology (ImagingOrder, ImagingStudy)
- operating_theatre (SurgicalCase)
- billing (MedicalBill)
- Remove Encounter from EMR
- Create complex migration strategy
- Comprehensive testing required
Estimated Impact:
- Critical: Affects core data model
- Requires database migration
- All apps referencing Encounter need updates
Phase 4: Standardize All Models
Status: Not Started
Priority: Medium
Required Standard Fields:
tenant = models.ForeignKey('core.Tenant', ...)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, ...)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, ...)
is_active = models.BooleanField(default=True)
external_id = models.CharField(max_length=100, blank=True)
Apps Requiring Updates:
- All 20 apps need review
- Approximately 153 models to check
- Many models missing one or more standard fields
Phase 5: Centralized Documentation
Status: Partially Complete
Priority: High
Completed:
- ✅
documentation/app created - ✅
documentation/models.pywith Document, DocumentVersion, DocumentLink, DocumentAttachment - ✅
documentation/services.pywith core functions
Remaining:
- Remove duplicate note/report models from:
inpatients.DischargeSummary(narrative fields)radiology.RadiologyReport(entire model)operating_theatre.SurgicalNote(entire model)emr.ClinicalNote(link to documentation)
- Update models to link to
documentation.Document - Create DRF serializers and viewsets
- Add to
settings.pyLOCAL_APPS - Create migrations
- Update admin interfaces
Phase 6: Create Service Layers
Status: Partially Complete
Priority: Medium
Completed:
- ✅
inventory/services.pyexists - ✅
documentation/services.pyexists
Remaining:
- Complete
inventory/services.pywith all CRUD operations - Create
billing/services.pyfor claim management - Create service layers for other domains as needed
- Document service layer APIs
- Update views to use services instead of direct model access
📊 STATISTICS
Overall Progress
- Total Issues Identified: 12
- Issues Resolved: 3 (25%)
- Issues In Progress: 0
- Issues Pending: 9 (75%)
By Category
- Name Collisions: 3/4 resolved (75%)
- Inventory Leaks: 0/6 addressed (0%)
- Canonical Conflicts: 0/1 resolved (0%)
- Field Similarities: 0/1 addressed (0%)
🎯 NEXT STEPS
Immediate (Next Session)
- Complete Phase 1.4: Resolve InsuranceClaim collision
- Begin Phase 2: Address inventory responsibility leaks
- Plan Phase 3: Encounter migration strategy
Short Term (This Week)
- Complete all Phase 1 name collisions
- Address critical inventory leaks
- Finalize centralized documentation implementation
Medium Term (This Month)
- Move Encounter to core (breaking change)
- Standardize all models with required fields
- Complete service layer implementation
- Comprehensive testing
⚠️ RISKS & CONSIDERATIONS
High Risk Items
- Encounter Migration: Core model move affects many apps
- InsuranceClaim Merge: Complex field mapping required
- Database Migrations: May require drop-and-reseed for clean implementation
Mitigation Strategies
- Create comprehensive backup before major changes
- Implement changes in development environment first
- Create detailed migration scripts with rollback procedures
- Extensive testing after each phase
- Consider incremental deployment vs. drop-and-reseed
📝 NOTES
Design Decisions
- Keeping canonical models in their domain-specific apps
- Using imports rather than moving all models to core
- Maintaining backward compatibility where possible
- Prioritizing data integrity over convenience
Technical Debt
- Some models still lack proper tenancy support
- Inconsistent field naming across apps
- Missing audit trails in some models
- No consistent external_id implementation
🔗 RELATED DOCUMENTS
_refactor_report/modular_refactoring_report.md- Complete analysis and recommendations_refactor_report/overlaps.json- Detailed overlap detection results_refactor_report/model_map.json- Complete model inventorytenhal_detailed_refactor_plan.md- Step-by-step implementation guidetools/analyze_models.py- Analysis tool source code
Last Updated: 2025-10-06 16:44 AST
Updated By: Automated Refactoring Process