hospital-management/tools/markdown/PHASE4_MODEL_CHANGES_SUMMARY.md
Marwan Alwali 263292f6be update
2025-11-04 00:50:06 +03:00

8.3 KiB

Phase 4: Core Consolidation - Model Changes Summary

Date: 2025-10-06

This document summarizes all the model changes and fixes made during Phase 4 of the core consolidation effort.


1. Core Models (core/models.py)

New Models Created:

  1. Tenant - Multi-tenant support
  2. Patient - Core patient identity (replaces PatientProfile as primary)
  3. Encounter - Clinical encounters
  4. Facility - Healthcare facilities
  5. AuditEvent - Audit logging (renamed from AuditLogEntry)
  6. Attachment - File attachments

Key Changes:

  • All models now inherit from TenantScopedModel for multi-tenancy
  • Patient model uses FHIR-aligned field names:
    • given_name instead of first_name
    • family_name instead of last_name
    • birth_date instead of date_of_birth
    • administrative_gender instead of gender
  • Added external_id field to all models for integration
  • Standardized timestamp fields: created_at, updated_at
  • Standardized user tracking: created_by, updated_by

2. Patients App (patients/models.py)

Model Structure Changes:

  1. PatientProfile - Now extends core.Patient via OneToOneField

    • Uses patient as primary key (OneToOneField to core.Patient)
    • Removed fields that now exist in core.Patient:
      • first_name, last_name, date_of_birth, gender, id_number
    • Kept extended demographics:
      • Contact info, address, marital status, language preferences
      • Healthcare info, allergies, advance directives
      • VIP and confidential flags
  2. EmergencyContact - Links to core.Patient

    • Changed FK from PatientProfile to core.Patient
    • Maintains all contact and authorization fields
  3. InsuranceInfo - Links to core.Patient

    • Changed FK from PatientProfile to core.Patient
    • Removed non-existent field: subscriber_id_number
    • Kept all insurance-related fields
  4. ConsentTemplate - No structural changes

    • Inherits from TenantScopedModel
  5. ConsentForm - Links to core.Patient

    • Changed FK from PatientProfile to core.Patient
    • Maintains all consent and signature fields
  6. PatientNote - Links to core.Patient

    • Changed FK from PatientProfile to core.Patient
    • Maintains all note fields

3. EMR App (emr/models.py)

  • ClinicalNote
  • Vital
  • Diagnosis
  • Procedure
  • Allergy
  • Problem
  • Observation
  • CarePlan

Key Changes:

  • All ForeignKey relationships changed from patients.PatientProfile to core.Patient
  • All models inherit from TenantScopedModel
  • Standardized field names and audit fields

4. Forms Updates

patients/forms.py:

  1. PatientProfileForm

    • Removed fields that don't exist in PatientProfile:
      • first_name, last_name, date_of_birth, gender, id_number
    • Only includes PatientProfile-specific fields:
      • Extended demographics, contact info, healthcare preferences
  2. InsuranceInfoForm

    • Removed non-existent field: subscriber_id_number
    • Added is_primary field
    • Excluded patient field (set programmatically)
  3. EmergencyContactForm

    • Excluded patient field (set programmatically)
  4. PatientNoteForm

    • Excluded patient field (set programmatically)
  5. PatientsSearchForm

    • Commented out gender field (now in core.Patient, not PatientProfile)

5. Views Updates

patients/views.py:

  • Changed import from AuditLogEntry to AuditEvent
  • All views updated to work with new model structure
  • Patient creation/update views need to handle both core.Patient and PatientProfile

core/views.py:

  • Updated to use AuditEvent instead of AuditLogEntry

core/forms.py:

  • TenantForm - Removed non-existent fields:
    • contact_person, contact_email, contact_phone
    • subscription_plan, subscription_status

6. Utils Updates

core/utils.py:

  • AuditLogger class updated to use AuditEvent model
  • All logging methods updated to match new model structure

7. Blood Bank App

blood_bank/forms.py:

  • Commented out non-existent models:
    • InventoryLocationForm (InventoryLocation model doesn't exist)
    • QualityControlForm (QualityControl model doesn't exist)
  • Removed imports for these models

blood_bank/views.py:

  • Commented out views that reference non-existent models:
    • inventory_overview (uses InventoryLocation)
    • quality_control_list (uses QualityControl)
    • quality_control_create (uses QualityControl)
  • Removed imports for these models

8. Database Migration Status

Current State:

  • All migration directories have been cleared
  • Database file (db.sqlite3) has been deleted
  • Ready for fresh migrations once all fixes are verified

Next Steps (When Ready):

  1. Run python manage.py makemigrations to create initial migrations
  2. Run python manage.py migrate to apply migrations
  3. Reseed data using updated data generation scripts

9. Key Architectural Changes

Multi-Tenancy:

  • All models now properly scoped to tenants
  • TenantScopedModel base class provides:
    • tenant ForeignKey
    • is_active flag
    • created_at, updated_at timestamps
    • created_by, updated_by user tracking
    • external_id for integration

Patient Identity:

  • core.Patient is now the canonical patient identity
  • patients.PatientProfile extends Patient with healthcare-specific demographics
  • All clinical and operational models link to core.Patient

Audit Trail:

  • Renamed AuditLogEntry to AuditEvent for clarity
  • Standardized audit logging across all apps
  • AuditLogger utility class provides consistent logging interface

10. Breaking Changes

For Existing Code:

  1. Patient References: Change from PatientProfile to Patient in:

    • Foreign keys
    • Queries
    • View logic
  2. Field Name Changes: Update references to:

    • first_namegiven_name
    • last_namefamily_name
    • date_of_birthbirth_date
    • genderadministrative_gender
  3. Audit Logging: Change from AuditLogEntry to AuditEvent

  4. Forms: Update form handling for split Patient/PatientProfile model


11. Files Modified

Core App:

  • core/models.py - Complete rewrite with new models
  • core/forms.py - Updated TenantForm
  • core/views.py - Updated to use AuditEvent
  • core/utils.py - Updated AuditLogger

Patients App:

  • patients/models.py - Restructured to extend core.Patient
  • patients/forms.py - Updated all forms for new structure
  • patients/views.py - Updated imports and logic

EMR App:

  • emr/models.py - Updated all FK relationships to core.Patient

Blood Bank App:

  • blood_bank/forms.py - Commented out non-existent model forms
  • blood_bank/views.py - Commented out views for non-existent models

Other Apps:

  • All apps updated to use core.Patient instead of patients.PatientProfile
  • All apps updated to use AuditEvent instead of AuditLogEntry

12. Testing Checklist

Before running migrations, verify:

  • All imports reference correct models
  • All ForeignKey relationships point to correct models
  • All forms only reference fields that exist in their models
  • All views handle the Patient/PatientProfile split correctly
  • All audit logging uses AuditEvent
  • No references to deleted/non-existent models

13. Data Migration Considerations

When reseeding data:

  1. Create Tenant first
  2. Create core.Patient records
  3. Create PatientProfile records (linked to Patient)
  4. Create related records (EmergencyContact, InsuranceInfo, etc.)
  5. Ensure all tenant scoping is correct
  6. Verify audit trails are created properly

Summary

This phase successfully consolidated patient identity into core.Patient while maintaining extended demographics in patients.PatientProfile. All apps have been updated to use the new structure, and the codebase is ready for fresh migrations once all fixes are verified.

The key achievement is a cleaner separation of concerns:

  • core.Patient: Canonical patient identity (FHIR-aligned)
  • patients.PatientProfile: Extended healthcare demographics
  • Clinical apps: All link to core.Patient for consistency

This structure provides a solid foundation for:

  • Multi-tenant operations
  • FHIR compliance
  • Integration with external systems
  • Comprehensive audit trails
  • Scalable architecture