Class: Services::SltcMdsPull

Inherits:
MdsPull show all
Includes:
FileUtils
Defined in:
app/models/services/sltc_mds_pull.rb

Constant Summary

Constants inherited from MdsPull

MdsPull::ASYNC_SEND_OPTS

Instance Attribute Summary

Attributes inherited from ServiceImplementation

#service_definition

Instance Method Summary collapse

Methods inherited from MdsPull

#invoke

Methods inherited from ServiceImplementation

#account_map, #initialize, #server_credentials

Constructor Details

This class inherits a constructor from Services::ServiceImplementation

Instance Method Details

#api_impl(configured_account) ⇒ Object



97
98
99
# File 'app/models/services/sltc_mds_pull.rb', line 97

def api_impl()
  SltcApi.()
end

#configure_added_facilities(added_facility_info) ⇒ Object



169
170
171
172
173
174
175
# File 'app/models/services/sltc_mds_pull.rb', line 169

def configure_added_facilities(added_facility_info)
  configure_existing_enabled_facilities(added_facility_info)
  
  # We will not be creating new facilities based on the information from 
  # SLTC. It will only configure existing, enabled facilities that have 
  # not yet been configured
end

#configure_existing_enabled_facilities(added_facility_info) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/models/services/sltc_mds_pull.rb', line 177

def configure_existing_enabled_facilities(added_facility_info)
  govids = added_facility_info.map { | facility_info | facility_info['ccn'] }
  facilities = ApplicationApi.api_impl.enabled_facilities_with_govids(govids)
  facilities.each do | facility |
    facility_info = added_facility_info.detect { | fi | fi['ccn'] == facility.govid }
    # Set the state code/fac_id if not already set.
    if facility.state != facility_info['state'] ||
        facility.fac_id != facility_info['mdsFacilityId']
      facility.update_attributes(fac_id: facility_info['mdsFacilityId'],
                                 state: facility_info['state'])
    end
    # Create the facility mappings we need
    FacilityMapping.create!(third_party_id: service_definition.third_party_id,
                           facility_id: facility.id,
                           facility_code: facility_info['id'],
                           top_level: false)

    # Create the configured facility we need
    ConfiguredFacility.create!(service_definition_id: service_definition.id,
                              facility_id: facility.id,
                              enabled: true)
  end
end

#disable_removed_facilities(facility_codes) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/services/sltc_mds_pull.rb', line 155

def disable_removed_facilities(facility_codes)
  fac_mappings = FacilityMapping.
    sltc_provider_mappings(facility_codes)

  facility_ids = fac_mappings.pluck(:facility_id)
  mapping_ids = fac_mappings.pluck(:id)

  # Delete all the facility configuration entries
  ConfiguredFacility.where(facility_id: facility_ids).destroy_all

  # Remove all the facility mappings
  FacilityMapping.where(id: mapping_ids).destroy_all
end

#download_file(api, available_file) ⇒ Object



75
76
77
78
79
80
81
# File 'app/models/services/sltc_mds_pull.rb', line 75

def download_file(api, available_file)
  mkdir_p available_file.download_path
  File.open(available_file.downloaded_file_path, "wb") do |f|
    f.write(api.download_assessment_request(available_file.request_id))
  end
  available_file.id
end

#identify_providers(configured_account) ⇒ Object

Overridden from MdsPull



9
10
11
12
13
# File 'app/models/services/sltc_mds_pull.rb', line 9

def identify_providers()
  api = api_impl()
  provider_list = api.list_providers
  process_provider_list(, provider_list)
end

#list_available_assessment_requests(api, configured_account) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/services/sltc_mds_pull.rb', line 40

def list_available_assessment_requests(api, )
  assessment_request_ids = []
  enabled_providers = ()

  AssessmentRequest.available.where(facility_code: enabled_providers).each do | assessment_request |
    status_response = api.assessment_request_status(assessment_request.request_id)
    case status_response['status']
    when 'Complete'
      assessment_request_ids << [ assessment_request.request_id, "#{assessment_request.request_id}.zip" ]
      assessment_request.update_attribute :status, AssessmentRequest::STATUS_COMPLETED
    when 'Empty'
      assessment_request.update_attribute :status, AssessmentRequest::STATUS_EMPTY
    when 'Error'
      assessment_request.update_attribute :status, AssessmentRequest::STATUS_ERROR
    else
      # Do nothing ... Must be processing so we leave the status as 'Active'
    end
  end

  assessment_request_ids
end

#list_available_files(configured_account) ⇒ Object



34
35
36
37
38
# File 'app/models/services/sltc_mds_pull.rb', line 34

def list_available_files()
  api = api_impl()
  
  list_available_assessment_requests api, 
end

#perform_cleanup_on_remote_server(configured_account) ⇒ Object



71
72
73
# File 'app/models/services/sltc_mds_pull.rb', line 71

def perform_cleanup_on_remote_server()
  .available_files.deleted.pluck(:id)
end

#perform_file_downloads(configured_account, files_to_download) ⇒ Object



62
63
64
65
66
67
68
69
# File 'app/models/services/sltc_mds_pull.rb', line 62

def perform_file_downloads(, files_to_download)
  api = api_impl()
  downloaded_files = []
  files_to_download.each do | available_file |
    downloaded_files << download_file(api, available_file)
  end
  downloaded_files.compact
end

#process_provider_list(configured_account, provider_list) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/services/sltc_mds_pull.rb', line 101

def process_provider_list(, provider_list)
  # The provider Ids need to be converted to strings because we store
  # the provider_codes as strings in the db.
  sltc_provider_ids = provider_list.map { | provider_info | provider_info['id'].to_s }
  # Get the provider ids that are currently enabled for this account
  mapped_facility_ids = ()

  facilities_removed = mapped_facility_ids - sltc_provider_ids
  facilities_added = sltc_provider_ids - mapped_facility_ids

  added_facility_info = provider_list.select { | provider | facilities_added.include?(provider['id'].to_s) }

  still_to_add = facilities_added - ProviderChange.
    added_providers(.id, facilities_added).
    pluck(:facility_code) 

  register_provider_deltas , provider_list.select { |p| still_to_add.include?(p['id'].to_s) }, ProviderChange::VERB_ADD

  still_to_remove = facilities_removed - ProviderChange.
    deleted_providers(.id, facilities_removed).
    pluck(:facility_code) 

  mappings = FacilityMapping.sltc_provider_mappings(still_to_remove).
    map do | fm |
    facility = fm.facility
    {
      'id' => fm.facility_code,
      'name' => facility.name,
      'state' => facility.state,
      'npi' => facility.npi,
      'ccn' => facility.govid,
      'mdsFacilityId' => facility.fac_id
    }
  end

  register_provider_deltas , mappings, ProviderChange::VERB_DELETE

  disable_removed_facilities(facilities_removed) unless facilities_removed.empty?
  configure_added_facilities(added_facility_info)
end

#providers_for_account(configured_account) ⇒ Object



26
27
28
29
30
31
32
# File 'app/models/services/sltc_mds_pull.rb', line 26

def ()
  enabled_facility_ids = ApplicationApi.api_impl.(.).pluck(:id)
  FacilityMapping.
    joins('LEFT OUTER JOIN services_configured_facilities ON services_configured_facilities.facility_id = services_facility_mappings.facility_id').
    where('services_facility_mappings.facility_id IN (?) AND services_configured_facilities.enabled = 1', enabled_facility_ids).
    pluck(:facility_code)
end

#register_provider_deltas(configured_account, provider_list, verb) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/services/sltc_mds_pull.rb', line 142

def register_provider_deltas(, provider_list, verb)
  provider_list.each do |pi|
    ProviderChange.create!(configured_account_id: .id,
                           facility_code: pi['id'],
                           name: pi['name'],
                           state: pi['state'],
                           npi: pi['npi'],
                           ccn: pi['ccn'],
                           fac_id: pi['mdsFacilityId'],
                           verb: verb)
  end
end

#request_assessments(configured_account) ⇒ Object

Overridden from MdsPull



18
19
20
21
22
23
24
# File 'app/models/services/sltc_mds_pull.rb', line 18

def request_assessments()
  enabled_providers = ()

  enabled_providers.each do | provider_id |
    request_assessments_to_pull(, provider_id)
  end
end

#request_assessments_to_pull(configured_account, provider_code) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/models/services/sltc_mds_pull.rb', line 201

def request_assessments_to_pull(, provider_code)
  # Find the latest assessment request that we pulled to. We do this because
  # we want to know from when to start our requested range.
  latest_assessment_request = AssessmentRequest.
    where(facility_code: provider_code).
    order('pull_to DESC').
    limit(1).
    first

  # Set the date range for the request
  pull_to = Time.now
  pull_from = latest_assessment_request.present? ? latest_assessment_request.pull_to - 1.second : Time.now - 24.hours

  api = api_impl()
  response = api.assessment_request(provider_code,
                                    pull_from,
                                    pull_to,
                                    'Accepted')

  # Register that we made the request
  AssessmentRequest.create(facility_code: provider_code,
                           request_id: response['requestId'],
                           pull_from: pull_from,
                           pull_to: pull_to,
                           status: AssessmentRequest::STATUS_AVAILABLE)
end

#request_baseline(configured_account, provider_id, uploaded_after, uploaded_before) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/services/sltc_mds_pull.rb', line 83

def request_baseline(, provider_id, uploaded_after, uploaded_before)
  api = api_impl()
  result = api.assessment_request(provider_id, uploaded_after, uploaded_before, 'Accepted')
  Rails.logger.info "Response from baseline request: #{result.inspect}"
  request_id = result['requestId']
  # Create a new row in the assessment request table
  AssessmentRequest.create!(request_id: request_id, 
                            facility_code: provider_id,
                            pull_from: uploaded_after,
                            pull_to: uploaded_before,
                            status: AssessmentRequest::STATUS_AVAILABLE)
  request_id
end