Skip to content

Commit

Permalink
Shifting to Resource Migration (#2765)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacobjeevan authored Jan 21, 2025
1 parent 51ddbf4 commit 57b91bf
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions care/facility/migrations/0485_migrate_shifting_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Generated by Django 5.1.4 on 2025-01-21 08:07

from django.db import migrations
import logging

logger = logging.getLogger(__name__)


def map_status(old_status):
# Map from ShiftingRequest status to ResourceRequest status
status_map = {
10: "pending", # PENDING
15: "pending", # ON HOLD
20: "approved", # APPROVED
30: "rejected", # REJECTED
40: "approved", # DESTINATION APPROVED
50: "rejected", # DESTINATION REJECTED
55: "transportation_to_be_arranged", # TRANSPORTATION TO BE ARRANGED
60: "transfer_in_progress", # PATIENT TO BE PICKED UP
70: "transfer_in_progress", # TRANSFER IN PROGRESS
80: "completed", # COMPLETED
90: "cancelled", # PATIENT EXPIRED
100: "cancelled", # CANCELLED
}
return status_map.get(old_status, "completed")


def migrate_shifting_to_resource(apps, schema_editor):
ShiftingRequest = apps.get_model("facility", "ShiftingRequest")
ResourceRequest = apps.get_model("emr", "ResourceRequest")
Patient = apps.get_model("emr", "Patient")

for shifting in ShiftingRequest.objects.filter(deleted=False).select_related('patient'):
# Store extra fields in meta
meta = {
"is_up_shift": shifting.is_up_shift,
"vehicle_preference": shifting.vehicle_preference,
"comments": shifting.comments,
"breathlessness_level": shifting.breathlessness_level,
"assigned_facility_external": shifting.assigned_facility_external,
"ambulance_driver_name": shifting.ambulance_driver_name,
"ambulance_number": shifting.ambulance_number,
"ambulance_phone_number": shifting.ambulance_phone_number,
"is_kasp": shifting.is_kasp,
"assigned_facility_type": shifting.assigned_facility_type,
"preferred_vehicle_choice": shifting.preferred_vehicle_choice,
}

# Get the new Patient model instance using migrated_emr_patient_id
patient = None
if shifting.patient and shifting.patient.migrated_emr_patient_id:
try:
patient = Patient.objects.get(id=shifting.patient.migrated_emr_patient_id)
except Patient.DoesNotExist:
logger.warning(f"Could not find migrated patient for shifting request {shifting.id}")

ResourceRequest.objects.create(
external_id=shifting.external_id,
origin_facility=shifting.origin_facility,
approving_facility=shifting.shifting_approving_facility,
assigned_facility=shifting.assigned_facility,
emergency=shifting.emergency,
title=f"Patient Shifting Request - {shifting.patient.name if shifting.patient else 'Unknown'}",
reason=shifting.reason,
referring_facility_contact_name=shifting.refering_facility_contact_name,
referring_facility_contact_number=shifting.refering_facility_contact_number,
status=map_status(shifting.status),
category="patient_care", # Default category for shifting requests
priority=1 if shifting.emergency else 2, # Set priority based on emergency
is_assigned_to_user=shifting.is_assigned_to_user,
assigned_to=shifting.assigned_to,
related_patient=patient,
created_by=shifting.created_by,
updated_by=shifting.last_edited_by,
created_date=shifting.created_date,
modified_date=shifting.modified_date,
deleted=shifting.deleted,
meta=meta,
)


def reverse_migrate(apps, schema_editor):
ResourceRequest = apps.get_model("emr", "ResourceRequest")
ResourceRequest.objects.filter(category="patient_care").delete()


class Migration(migrations.Migration):
dependencies = [
('facility', '0484_migrate_symptoms'),
]

operations = [
migrations.RunPython(migrate_shifting_to_resource, reverse_migrate),
]

0 comments on commit 57b91bf

Please sign in to comment.