Sha256: 5a8d8a6f0528c74ec159ec2640927b4c47203a097dadf801f650dbdecbfa3074

Contents?: true

Size: 1.75 KB

Versions: 27

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require "renalware/feeds"
require "attr_extras"

module Renalware
  module Patients
    module Ingestion
      class UpdateMasterPatientIndex
        pattr_initialize :hl7_message
        attr_reader :rw_patient
        delegate :patient_identification, to: :hl7_message

        def self.call(hl7_message)
          new(hl7_message).call
        end

        def call
          return unless hl7_message.adt?

          @rw_patient = find_patient_in_renalware
          update_or_create_abridged_patient
        end

        private

        def find_patient_in_renalware
          ::Renalware::Patient.find_by(
            local_patient_id: patient_identification.internal_id
          ) || NullObject.instance
        end

        # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
        def update_or_create_abridged_patient
          find_or_initialize_abridged_patient.update!(
            nhs_number: patient_identification.nhs_number,
            given_name: patient_identification.given_name,
            family_name: patient_identification.family_name,
            sex: patient_identification.sex,
            title: patient_identification.title,
            suffix: patient_identification.suffix,
            born_on: patient_identification.born_on,
            died_at: patient_identification.died_at,
            patient_id: rw_patient.id,
            practice_code: hl7_message.practice_code,
            gp_code: hl7_message.gp_code
          )
        end
        # rubocop:enable Metrics/AbcSize, Metrics/MethodLength

        def find_or_initialize_abridged_patient
          Patients::Abridgement.find_or_initialize_by(
            hospital_number: patient_identification.internal_id
          )
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
renalware-core-2.0.149 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.148 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.147 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.146 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.145 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.144 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.143 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.142 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.141 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.140 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.139 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.138 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.137 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.136 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.135 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.134 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.133 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.132 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.131 app/models/renalware/patients/ingestion/update_master_patient_index.rb
renalware-core-2.0.130 app/models/renalware/patients/ingestion/update_master_patient_index.rb