his_emr_api_lab 1.1.2 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2202cc31522b4ae92847b3102841cfcf0c65a9e389fde480e3e787f1ef49ec7
4
- data.tar.gz: a82291c66cdd6fb651e0917dc68e5b28b6dcedeb124973ab57f9f60187b4c920
3
+ metadata.gz: ec3adbde37c8f4213c049a83371cd30deb35a4833e3f95d5ace57bf9e2344e02
4
+ data.tar.gz: 2b676b2060b642493906794747160939e9d5cec15e94f87cb35e486434e4a7cd
5
5
  SHA512:
6
- metadata.gz: 7da2c231dba9fa4b013890becd128b0a8694fe797b510e9baa37638d1437133cc11e27e2479938eda921ccfe6c23583544092cb477372b5db298922913035c72
7
- data.tar.gz: cb8fbf9e9c71d187e0085b42f0fe5299976dc4309d672c815c231cdb096548d9b53b9b0f222f453b66a41a8d18b759f2d8d295c46c899206d7c7ffcee500bb6c
6
+ metadata.gz: 232dbd10ff1f15f18432d3421e99ecce64ab5855c30fc0d6f028bfe3cce291f29d5954453c0f7bc6f9d9c27a5990ac16299677721188d4d0b981734ba23d283f
7
+ data.tar.gz: 2f823d4b6edc2494f405002dc6681eb895ec7e961c817dd7c02b6688e286edf9a72b9467651d39d743d74a4bee094a4590150020002c3028a69d8534696af8de
@@ -38,6 +38,9 @@ module Lab
38
38
  end
39
39
 
40
40
  def create_order(order)
41
+ order = order.dup
42
+ order.delete('_id')
43
+
41
44
  bum.couch_rest :post, '/', order
42
45
  end
43
46
 
@@ -59,6 +59,9 @@ class Lab::Lims::Api::RestApi
59
59
  end
60
60
 
61
61
  yield order_dto, OpenStruct.new(last_seq: 0)
62
+ rescue LimsApiError => e
63
+ Rails.logger.error("Failed to fetch updates for ##{order.accession_number}: #{e.class} - #{e.message}")
64
+ sleep(1)
62
65
  end
63
66
  end
64
67
 
@@ -11,15 +11,19 @@ module Lab
11
11
  class ConfigNotFound < RuntimeError; end
12
12
 
13
13
  class << self
14
+ def preferred_api
15
+ emr_api_application('lims_api', 'couchdb')
16
+ end
17
+
14
18
  ##
15
19
  # Returns LIMS' couchdb configuration file for the current environment (Rails.env)
16
20
  def couchdb
17
21
  config_path = begin
18
- find_config_path('couchdb.yml')
19
- rescue ConfigNotFound => e
20
- Rails.logger.error("Failed to find default LIMS couchdb config: #{e.message}")
21
- find_config_path('couchdb-lims.yml') # This can be placed in HIS-EMR-API/config
22
- end
22
+ find_config_path('couchdb.yml')
23
+ rescue ConfigNotFound => e
24
+ Rails.logger.error("Failed to find default LIMS couchdb config: #{e.message}")
25
+ find_config_path('couchdb-lims.yml') # This can be placed in HIS-EMR-API/config
26
+ end
23
27
 
24
28
  Rails.logger.debug("Using LIMS couchdb config: #{config_path}")
25
29
 
@@ -64,9 +68,7 @@ module Lab
64
68
  @emr_api_application ||= YAML.load_file(Rails.root.join('config', 'application.yml'))
65
69
 
66
70
  @emr_api_application.fetch(param) do
67
- unless fallback
68
- raise ConfigNotFound, "Missing config param: #{param}"
69
- end
71
+ raise ConfigNotFound, "Missing config param: #{param}" unless fallback
70
72
 
71
73
  fallback
72
74
  end
@@ -83,9 +85,7 @@ module Lab
83
85
  Rails.root.parent.join("nlims_controller/config/#{filename}")
84
86
  ]
85
87
 
86
- if filename == 'couchdb.yml'
87
- paths = [Rails.root.join('config/lims-couchdb.yml'), *paths]
88
- end
88
+ paths = [Rails.root.join('config/lims-couchdb.yml'), *paths] if filename == 'couchdb.yml'
89
89
 
90
90
  paths.each do |path|
91
91
  Rails.logger.debug("Looking for LIMS couchdb config at: #{path}")
@@ -69,9 +69,7 @@ module Lab
69
69
  end
70
70
 
71
71
  def start_date
72
- if self['date_created'].blank?
73
- raise LimsException, 'Order missing created date'
74
- end
72
+ raise LimsException, 'Order missing created date' if self['date_created'].blank?
75
73
 
76
74
  Utils.parse_date(self['date_created'])
77
75
  end
@@ -85,7 +83,7 @@ module Lab
85
83
 
86
84
  # Translates a LIMS sample priority to a concept_id
87
85
  def reason_for_test
88
- return unknown_concept.concept_id unless self['priority']
86
+ return nil unless self['priority']
89
87
 
90
88
  name = case self['priority']
91
89
  when %r{Reapet / Missing}i then 'Repeat / Missing'
@@ -64,7 +64,7 @@ module Lab
64
64
  first_name: name&.given_name,
65
65
  last_name: name&.family_name,
66
66
  id: national_id&.identifier,
67
- phone_number: phone_number&.value,
67
+ phone_number: phone_number&.value || 'Unknown',
68
68
  gender: person.gender,
69
69
  email: nil
70
70
  }
@@ -83,9 +83,7 @@ module Lab
83
83
  end
84
84
 
85
85
  def format_sample_status_trail(order)
86
- if order.concept_id == ConceptName.find_by_name!('Unknown').concept_id
87
- return []
88
- end
86
+ return [] if order.concept_id == ConceptName.find_by_name!('Unknown').concept_id
89
87
 
90
88
  user = User.find(order.discontinued_by || order.creator)
91
89
  drawn_by = PersonName.find_by_person_id(user.user_id)
@@ -17,7 +17,12 @@ module Lab
17
17
  loop do
18
18
  logger.info('Looking for new orders to push to LIMS...')
19
19
  orders = orders_pending_sync(batch_size).all
20
- orders.each { |order| push_order(order) }
20
+ orders.each do |order|
21
+ push_order(order)
22
+ rescue GatewayError => e
23
+ logger.error("Failed to push order ##{order.accession_number}: #{e.class} - #{e.message}")
24
+ sleep(Lab::Lims::Config.updates_poll_frequency)
25
+ end
21
26
 
22
27
  # Doing this after .each above to stop ActiveRecord from executing
23
28
  # an extra request to the database (ActiveRecord's lazy evaluation
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'logger_multiplexor'
4
4
 
5
+ require_relative './api/couchdb_api'
6
+
5
7
  module Lab
6
8
  module Lims
7
9
  ##
@@ -19,8 +21,7 @@ module Lab
19
21
 
20
22
  def self.start_push_worker
21
23
  start_worker('push_worker') do
22
- api = Lims::Api::RestApi.new(Lab::Lims::Config.rest_api)
23
- worker = PushWorker.new(api)
24
+ worker = PushWorker.new(lims_api)
24
25
 
25
26
  worker.push_orders # (wait: true)
26
27
  end
@@ -28,8 +29,7 @@ module Lab
28
29
 
29
30
  def self.start_pull_worker
30
31
  start_worker('pull_worker') do
31
- api = Lims::Api::RestApi.new(Lab::Lims::Config.rest_api)
32
- worker = PullWorker.new(api)
32
+ worker = PullWorker.new(lims_api)
33
33
 
34
34
  worker.pull_orders
35
35
  end
@@ -37,8 +37,7 @@ module Lab
37
37
 
38
38
  def self.start_realtime_pull_worker
39
39
  start_worker('realtime_pull_worker') do
40
- api = Lims::Api::WsApi.new(Lab::Lims::Config.updates_socket)
41
- worker = PullWorker.new(api)
40
+ worker = PullWorker.new(Lims::Api::WsApi.new(Lab::Lims::Config.updates_socket))
42
41
 
43
42
  worker.pull_orders
44
43
  end
@@ -71,6 +70,14 @@ module Lab
71
70
  Rails.logger.warn("Check for realtime updates failed: #{e.message}")
72
71
  false
73
72
  end
73
+
74
+ def self.lims_api
75
+ case Lims::Config.preferred_api
76
+ when /couchdb/i then Api::CouchDbApi.new(config: Lab::Lims::Config.couchdb)
77
+ when /rest/i then Api::RestApi.new(Lab::Lims::Config.rest_api)
78
+ else raise "Invalid LIMS API in application.yml, expected 'rest' or 'couchdb'"
79
+ end
80
+ end
74
81
  end
75
82
  end
76
83
  end
@@ -66,9 +66,7 @@ module Lab
66
66
 
67
67
  def update_order(order_id, params)
68
68
  specimen_id = params.dig(:specimen, :concept_id)
69
- unless specimen_id
70
- raise ::InvalidParameterError, 'Specimen concept_id is required'
71
- end
69
+ raise ::InvalidParameterError, 'Specimen concept_id is required' unless specimen_id
72
70
 
73
71
  order = Lab::LabOrder.find(order_id)
74
72
  if order.concept_id != unknown_concept_id && !params[:force_update]&.casecmp?('true')
@@ -84,7 +82,7 @@ module Lab
84
82
  discontinued_reason_non_coded: 'Sample drawn/updated')
85
83
  end
86
84
 
87
- if params.key?(:reason_for_test_id)
85
+ if params[:reason_for_test_id]
88
86
  Rails.logger.debug("Updating reason for test on order ##{order.order_id}")
89
87
  update_reason_for_test(order, params[:reason_for_test_id])
90
88
  end
@@ -116,13 +114,9 @@ module Lab
116
114
  # a 'Lab' encounter is created using the provided program_id and
117
115
  # patient_id.
118
116
  def find_encounter(order_params)
119
- if order_params[:encounter_id]
120
- return Encounter.find(order_params[:encounter_id])
121
- end
117
+ return Encounter.find(order_params[:encounter_id]) if order_params[:encounter_id]
122
118
 
123
- unless order_params[:patient_id]
124
- raise InvalidParameterError, 'encounter_id or patient_id required'
125
- end
119
+ raise InvalidParameterError, 'encounter_id or patient_id required' unless order_params[:patient_id]
126
120
 
127
121
  program_id = order_params[:program_id] || Program.find_by_name!(Lab::Metadata::LAB_PROGRAM_NAME).program_id
128
122
 
@@ -203,17 +197,13 @@ module Lab
203
197
  end
204
198
 
205
199
  def update_reason_for_test(order, concept_id)
206
- if concept_id.blank?
207
- raise InvalidParameterError, "Reason for test can't be blank"
208
- end
200
+ raise InvalidParameterError, "Reason for test can't be blank" if concept_id.blank?
209
201
 
210
202
  return if order.reason_for_test&.value_coded == concept_id
211
203
 
212
- unless order.reason_for_test&.value_coded.nil?
213
- raise InvalidParameterError, "Can't change reason for test once set"
214
- end
204
+ raise InvalidParameterError, "Can't change reason for test once set" if order.reason_for_test&.value_coded
215
205
 
216
- order.reason_for_test.delete
206
+ order.reason_for_test&.delete
217
207
  add_reason_for_test(order, date: order.start_date, reason_for_test_id: concept_id)
218
208
  end
219
209
  end
@@ -0,0 +1,7 @@
1
+ class AddDefaultToLimsOrderMapping < ActiveRecord::Migration[5.2]
2
+ def up
3
+ ActiveRecord::Base.connection.execute('ALTER TABLE lab_lims_order_mappings MODIFY revision VARCHAR(256) DEFAULT NULL')
4
+ end
5
+
6
+ def down; end
7
+ end
data/lib/lab/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lab
4
- VERSION = '1.1.2'
4
+ VERSION = '1.1.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: his_emr_api_lab
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elizabeth Glaser Pediatric Foundation Malawi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-15 00:00:00.000000000 Z
11
+ date: 2021-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: couchrest
@@ -290,6 +290,7 @@ files:
290
290
  - db/migrate/20210326195504_add_order_revision_to_lims_order_mapping.rb
291
291
  - db/migrate/20210407071728_create_lab_lims_failed_imports.rb
292
292
  - db/migrate/20210610095024_fix_numeric_results_value_type.rb
293
+ - db/migrate/20210807111531_add_default_to_lims_order_mapping.rb
293
294
  - lib/auto12epl.rb
294
295
  - lib/couch_bum/couch_bum.rb
295
296
  - lib/generators/lab/install/USAGE