renalware-core 2.0.0.pre.rc1 → 2.0.0.pre.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/stylesheets/renalware/modules/_dashboard.scss +5 -4
- data/app/controllers/renalware/admin/users_controller.rb +6 -2
- data/app/controllers/renalware/admissions/consults_controller.rb +1 -1
- data/app/controllers/renalware/letters/contacts_controller.rb +4 -1
- data/app/controllers/renalware/pathology/current_observation_results_controller.rb +5 -11
- data/app/controllers/renalware/research/study_participants_controller.rb +1 -1
- data/app/helpers/renalware/dashboards_helper.rb +10 -0
- data/app/helpers/renalware/layout_helper.rb +3 -1
- data/app/models/concerns/renalware/letters/letter_pathology.rb +20 -0
- data/app/models/concerns/renalware/patient_pathology_scopes.rb +15 -7
- data/app/models/renalware/admissions/consult.rb +4 -3
- data/app/models/renalware/admissions/consult_site.rb +9 -0
- data/app/models/renalware/hd/mdm_patients_query.rb +2 -1
- data/app/models/renalware/letters/draft_letter.rb +2 -1
- data/app/models/renalware/letters/letter.rb +1 -0
- data/app/models/renalware/letters/part/recent_pathology_results.rb +2 -16
- data/app/models/renalware/letters/revise_letter.rb +2 -0
- data/app/models/renalware/pathology/{current_key_observation_set.rb → current_key_observation_set.rb.dead} +0 -0
- data/app/models/renalware/pathology/current_observation_set.rb +35 -0
- data/app/models/renalware/pathology/message_listener.rb +5 -3
- data/app/models/renalware/pathology/message_param_parser.rb +35 -10
- data/app/models/renalware/pathology/observation.rb +1 -1
- data/app/models/renalware/pathology/observation_request.rb +4 -1
- data/app/models/renalware/pathology/observations_jsonb_serializer.rb +73 -0
- data/app/models/renalware/pathology/patient.rb +4 -0
- data/app/models/renalware/pathology/update_current_observations.rb.dead +25 -0
- data/app/models/renalware/patient.rb +1 -1
- data/app/models/renalware/patients/mdm_patients_query.rb +4 -8
- data/app/models/renalware/pd/mdm_patients_query.rb +4 -8
- data/app/models/renalware/renal/low_clearance/mdm_patients_query.rb +5 -4
- data/app/models/renalware/transplants/mdm_patients_query.rb +4 -1
- data/app/presenters/renalware/admissions/consult_presenter.rb +6 -2
- data/app/presenters/renalware/dashboard/dashboard_presenter.rb +0 -4
- data/app/presenters/renalware/hd/patient_presenter.rb +6 -0
- data/app/presenters/renalware/mdm_patient_presenter.rb +6 -10
- data/app/presenters/renalware/pathology/{current_observation_results/html_table_view.rb → current_observation_results.dead/html_table_view.rb.dead} +0 -0
- data/app/presenters/renalware/pathology/{current_observation_results/presenter.rb → current_observation_results.dead/presenter.rb.dead} +0 -0
- data/app/presenters/renalware/pathology/observation_set_presenter.rb +57 -0
- data/app/presenters/renalware/pathology/observations_diff.rb +97 -77
- data/app/presenters/renalware/pathology/patient_presenter.rb +0 -4
- data/app/presenters/renalware/renal/clinical_summary_presenter.rb +1 -1
- data/app/views/renalware/admin/users/_filters.html.slim +19 -0
- data/app/views/renalware/admin/users/index.html.slim +9 -12
- data/app/views/renalware/admissions/consults/_filters.html.slim +2 -2
- data/app/views/renalware/admissions/consults/_form.html.slim +13 -53
- data/app/views/renalware/admissions/consults/_table.html.slim +3 -3
- data/app/views/renalware/dashboard/dashboards/show.html.slim +1 -1
- data/app/views/renalware/hd/mdm_patients/_patient.html.slim +2 -2
- data/app/views/renalware/letters/letters/_pathology.html.slim +2 -2
- data/app/views/renalware/letters/parts/_recent_pathology_results.html.slim +3 -3
- data/app/views/renalware/mdm_patients/_patient.html.slim +7 -7
- data/app/views/renalware/pathology/current_observation_results/index.html.slim +12 -1
- data/app/views/renalware/pathology/observations/_diff.html.slim +26 -20
- data/config/initializers/core_extensions.rb +2 -0
- data/config/locales/renalware/admissions/consults.en.yml +9 -0
- data/config/locales/renalware/dashboard/dashboard.yml +1 -1
- data/config/locales/renalware/patient.yml +0 -1
- data/db/migrate/20171204112150_create_consult_sites.rb +14 -0
- data/db/migrate/20171211161400_create_pathology_current_table.rb +19 -0
- data/db/migrate/20171213111513_create_fn_to_refresh_current_obs.rb +50 -0
- data/db/migrate/20171214141335_create_trigger_to_update_current_observation_sets.rb +111 -0
- data/db/migrate/20171214190849_enforce_request_id_on_observations.rb +5 -0
- data/db/migrate/20171215122454_add_pathology_observation_set_to_letters.rb +10 -0
- data/lib/core_extensions/hash.rb +11 -0
- data/lib/renalware/version.rb +1 -1
- data/spec/factories/admissions/consult_sites.rb +6 -0
- data/spec/factories/admissions/consults.rb +1 -1
- metadata +21 -5
@@ -3,10 +3,6 @@ require "renalware/hd"
|
|
3
3
|
module Renalware
|
4
4
|
module Pathology
|
5
5
|
class PatientPresenter < SimpleDelegator
|
6
|
-
delegate :hgb_result,
|
7
|
-
:hgb_observed_at,
|
8
|
-
to: :current_key_observation_set
|
9
|
-
|
10
6
|
def initialize(patient)
|
11
7
|
super(Pathology.cast_patient(patient.__getobj__))
|
12
8
|
end
|
@@ -11,7 +11,7 @@ module Renalware
|
|
11
11
|
def current_prescriptions
|
12
12
|
@current_prescriptions ||= begin
|
13
13
|
prescriptions = @patient.prescriptions
|
14
|
-
.includes(drug: [:drug_types])
|
14
|
+
.includes(drug: [:drug_types, :classifications])
|
15
15
|
.includes(:medication_route)
|
16
16
|
.current
|
17
17
|
.ordered
|
@@ -0,0 +1,19 @@
|
|
1
|
+
.filters
|
2
|
+
.filters__form
|
3
|
+
= search_form_for user_search, url: admin_users_path do |f|
|
4
|
+
.row
|
5
|
+
.small-9.large-4.columns
|
6
|
+
= f.label "Name or username contains"
|
7
|
+
= f.search_field :family_name_or_given_name_or_username_cont
|
8
|
+
.small-3.large-4.columns.actions.end
|
9
|
+
= f.submit t("helpers.submit.filter"), class: "button"
|
10
|
+
span= " or "
|
11
|
+
= link_to t("helpers.reset"), admin_users_path
|
12
|
+
|
13
|
+
dl.sub-nav
|
14
|
+
dd(class=("active" if params[:q].nil?))
|
15
|
+
= link_to "All", admin_users_path(q: nil)
|
16
|
+
dd(class=("active" if params.fetch(:q, {})[:unapproved]))
|
17
|
+
= link_to "Unapproved", admin_unapproved_users_path
|
18
|
+
dd(class=("active" if params.fetch(:q, {})[:inactive]))
|
19
|
+
= link_to "Inactive", admin_inactive_users_path
|
@@ -1,22 +1,19 @@
|
|
1
1
|
= within_admin_layout(title: "Users") do
|
2
2
|
|
3
|
-
|
4
|
-
dd(class=("active" if params[:q].nil?))
|
5
|
-
= link_to "All", admin_users_path(q: nil)
|
6
|
-
dd(class=("active" if params.fetch(:q, {})[:unapproved]))
|
7
|
-
= link_to "Unapproved", admin_unapproved_users_path
|
8
|
-
dd(class=("active" if params.fetch(:q, {})[:inactive]))
|
9
|
-
= link_to "Inactive", admin_inactive_users_path
|
3
|
+
= render "filters", user_search: user_search
|
10
4
|
|
11
5
|
table
|
12
6
|
thead
|
13
7
|
th.col-width-tiny
|
14
8
|
th.col-width-tiny ID
|
15
|
-
th.col-width-medium
|
16
|
-
|
17
|
-
|
9
|
+
th.col-width-medium= sort_link(user_search,
|
10
|
+
:family_name,
|
11
|
+
[:family_name, :given_name],
|
12
|
+
"User")
|
13
|
+
th.col-width-small= sort_link(user_search, :username, "Login")
|
14
|
+
th= sort_link(user_search, :email, "Email")
|
18
15
|
th Telephone #
|
19
|
-
th.col-width-date-time Added
|
16
|
+
th.col-width-date-time= sort_link(user_search, :created_at, "Added")
|
20
17
|
th.col-width-tiny Approved
|
21
18
|
|
22
19
|
tbody
|
@@ -29,6 +26,6 @@
|
|
29
26
|
td= user.email
|
30
27
|
td= user.telephone
|
31
28
|
td= l user.created_at
|
32
|
-
td= user.approved?
|
29
|
+
td= yes_no(user.approved?)
|
33
30
|
|
34
31
|
= paginate users
|
@@ -5,8 +5,8 @@
|
|
5
5
|
method: :get do |f|
|
6
6
|
.row
|
7
7
|
.columns.medium-4.large-3
|
8
|
-
= f.input :
|
9
|
-
collection: Renalware::
|
8
|
+
= f.input :consult_site_id_eq,
|
9
|
+
collection: Renalware::Admissions::ConsultSite.pluck(:name, :id),
|
10
10
|
label: "Site"
|
11
11
|
.columns.medium-3.large-2
|
12
12
|
= f.input :requires_aki_nurse_eq,
|
@@ -1,34 +1,3 @@
|
|
1
|
-
= content_for(:javascript)
|
2
|
-
javascript:
|
3
|
-
$(document).ready(function() {
|
4
|
-
function replaceOptionsInWardDropdown(data) {
|
5
|
-
var $select = $('#admissions_consult_hospital_ward_id');
|
6
|
-
$select.html("<option />");
|
7
|
-
if (data != undefined) {
|
8
|
-
$.each(data, function(index, hash) {
|
9
|
-
$select.append($('<option />', { value: hash["id"], text: hash["name"] }));
|
10
|
-
});
|
11
|
-
}
|
12
|
-
};
|
13
|
-
|
14
|
-
$(document).on('change', '#admissions_consult_hospital_unit_id', function(evt) {
|
15
|
-
var wardsUrl = $("option:selected", this).data("wards-url");
|
16
|
-
if (wardsUrl == undefined) {
|
17
|
-
replaceOptionsInWardDropdown(undefined);
|
18
|
-
} else {
|
19
|
-
var jqxhr = $.getJSON(wardsUrl)
|
20
|
-
.done(function(data) {
|
21
|
-
replaceOptionsInWardDropdown(data);
|
22
|
-
})
|
23
|
-
.fail(function(error, a, b) {
|
24
|
-
console.log( "error", a, b);
|
25
|
-
})
|
26
|
-
}
|
27
|
-
return true;
|
28
|
-
})
|
29
|
-
});
|
30
|
-
|
31
|
-
= render "renalware/shared/errors", model: consult
|
32
1
|
= simple_form_for(consult,
|
33
2
|
html: { autocomplete: "off" },
|
34
3
|
wrapper: :horizontal_form) do |f|
|
@@ -48,31 +17,22 @@
|
|
48
17
|
placeholder: "Search by patient name or hospital/NHS no." }\
|
49
18
|
}
|
50
19
|
|
51
|
-
|
52
|
-
|
53
|
-
/ .error classes etc.), we use a custom select_container input which allows us to build a custom
|
54
|
-
/ options collection and pass it in.
|
55
|
-
/ Credit to https://stackoverflow.com/questions/17554748/how-do-i-add-html-attributes-to-select-
|
56
|
-
/ options-with-simple-form-rails#answer-34827962
|
57
|
-
ruby:
|
58
|
-
options_html = capture do
|
59
|
-
Renalware::Hospitals::Unit.select(:id, :unit_code).all.map do |unit|
|
60
|
-
concat(content_tag(:option,
|
61
|
-
value: unit.id,
|
62
|
-
selected: unit.id == consult.hospital_unit_id,
|
63
|
-
"data-wards-url" => hospitals_unit_wards_url(unit, format: :json)) { unit.unit_code })
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
= f.input :hospital_unit_id,
|
68
|
-
as: :select_container,
|
69
|
-
options_html: options_html,
|
20
|
+
= f.input :consult_site_id,
|
21
|
+
collection: Renalware::Admissions::ConsultSite.all,
|
70
22
|
include_blank: true,
|
71
|
-
wrapper: :horizontal_medium
|
23
|
+
wrapper: :horizontal_medium,
|
24
|
+
include_blank: "Select local site (or enter other site below)",
|
25
|
+
label: "Site"
|
72
26
|
|
73
27
|
= f.association :hospital_ward,
|
74
|
-
|
75
|
-
|
28
|
+
as: :grouped_select,
|
29
|
+
group_method: :wards,
|
30
|
+
collection: Renalware::Hospitals::Unit.includes(wards: [:hospital_unit]),
|
31
|
+
label_method: ->(s){ "#{s.to_s} (#{s.hospital_unit.unit_code})" },
|
32
|
+
wrapper: :horizontal_medium,
|
33
|
+
include_blank: "Select local ward (or enter other ward below)"
|
34
|
+
|
35
|
+
= f.input :other_site_or_ward, wrapper: :horizontal_medium
|
76
36
|
= f.input :started_on, as: :date_picker, wrapper: :horizontal_datepicker
|
77
37
|
= f.input :consult_type, wrapper: :horizontal_medium
|
78
38
|
= f.input :decided_on, as: :date_picker, wrapper: :horizontal_datepicker
|
@@ -2,7 +2,7 @@
|
|
2
2
|
table
|
3
3
|
thead
|
4
4
|
tr
|
5
|
-
th.col-width-
|
5
|
+
th.col-width-large Location
|
6
6
|
th.at-least.col-width-medium Patient
|
7
7
|
th.col-width-nhs-no NHS No.
|
8
8
|
th.col-width-reference-no Hosp Nos.
|
@@ -18,7 +18,7 @@
|
|
18
18
|
- consults.each do |consult|
|
19
19
|
- uid = "consult-#{consult.id}"
|
20
20
|
tr
|
21
|
-
td= consult.
|
21
|
+
td= consult.location
|
22
22
|
td= consult.patient_name
|
23
23
|
td= consult.patient_nhs_number
|
24
24
|
td= consult.patient_hospital_identifiers
|
@@ -43,7 +43,7 @@
|
|
43
43
|
|
44
44
|
= dropdown_btn_item(icon: "fa-edit",
|
45
45
|
url: edit_admissions_consult_path(consult),
|
46
|
-
title: "Edit",
|
46
|
+
title: "View/Edit",
|
47
47
|
enabled: policy(consult).edit?)
|
48
48
|
|
49
49
|
= dropdown_btn_item(icon: "fa-trash",
|
@@ -1,2 +1,2 @@
|
|
1
|
-
= within_admin_layout(title:
|
1
|
+
= within_admin_layout(title: composed_dashboard_title(current_user.full_name), page_title: "Dashboard") do
|
2
2
|
= render "content", dashboard: dashboard
|
@@ -16,6 +16,6 @@ tr
|
|
16
16
|
td= patient.dialysing_at_unit
|
17
17
|
td= transplant_patient.current_registration_status
|
18
18
|
td= patient.transport_summary
|
19
|
-
td=
|
20
|
-
td= l(
|
19
|
+
td= patient.current_observation_set.hgb_result
|
20
|
+
td= l(patient.current_observation_set.hgb_observed_at)
|
21
21
|
td.actions= link_to t(".view"), view_proc.call(patient)
|
@@ -2,7 +2,8 @@ ruby:
|
|
2
2
|
new_pathology_timestamp = Time.zone.now
|
3
3
|
diff = Renalware::Pathology::ObservationsDiff.new(
|
4
4
|
patient: letter.patient,
|
5
|
-
|
5
|
+
observation_set_a: letter.pathology_snapshot,
|
6
|
+
observation_set_b: letter.patient.current_observation_set&.values,
|
6
7
|
descriptions: Renalware::Letters::RelevantObservationDescription.all
|
7
8
|
)
|
8
9
|
|
@@ -22,5 +23,4 @@ ruby:
|
|
22
23
|
as: :inline_radio_buttons,
|
23
24
|
label: "Include pathology updates below"
|
24
25
|
|
25
|
-
|
26
26
|
= diff.to_html
|
@@ -2,8 +2,8 @@
|
|
2
2
|
h3 Recent Investigations
|
3
3
|
|
4
4
|
- if recent_pathology_results.any?
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
- recent_pathology_results.each do |code, observation|
|
6
|
+
span #{l(observation[:observed_at]&.to_date)}: #{code} #{observation[:result]};
|
7
|
+
|
|
8
8
|
- else
|
9
9
|
p None
|
@@ -5,12 +5,12 @@ tr
|
|
5
5
|
td= patient.sex.code
|
6
6
|
td= patient.age
|
7
7
|
td= patient.modality_description&.name
|
8
|
-
td= patient.hgb_result
|
9
|
-
td= l(patient.hgb_observed_at
|
10
|
-
td= patient.ure_result
|
11
|
-
td= l(patient.ure_observed_at
|
12
|
-
td= patient.cre_result
|
13
|
-
td= l(patient.cre_observed_at
|
14
|
-
td= patient.mdrd_result
|
8
|
+
td= patient.current_observation_set.hgb_result
|
9
|
+
td= l(patient.current_observation_set.hgb_observed_at)
|
10
|
+
td= patient.current_observation_set.ure_result
|
11
|
+
td= l(patient.current_observation_set.ure_observed_at)
|
12
|
+
td= patient.current_observation_set.cre_result
|
13
|
+
td= l(patient.current_observation_set.cre_observed_at)
|
14
|
+
td= patient.current_observation_set.mdrd_result
|
15
15
|
td= l(patient.esrf_date)
|
16
16
|
td.actions= link_to t(".view"), view_proc.call(patient)
|
@@ -1,3 +1,14 @@
|
|
1
1
|
= within_patient_layout title: "Current Pathology Results",
|
2
2
|
navigation_partial: "renalware/pathology/navigation" do
|
3
|
-
|
3
|
+
table.current-observations
|
4
|
+
thead
|
5
|
+
tr
|
6
|
+
td.col-width-date Date
|
7
|
+
td.col-width-small Result
|
8
|
+
td Description
|
9
|
+
tbody
|
10
|
+
- observation_set.each_observation do |observation|
|
11
|
+
tr
|
12
|
+
td= l(observation.observed_at&.to_date)
|
13
|
+
td= observation.result
|
14
|
+
th= observation.description
|
@@ -1,22 +1,28 @@
|
|
1
1
|
/ Use by Pathology::ObservationsDiff renderer
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
2
|
+
.row
|
3
|
+
- diff.to_h.to_a.in_groups_of(11, false) do |group|
|
4
|
+
.columns.medium-6.large-4.end
|
5
|
+
table.table.pathology-observation-diff.columns
|
6
|
+
thead
|
7
|
+
tr
|
8
|
+
th.col-width-tiny(rowspan=2) Code
|
9
|
+
th.col-width-medium(colspan=2) Currently Used
|
10
|
+
th.col-width-medium(colspan=2) Newer
|
11
|
+
tr
|
12
|
+
th.col-width-tiny Result
|
13
|
+
th.col-width-datetime Date
|
14
|
+
th.col-width-tiny Result
|
15
|
+
th.col-width-datetime Date
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
tbody
|
18
|
+
- group.each do |arr|
|
19
|
+
- if arr.present?
|
20
|
+
- code = arr[0]
|
21
|
+
- curr = arr[1][0]
|
22
|
+
- poss = arr[1][1]
|
23
|
+
tr
|
24
|
+
th= code
|
25
|
+
th= curr&.result
|
26
|
+
td.col-width-date= l(curr&.observed_at&.to_date)
|
27
|
+
td= poss&.result
|
28
|
+
td.col-width-date= l(poss&.observed_at&.to_date)
|
@@ -5,9 +5,11 @@ require "core_extensions/active_record/sort"
|
|
5
5
|
require "core_extensions/date"
|
6
6
|
require "core_extensions/active_support/duration"
|
7
7
|
require "core_extensions/scenic"
|
8
|
+
require "core_extensions/hash"
|
8
9
|
|
9
10
|
I18n.extend CoreExtensions::I18n::HandleBlankValue
|
10
11
|
I18n::Backend::Simple.send(:include, I18n::Backend::Cascade)
|
11
12
|
I18n.extend CoreExtensions::I18n::AlwaysCascade
|
12
13
|
ActiveRecord::Base.send(:include, CoreExtensions::ActiveRecord::Sort)
|
13
14
|
Date.include CoreExtensions::Date::Constants
|
15
|
+
Hash.send(:include, CoreExtensions::Hash::OpenStructConversion)
|
@@ -10,6 +10,15 @@ en:
|
|
10
10
|
hospital_ward: Ward
|
11
11
|
consult_type: Type
|
12
12
|
contact_number: Bleep/Ext
|
13
|
+
other_site_or_ward: Other site or ward
|
14
|
+
errors:
|
15
|
+
models:
|
16
|
+
renalware/admissions/consult:
|
17
|
+
attributes:
|
18
|
+
other_site_or_ward:
|
19
|
+
blank: Please enter a location, or choose a Site or Ward above
|
20
|
+
renalware/admissions/consult:
|
21
|
+
other_site_or_ward:
|
13
22
|
enumerize:
|
14
23
|
renalware/admissions/consult:
|
15
24
|
transfer_priority:
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateConsultSites < ActiveRecord::Migration[5.1]
|
2
|
+
def change
|
3
|
+
create_table :admission_consult_sites do |t|
|
4
|
+
t.string :name, index: :unique
|
5
|
+
end
|
6
|
+
|
7
|
+
remove_column :admission_consults, :hospital_unit_id
|
8
|
+
add_column :admission_consults, :other_site_or_ward, :string
|
9
|
+
add_reference :admission_consults,
|
10
|
+
:consult_site,
|
11
|
+
references: :admission_consult_sites,
|
12
|
+
index: true
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# The pathology_current_observation_sets table has a values hash (jsonb)
|
2
|
+
# of the most recent pathology.
|
3
|
+
#
|
4
|
+
# You can refresh the content of this table with the following query:
|
5
|
+
# select refresh_current_observation_set(id) from patients;
|
6
|
+
|
7
|
+
class CreatePathologyCurrentTable < ActiveRecord::Migration[5.1]
|
8
|
+
def change
|
9
|
+
create_table :pathology_current_observation_sets do |t|
|
10
|
+
t.references :patient, null: false, foreign_key: true, index: { unique: true }
|
11
|
+
t.jsonb :values, index: { using: :gin }, default: {}
|
12
|
+
|
13
|
+
t.datetime :created_at, null: false, default: -> { 'CURRENT_TIMESTAMP' }
|
14
|
+
t.datetime :updated_at, null: false, default: -> { 'CURRENT_TIMESTAMP' }
|
15
|
+
end
|
16
|
+
|
17
|
+
drop_view :pathology_current_key_observation_sets, revert_to_version: 2
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class CreateFnToRefreshCurrentObs < ActiveRecord::Migration[5.1]
|
2
|
+
def up
|
3
|
+
sql = <<-SQL
|
4
|
+
CREATE OR REPLACE FUNCTION refresh_current_observation_set(a_patient_id integer)
|
5
|
+
-- Function to update the pathology_current_observation_set for a patient.
|
6
|
+
-- It stores the most recent results into the jsonb hash on that table.
|
7
|
+
-- To run for all patients, use
|
8
|
+
-- select refresh_current_observation_set(id) from patients;
|
9
|
+
--
|
10
|
+
RETURNS integer
|
11
|
+
LANGUAGE 'plpgsql'
|
12
|
+
as $$
|
13
|
+
BEGIN
|
14
|
+
with current_patient_obs as(
|
15
|
+
select
|
16
|
+
DISTINCT ON (p.id, obxd.id)
|
17
|
+
p.id as patient_id,
|
18
|
+
obxd.code,
|
19
|
+
json_build_object('result',(obx.result),'observed_at',obx.observed_at) as value
|
20
|
+
from patients p
|
21
|
+
inner join pathology_observation_requests obr on obr.patient_id = p.id
|
22
|
+
inner join pathology_observations obx on obx.request_id = obr.id
|
23
|
+
inner join pathology_observation_descriptions obxd on obx.description_id = obxd.id
|
24
|
+
where p.id = a_patient_id
|
25
|
+
order by p.id, obxd.id, obx.observed_at desc
|
26
|
+
),
|
27
|
+
current_patient_obs_as_jsonb as (
|
28
|
+
select patient_id,
|
29
|
+
jsonb_object_agg(code, value) as values,
|
30
|
+
CURRENT_TIMESTAMP,
|
31
|
+
CuRRENT_TIMESTAMP
|
32
|
+
from current_patient_obs
|
33
|
+
group by patient_id order by patient_id
|
34
|
+
)
|
35
|
+
insert into pathology_current_observation_sets (patient_id, values, created_at, updated_at)
|
36
|
+
select * from current_patient_obs_as_jsonb
|
37
|
+
ON conflict (patient_id)
|
38
|
+
DO UPDATE
|
39
|
+
SET values = excluded.values, updated_at = excluded.updated_at;
|
40
|
+
RETURN a_patient_id;
|
41
|
+
END
|
42
|
+
$$;
|
43
|
+
SQL
|
44
|
+
ActiveRecord::Base.connection.execute(sql)
|
45
|
+
end
|
46
|
+
|
47
|
+
def down
|
48
|
+
ActiveRecord::Base.connection.execute("drop function if exists refresh_current_observation_set(integer)")
|
49
|
+
end
|
50
|
+
end
|