forked from pophealth/popHealth
-
Notifications
You must be signed in to change notification settings - Fork 45
/
patients_controller.rb
118 lines (96 loc) · 4.61 KB
/
patients_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module Api
module Admin
class PatientsController < ApplicationController
resource_description do
resource_id 'Admin::Patients'
short 'Patients Admin'
formats ['json']
description "This resource allows for the management of clinical quality measures in the popHealth application."
end
include ApplicationHelper
include LogsHelper
before_filter :authenticate_user!
before_filter :validate_authorization!
skip_before_action :verify_authenticity_token
api :GET, "/patients/count", "Get count of patients in the database"
formats ['json']
example '{"patient_count":56}'
def count
log_admin_api_call LogAction::VIEW, "Get patient count"
json = {}
json['patient_count'] = QDM::Patient.count
render :json => json
end
api :POST, "/admin/patients", "Upload a zip file of patients."
param :file, nil, :desc => 'The zip file of patients to upload.', :required => true
param :practice_id, String, :desc => "ID for the patient's Practice", :required => false
param :practice_name, String, :desc => "Name for the patient's Practice", :required => false
def create
log_admin_api_call LogAction::ADD, "Upload patient ZIP file", true
file = params[:file]
practice = get_practice_parameter(params[:practice_id], params[:practice_name])
FileUtils.mkdir_p(File.join(Dir.pwd, "tmp/import"))
file_location = File.join(Dir.pwd, "tmp/import")
file_name = "patient_upload" + Time.now.to_i.to_s + rand(1000).to_s
temp_file = File.new(file_location + "/" + file_name, "w")
File.open(temp_file.path, "wb") { |f| f.write(file.read) }
Delayed::Job.enqueue(ImportArchiveJob.new({'practice' => practice, 'file' => temp_file,'user' => current_user}),:queue=>:patient_import)
render status: 200, text: 'Patient file has been uploaded.'
end
api :DELETE, "/patients/deletePatientsFromPractice", "Delete all the patients from a practice (practice_id)"
param :practice_id, String, :desc => "Practice ID", :required => true
def deletePatientsFromPractice
log_admin_api_call LogAction::DELETE, "Removed patients from practice" + params[:practice_id], true
QDM::Patient.where('extendedData.practice_id' => params[:practice_id]).delete
render status: 200, text: "Patients removed from practice: " + params[:practice_id]
end
api :DELETE, "/admin/patients", "Delete all patients in the database."
def destroy
log_admin_api_call LogAction::DELETE, "Delete all patients", true
QDM::Patient.delete_all
render status: 200, text: 'Patient records successfully removed from database.'
end
api :PUT, "/patient", "Load a single patient XML file into popHealth"
formats ['xml']
param :file, nil, :desc => "The XML patient file", :required => true
param :practice_id, String, :desc => "ID for the patient's Practice", :required => false
param :practice_name, String, :desc => "Name for the patient's Practice", :required => false
description "Upload a single XML file for a patient into popHealth."
def upload_single_patient
log_admin_api_call LogAction::ADD, "Upload single patient", true
file = StringIO.new(request.body.read)
practice = get_practice_parameter(params[:practice_id], params[:practice_name])
FileUtils.mkdir_p(File.join(Dir.pwd, "tmp/import"))
file_location = File.join(Dir.pwd, "tmp/import")
file_name = "patient_upload" + Time.now.to_i.to_s + rand(1000).to_s
temp_file = File.new(file_location + "/" + file_name, "w")
File.open(temp_file.path, "wb") { |f| f.write(file.read) }
begin
response_hash = BulkRecordImporter.import_file(temp_file,File.new(temp_file).read,nil,{},practice)
status_code = 200
rescue
status_code = 500
end
if response_hash == nil
status_text = 'File not uploaded'
status_code = 400
elsif response_hash == false
status_text = 'Patient could not be saved'
status_code = 400
elsif response_hash == true
status_text = "Patient upload successful"
status_code = 200
else
status_text = response_hash[:message]
status_code = response_hash[:status_code]
end
FileUtils.rm_rf Dir.glob("tmp/import/*")
render text: status_text, status: status_code
end
private
def validate_authorization!
authorize! :admin, :users
end
end
end
end