Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Health::PatientReferral#create_patient #4676

Draft
wants to merge 4 commits into
base: stable
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions app/models/health/patient_referral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,10 @@ def convert_to_patient

update(effective_date: Date.current)
# look for an existing patient
if Health::Patient.where(medicaid_id: medicaid_id).exists?
patient = Health::Patient.where(medicaid_id: medicaid_id).first
create_patient(patient.client)
patient = Health::Patient.with_deleted.find_by(medicaid_id: medicaid_id)

if patient.present?
create_patient(patient.client) # Create will update the existing client
else
source_client = create_source_client
destination_client = connect_destination_client(source_client)
Expand Down Expand Up @@ -464,13 +465,18 @@ def create_destination_client source_client
end

def create_patient destination_client
linked_patient = Health::Patient.with_deleted.find_by(client_id: destination_client.id)
patient = Health::Patient.with_deleted.where(medicaid_id: medicaid_id).first_or_initialize
patient_with_medicaid_id = Health::Patient.with_deleted.where(medicaid_id: medicaid_id).first_or_initialize
patient_attached_to_client = Health::Patient.with_deleted.find_by(client_id: destination_client.id)

# The medicaid id has changed, or points to a different client!
raise MedicaidIdConflict, "Patient: #{patient.client_id}, linked_patient: #{linked_patient.id}" if linked_patient.present? && patient.client_id != linked_patient.id
if patient_attached_to_client.present? && patient_with_medicaid_id.id != patient_attached_to_client.id
raise MedicaidIdConflict(
"Referral #{id}: Patient #{patient_with_medicaid_id.id} has associated Medicaid ID, " +
"but, patient #{patient_attached_to_client.id} matches by personal identifiers",
)
end

patient.assign_attributes(
# Update patient w/ data from referral
patient_with_medicaid_id.assign_attributes(
id_in_source: id,
first_name: first_name,
last_name: last_name,
Expand All @@ -481,11 +487,13 @@ def create_patient destination_client
pilot: false,
# engagement_date: engagement_date,
data_source_id: Health::DataSource.where(name: 'Patient Referral').pluck(:id).first,
deleted_at: nil,
deleted_at: nil, # Resurrect patient if previously deleted
)
patient.save!
patient.import_epic_team_members
update(patient_id: patient.id)
patient_with_medicaid_id.save!
patient_with_medicaid_id.import_epic_team_members

# Link patient to referral
update(patient_id: patient_with_medicaid_id.id)
end

def self.text_search(text)
Expand Down
Loading