Sha256: e5bc221f0b16dc28c967ec5c1eb9e6ee182b616eec730a24f20870f2770a994c

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/feeds"

module Renalware
  module Feeds
    class PersistMessage
      # hl7_message is an HL7Message (a decorator around ::HL7::Message)
      # If the same message is persisted twice we'll get an ActiveRecord::RecordNotUnique error
      # but that's fine as we don't want to process the same HL7 message twice.
      def call(hl7_message)
        Message.create!(
          event_code: hl7_message.type,
          header_id: hl7_message.header_id,
          body: hl7_message.to_s,
          body_hash: Digest::MD5.hexdigest(hl7_message.to_s),
          patient_identifier: hl7_message.patient_identification&.internal_id
        )
      rescue ActiveRecord::RecordNotUnique => e
        # If a duplicate messages comes in (we have calculated the body_hash for the message and it
        # turns out that body_hash is not unique in the database, meaning the message is already
        # stored) then raise a custom error so it can be handled upstream - ie we can choose to
        # ignore it.
        raise Feeds::DuplicateMessageReceivedError, e.message
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
renalware-core-2.0.113 app/models/renalware/feeds/persist_message.rb
renalware-core-2.0.112 app/models/renalware/feeds/persist_message.rb
renalware-core-2.0.111 app/models/renalware/feeds/persist_message.rb