Class: Lab::Lims::PullWorker

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
app/services/lab/lims/pull_worker.rb

Overview

Pulls orders from a Lims API object and saves them to the local database.

Direct Known Subclasses

Migrator::MigrationWorker

Constant Summary collapse

LIMS_LOG_PATH =
Rails.root.join('log', 'lims')

Constants included from Utils

Utils::TEST_NAME_MAPPINGS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

find_concept_by_name, lab_user, #logger, parse_date, structify, translate_test_name

Constructor Details

#initialize(lims_api) ⇒ PullWorker

Returns a new instance of PullWorker.



14
15
16
# File 'app/services/lab/lims/pull_worker.rb', line 14

def initialize(lims_api)
  @lims_api = lims_api
end

Instance Attribute Details

#lims_apiObject (readonly)

Returns the value of attribute lims_api.



8
9
10
# File 'app/services/lab/lims/pull_worker.rb', line 8

def lims_api
  @lims_api
end

Instance Method Details

#process_order(order_dto) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/services/lab/lims/pull_worker.rb', line 60

def process_order(order_dto)
  patient = find_patient_by_nhid(order_dto[:patient][:id])
  unless patient
    logger.debug("Discarding order: Non local patient ##{order_dto[:patient][:id]} on order ##{order_dto[:tracking_number]}")
    order_rejected(order_dto, "Patient NPID, '#{order_dto[:patient][:id]}', didn't match any local NPIDs")
    return
  end

  if order_dto[:tests].empty?
    logger.debug("Discarding order: Missing tests on order ##{order_dto[:tracking_number]}")
    order_rejected(order_dto, 'Order is missing tests')
    return
  end

  diff = match_patient_demographics(patient, order_dto['patient'])
  if diff.empty?
    save_order(patient, order_dto)
    order_saved(order_dto)
  else
    save_failed_import(order_dto, 'Demographics not matching', diff)
  end
end

#pull_orders(batch_size: 10_000, **kwargs) ⇒ Object

Pulls orders from the LIMS queue and writes them to the local database



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/lab/lims/pull_worker.rb', line 20

def pull_orders(batch_size: 10_000, **kwargs)
  logger.info("Retrieving LIMS orders starting from #{last_seq}")

  lims_api.consume_orders(from: last_seq, limit: batch_size, **kwargs) do |order_dto, context|
    logger.debug("Retrieved order ##{order_dto[:tracking_number]}: #{order_dto}")

    patient = find_patient_by_nhid(order_dto[:patient][:id])
    unless patient
      logger.debug("Discarding order: Non local patient ##{order_dto[:patient][:id]} on order ##{order_dto[:tracking_number]}")
      order_rejected(order_dto, "Patient NPID, '#{order_dto[:patient][:id]}', didn't match any local NPIDs")
      next
    end

    if order_dto[:tests].empty?
      logger.debug("Discarding order: Missing tests on order ##{order_dto[:tracking_number]}")
      order_rejected(order_dto, 'Order is missing tests')
      next
    end

    diff = match_patient_demographics(patient, order_dto['patient'])
    if diff.empty?
      save_order(patient, order_dto)
      order_saved(order_dto)
    else
      save_failed_import(order_dto, 'Demographics not matching', diff)
    end

    update_last_seq(context.current_seq)
  rescue Lab::Lims::DuplicateNHID
    logger.warn("Failed to import order due to duplicate patient NHID: #{order_dto[:patient][:id]}")
    save_failed_import(order_dto, "Duplicate local patient NHID: #{order_dto[:patient][:id]}")
  rescue MissingAccessionNumber
    logger.warn("Failed to import order due to missing accession number: #{order_dto[:_id]}")
    save_failed_import(order_dto, 'Order missing tracking number')
  rescue LimsException => e
    logger.warn("Failed to import order due to #{e.class} - #{e.message}")
    save_failed_import(order_dto, e.message)
  end
end