Class: Lab::Lims::Api::RestApi

Inherits:
Object
  • Object
show all
Defined in:
app/services/lab/lims/api/rest_api.rb

Defined Under Namespace

Classes: AuthenticationTokenExpired, InvalidParameters, LimsApiError

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ RestApi

Returns a new instance of RestApi.



11
12
13
# File 'app/services/lab/lims/api/rest_api.rb', line 11

def initialize(config)
  @config = config
end

Instance Method Details

#acknowledge(acknowledgement_dto) ⇒ Object



36
37
38
39
40
41
42
43
# File 'app/services/lab/lims/api/rest_api.rb', line 36

def acknowledge(acknowledgement_dto)
  Rails.logger.info("Acknowledging order ##{acknowledgement_dto} in LIMS")
  response = in_authenticated_session do |headers|
    RestClient.post(expand_uri('/acknowledge/test/results/recipient'), acknowledgement_dto, headers)
  end
  Rails.logger.info("Acknowledged order ##{acknowledgement_dto} in LIMS. Response: #{response}")
  JSON.parse(response)
end

#consume_orders(*_args, patient_id: nil, **_kwargs) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/lab/lims/api/rest_api.rb', line 55

def consume_orders(*_args, patient_id: nil, **_kwargs)
  orders_pending_updates(patient_id).each do |order|
    order_dto = Lab::Lims::OrderSerializer.serialize_order(order)

    if order_dto['priority'].nil? || order_dto['sample_type'].casecmp?('not_specified')
      patch_order_dto_with_lims_order!(order_dto, find_lims_order(order.accession_number))
    end

    if order_dto['test_results'].empty?
      begin
        patch_order_dto_with_lims_results!(order_dto, find_lims_results(order.accession_number))
      rescue InvalidParameters => e # LIMS responds with a 401 when a result is not found :(
        Rails.logger.error("Failed to fetch results for ##{order.accession_number}: #{e.message}")
      end
    end

    yield order_dto, OpenStruct.new(last_seq: 0)
  rescue LimsApiError => e
    Rails.logger.error("Failed to fetch updates for ##{order.accession_number}: #{e.class} - #{e.message}")
    sleep(1)
  end
end

#create_order(order_dto) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/lab/lims/api/rest_api.rb', line 15

def create_order(order_dto)
  response = in_authenticated_session do |headers|
    Rails.logger.info("Pushing order ##{order_dto[:tracking_number]} to LIMS")

    if order_dto['sample_type'].casecmp?('not_specified')
      RestClient.post(expand_uri('request_order', api_version: 'v2'), make_create_params(order_dto), headers)
    else
      RestClient.post(expand_uri('create_order'), make_create_params(order_dto), headers)
    end
  end

  data = JSON.parse(response.body)
  update_order_results(order_dto) unless data['message'].casecmp?('Order already available')

  ActiveSupport::HashWithIndifferentAccess.new(
    id: order_dto.fetch(:_id, order_dto[:tracking_number]),
    rev: 0,
    tracking_number: order_dto[:tracking_number]
  )
end

#delete_order(_id, order_dto) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/lab/lims/api/rest_api.rb', line 78

def delete_order(_id, order_dto)
  tracking_number = order_dto.fetch('tracking_number')

  order_dto['tests'].each do |test|
    Rails.logger.info("Voiding test '#{test}' (#{tracking_number}) in LIMS")
    in_authenticated_session do |headers|
      date_voided, voided_status = find_test_status(order_dto, test, 'Voided')
      params = make_void_test_params(tracking_number, test, voided_status['updated_by'], date_voided)
      RestClient.post(expand_uri('update_test'), params, headers)
    end
  end
end

#update_order(_id, order_dto) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/services/lab/lims/api/rest_api.rb', line 45

def update_order(_id, order_dto)
  in_authenticated_session do |headers|
    RestClient.post(expand_uri('update_order'), make_update_params(order_dto), headers)
  end

  update_order_results(order_dto)

  { tracking_number: order_dto[:tracking_number] }
end

#verify_tracking_number(tracking_number) ⇒ Object



91
92
93
94
95
96
# File 'app/services/lab/lims/api/rest_api.rb', line 91

def verify_tracking_number(tracking_number)
  find_lims_order(tracking_number)
rescue InvalidParameters => e
  Rails.logger.error("Failed to verify tracking number #{tracking_number}: #{e.message}")
  false
end