docx icon indicating copy to clipboard operation
docx copied to clipboard

Failing on Redhat

Open antarr opened this issue 1 year ago • 0 comments

Describe the bug

My code works on a mac but I'm getting a lot of failures when running in RedHat 8. Are there any dependencies I need to consider when running on linux?

app/models/concerns/attachable.rb

module Attachable
  extend ActiveSupport::Concern

  included do
    mount_uploader :attachment, AttachmentUploader
  end

  class_methods do
    def create_from_attachment(attachment)
      patient_data = read_data_from_file(attachment)

      mrn, _encounter_number = patient_data[:mrn].split('-')

      patient = Patient.find_by_mrn(mrn)
      patient ||= create_patient(patient_data)

      new_attacheble = find_by(encounter_number: patient_data[:mrn])
      new_attacheble ||= new

      new_attacheble.patient = patient
      new_attacheble.clinical_history = patient_data[:history]
      new_attacheble.report_date = patient_data[:report_date]
      new_attacheble.admission_date = patient_data[:report_date]
      new_attacheble.meg_impression = patient_data[:meg_impression]
      new_attacheble.eeg_results = patient_data[:eeg_results]
      new_attacheble.encounter_number = patient_data[:mrn]
      new_attacheble.attachment = attachment
      if patient_data[:referring_physician].present?
        first_name = patient_data[:referring_physician].split[0]
        last_name = patient_data[:referring_physician].split[1].split(',').first
        referring_physician = ReferringPhysician.find_by(first_name: first_name, last_name: last_name)
        new_attacheble.referring_physician_id = referring_physician.id if referring_physician
      end
      new_attacheble.save

      patient_data[:reported_by].each do |reported_by|
        name = reported_by.split(',').first
        first_name = name.split.first
        last_name = name.split.last
        user = User.find_by(user_firstname: first_name, user_lastname: last_name)
        next unless user

        new_attacheble.clinical_attendings << user if user.clinical_attending?
        new_attacheble.clinical_fellows << user if user.clinical_fellow?
        unless new_attacheble.save
          raise "Error saving new attacheble"
        else
          new_attacheble.sign(user)
        end
      end

      Rails.logger.debug '-----------------New Attacheble-----------------'
      Rails.logger.debug JSON.pretty_generate(new_attacheble.as_json)
      new_attacheble
    end

    private

    def create_patient(patient_data)
      patient = Patient.new
      patient.first_name = patient_data[:first_name]
      patient.middle_name = patient_data[:middle_name]
      patient.last_name = patient_data[:last_name]
      patient.medical_record_number = patient_data[:mrn].split('-').first
      patient.date_of_birth = patient_data[:patient_dob]
      patient.gender = 'Female' if patient_data[:history].include?(' female ')
      patient.gender ||= 'Male' if patient_data[:history].include?(' male ')
      patient.save

      Rails.logger.debug '-----------------Patient-----------------'
      Rails.logger.debug JSON.pretty_generate(patient.as_json)

      patient
    end

    def read_data_from_file(attachment)
      data = Hash.new('')
      data[:reported_by] = []

      doc = Docx::Document.open(attachment.path)
      if doc.inspect.include?("relationships/image")
        raise 'Attachment contains image'
      end

      unless doc.paragraphs.any? { |p| p.text.include?('Patient Name') }
        raise 'Attachment does not contain patient name'
      end
      looking_at_history = false
      looking_at_reported_by = false
      looking_at_meg_impression = false
      looking_at_eeg_results = false
      looking_at_meg_results = false

      doc.paragraphs.each do |p|
        data[:patient_name] = extract_value(p.text) if p.text.include?('Patient Name')
        data[:mrn] = extract_value(p.text) if p.text.include?('MRN')
        data[:patient_dob] = extract_value(p.text) if p.text.include?('DOB')
        data[:report_date] = extract_value(p.text) if p.text.include?('Date of Exam')
        data[:referring_physician] = extract_value(p.text) if p.text.include?('Referring Physician')
        if looking_at_history
          data[:history] = p.text
          looking_at_history = false
        end

        if p.text.include?('INTRODUCTION')
          looking_at_history = true
          looking_at_reported_by = false
        end

        if p.text.include?('MEG RESULTS')
          looking_at_meg_results = true
          looking_at_reported_by = false
          looking_at_history = false
          next
        end

        if p.text.include?('EEG RESULTS')
          looking_at_eeg_results = true
          looking_at_meg_impression = false
          looking_at_reported_by = false
          looking_at_history = false
          next
        end

        if p.text.include?('MEG AND EEG IMPRESSION')
          looking_at_meg_impression = true
          looking_at_reported_by = false
          looking_at_history = false
          looking_at_eeg_results = false
          next
        end

        if p.text.include?('There is a picture set that is part of this MEG report.')
          looking_at_meg_impression = false
          looking_at_reported_by = false
          looking_at_history = false
          next
        end

        if looking_at_reported_by
          data[:reported_by] << p.text if p.text.present?
        end

        if looking_at_meg_impression
          data[:meg_impression] << ("\n#{p.text}") if p.text.present?
        end

        if looking_at_eeg_results
          data[:eeg_results] << ("\n#{p.text}") if p.text.present?
        end

        if looking_at_meg_results
          data[:meg_results] << ("\n#{p.text}") if p.text.present?
        end

        looking_at_reported_by = true if p.text.include?('Reported By')
      end

      name_parts = data[:patient_name].split(',')
      first_name_parts = name_parts.first.split
      data[:last_name] = name_parts.first.strip
      data[:middle_name] = first_name_parts[1..-1].join(' ')
      data[:first_name] = first_name_parts.first

      data
    end

    def extract_value(string)
      string.split(':').last.strip
    end
  end
end

The error occurs here when the patient has no middle name

I.E Smith, Chris vs Smith, Chris Michael

      name_parts = data[:patient_name].split(',')
      first_name_parts = name_parts.first.split
      data[:last_name] = name_parts.first.strip
      data[:middle_name] = first_name_parts[1..-1].join(' ')
      data[:first_name] = first_name_parts.first

Expected behavior

A clear and concise description of what you expected to happen.

Environment

  • Ruby version: 2.7.7
  • docx gem version: [0.8.0]
  • OS: [RedHat 8.x]

antarr avatar Oct 09 '24 15:10 antarr