Skip to content

Commit 8c1eeb5

Browse files
author
Alistair Davidson
committed
Separate-out consent fields into separate ReportableConsentEvent model. Split out ReportableEventMethods into a shared concern. Add Rake tasks for populating the reporting table
1 parent e89a188 commit 8c1eeb5

10 files changed

+239
-33
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module ReportableEventMethods
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
belongs_to :source, polymorphic: true
6+
belongs_to :patient
7+
8+
before_validation :set_event_timestamp_date_part_attributes, :set_patient_year_group
9+
10+
protected
11+
12+
def set_patient_year_group
13+
self.patient_year_group = self.patient&.year_group(now: self.event_timestamp&.to_date)
14+
end
15+
16+
def set_event_timestamp_date_part_attributes
17+
self.event_timestamp_day = event_timestamp&.day
18+
self.event_timestamp_month = event_timestamp&.month
19+
self.event_timestamp_year = event_timestamp&.year
20+
21+
self.event_timestamp_academic_year = event_timestamp.to_date.academic_year
22+
end
23+
end
24+
end

app/models/consent.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,29 @@ def notes_required?
207207
(response_refused? && !reason_for_refusal_personal_choice?)
208208
end
209209

210+
def create_or_update_reportable_consent_event
211+
re =
212+
ReportableConsentEvent.find_or_initialize_by(
213+
event_timestamp: self.consent_form&.recorded_at || self.submitted_at,
214+
event_type: ["consent", self.response].join("_"),
215+
source_id: self.id,
216+
source_type: self.class.name
217+
)
218+
219+
re.copy_attributes_from_references(
220+
patient: self.patient,
221+
parent: self.parent,
222+
parent_relationship: self.patient.parent_relationships.find_by(parent_id: self.parent_id),
223+
consent: self,
224+
programme: self.programme,
225+
organisation: self.organisation
226+
)
227+
228+
re.save!
229+
re
230+
end
231+
232+
210233
class ConsentFormNotRecorded < StandardError
211234
end
212235
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ReportableConsentEvent < ApplicationRecord
2+
include DenormalizingConcern
3+
include ReportableEventMethods
4+
5+
enum :event_type, {
6+
consent_request_sent: "consent_request_sent",
7+
consent_given: "consent_given",
8+
consent_refused: "consent_refused",
9+
consent_not_provided: 'consent_not_provided',
10+
}, validate: true
11+
end

app/models/reportable_vaccination_event.rb

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#
77
# id :bigint not null, primary key
88
# event_timestamp :datetime
9+
# event_timestamp_academic_year :integer
910
# event_timestamp_day :integer
1011
# event_timestamp_month :integer
1112
# event_timestamp_year :integer
@@ -68,26 +69,12 @@
6869
#
6970
class ReportableVaccinationEvent < ApplicationRecord
7071
include DenormalizingConcern
71-
72-
belongs_to :source, polymorphic: true
73-
72+
include ReportableEventMethods
73+
7474
enum :event_type,
75-
{
76-
vaccination_not_well: "not_well",
77-
vaccination_administered: "vaccination_administered",
78-
consent_request_sent: "consent_request_sent",
79-
consent_given: "consent_given",
80-
consent_refused: "consent_refused"
81-
},
82-
validate: true
83-
84-
before_validation :set_event_timestamp_date_part_attributes
85-
86-
protected
87-
88-
def set_event_timestamp_date_part_attributes
89-
self.event_timestamp_day = event_timestamp&.day
90-
self.event_timestamp_month = event_timestamp&.month
91-
self.event_timestamp_year = event_timestamp&.year
92-
end
75+
{
76+
vaccination_not_well: "not_well",
77+
vaccination_administered: "vaccination_administered",
78+
},
79+
validate: true
9380
end

app/models/vaccination_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def create_or_update_reportable_vaccination_event
196196
organisation: self.team&.organisation,
197197
programme: self.programme
198198
)
199-
re.patient_year_group = self.patient&.year_group(now: self.performed_at&.to_date)
199+
200200
re.save!
201201
re
202202
end

db/migrate/20250624135538_add_reportable_events.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def change
88
t.integer :event_timestamp_year
99
t.integer :event_timestamp_month
1010
t.integer :event_timestamp_day
11-
11+
t.integer :event_timestamp_academic_year
12+
1213
t.references :source, polymorphic: true
1314

1415
t.bigint :patient_id
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class AddReportableConsentEvents < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :reportable_consent_events do |t|
4+
t.string :event_type
5+
t.datetime :event_timestamp
6+
t.integer :event_timestamp_year
7+
t.integer :event_timestamp_month
8+
t.integer :event_timestamp_day
9+
t.integer :event_timestamp_academic_year
10+
11+
t.references :source, polymorphic: true
12+
13+
t.bigint :patient_id
14+
t.date :patient_date_of_birth
15+
t.string :patient_nhs_number
16+
17+
t.string :patient_address_town
18+
t.string :patient_address_postcode
19+
t.string :patient_gender_code
20+
t.boolean :patient_home_educated
21+
t.date :patient_date_of_death
22+
t.integer :patient_birth_academic_year
23+
t.integer :patient_year_group
24+
25+
t.integer :consent_response
26+
t.integer :consent_reason_for_refusal
27+
t.text :consent_notes
28+
t.integer :consent_route
29+
t.jsonb :consent_health_answers
30+
t.bigint :consent_recorded_by_user_id
31+
t.bigint :consent_parent_id
32+
t.bigint :consent_organisation_id
33+
t.datetime :consent_withdrawn_at
34+
t.datetime :consent_invalidated_at
35+
t.boolean :consent_notify_parents
36+
t.datetime :consent_submitted_at
37+
t.integer :consent_vaccine_methods, array: true
38+
39+
t.string :parent_full_name
40+
t.string :parent_email
41+
t.string :parent_phone
42+
t.text :parent_contact_method_other_details
43+
t.datetime :parent_created_at
44+
t.datetime :parent_updated_at
45+
t.string :parent_contact_method_type
46+
t.boolean :parent_phone_receive_updates
47+
48+
t.string :parent_relationship_type
49+
t.string :parent_relationship_other_name
50+
51+
t.string :consent_recorded_by_user_email
52+
t.string :consent_recorded_by_user_given_name
53+
t.string :consent_recorded_by_user_family_name
54+
55+
t.bigint :vaccine_id
56+
t.text :vaccine_brand
57+
t.integer :vaccine_method
58+
t.text :vaccine_manufacturer
59+
t.decimal :vaccine_dose_volume_ml
60+
t.string :vaccine_snomed_product_code
61+
t.string :vaccine_snomed_product_term
62+
t.text :vaccine_nivs_name
63+
t.boolean :vaccine_discontinued
64+
t.bigint :vaccine_programme_id
65+
t.boolean :vaccine_full_dose
66+
67+
t.bigint :programme_id
68+
t.string :programme_type
69+
70+
t.timestamps
71+
end
72+
73+
end
74+
end

db/schema.rb

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_06_26_162928) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_06_27_081633) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pg_trgm"
@@ -659,12 +659,76 @@
659659
t.index ["type"], name: "index_programmes_on_type", unique: true
660660
end
661661

662+
create_table "reportable_consent_events", force: :cascade do |t|
663+
t.string "event_type"
664+
t.datetime "event_timestamp"
665+
t.integer "event_timestamp_year"
666+
t.integer "event_timestamp_month"
667+
t.integer "event_timestamp_day"
668+
t.integer "event_timestamp_academic_year"
669+
t.string "source_type"
670+
t.bigint "source_id"
671+
t.bigint "patient_id"
672+
t.date "patient_date_of_birth"
673+
t.string "patient_nhs_number"
674+
t.string "patient_address_town"
675+
t.string "patient_address_postcode"
676+
t.string "patient_gender_code"
677+
t.boolean "patient_home_educated"
678+
t.date "patient_date_of_death"
679+
t.integer "patient_birth_academic_year"
680+
t.integer "patient_year_group"
681+
t.integer "consent_response"
682+
t.integer "consent_reason_for_refusal"
683+
t.text "consent_notes"
684+
t.integer "consent_route"
685+
t.jsonb "consent_health_answers"
686+
t.bigint "consent_recorded_by_user_id"
687+
t.bigint "consent_parent_id"
688+
t.bigint "consent_organisation_id"
689+
t.datetime "consent_withdrawn_at"
690+
t.datetime "consent_invalidated_at"
691+
t.boolean "consent_notify_parents"
692+
t.datetime "consent_submitted_at"
693+
t.integer "consent_vaccine_methods", array: true
694+
t.string "parent_full_name"
695+
t.string "parent_email"
696+
t.string "parent_phone"
697+
t.text "parent_contact_method_other_details"
698+
t.datetime "parent_created_at"
699+
t.datetime "parent_updated_at"
700+
t.string "parent_contact_method_type"
701+
t.boolean "parent_phone_receive_updates"
702+
t.string "parent_relationship_type"
703+
t.string "parent_relationship_other_name"
704+
t.string "consent_recorded_by_user_email"
705+
t.string "consent_recorded_by_user_given_name"
706+
t.string "consent_recorded_by_user_family_name"
707+
t.bigint "vaccine_id"
708+
t.text "vaccine_brand"
709+
t.integer "vaccine_method"
710+
t.text "vaccine_manufacturer"
711+
t.decimal "vaccine_dose_volume_ml"
712+
t.string "vaccine_snomed_product_code"
713+
t.string "vaccine_snomed_product_term"
714+
t.text "vaccine_nivs_name"
715+
t.boolean "vaccine_discontinued"
716+
t.bigint "vaccine_programme_id"
717+
t.boolean "vaccine_full_dose"
718+
t.bigint "programme_id"
719+
t.string "programme_type"
720+
t.datetime "created_at", null: false
721+
t.datetime "updated_at", null: false
722+
t.index ["source_type", "source_id"], name: "index_reportable_consent_events_on_source"
723+
end
724+
662725
create_table "reportable_vaccination_events", force: :cascade do |t|
663726
t.string "event_type"
664727
t.datetime "event_timestamp"
665728
t.integer "event_timestamp_year"
666729
t.integer "event_timestamp_month"
667730
t.integer "event_timestamp_day"
731+
t.integer "event_timestamp_academic_year"
668732
t.string "source_type"
669733
t.bigint "source_id"
670734
t.bigint "patient_id"

lib/tasks/events.rake

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
# frozen_string_literal: true
22

33
namespace :events do
4-
desc "Clear all reportable events"
5-
task clear: :environment do
6-
ReportableVaccinationEvent.delete_all
4+
namespace :vaccinations do
5+
desc "Clear all reportable vaccination events"
6+
task clear: :environment do
7+
ReportableVaccinationEvent.delete_all
8+
end
9+
10+
desc "Write reportable vaccination events for all VaccinationRecords"
11+
task write_all: :environment do
12+
puts "#{VaccinationRecord.count} vaccination records"
13+
14+
VaccinationRecord.all.find_each do |vaccination|
15+
vaccination.create_or_update_reportable_vaccination_event
16+
end
17+
18+
puts "#{ReportableVaccinationEvent.count} reportable events"
19+
end
720
end
821

9-
desc "Write all VaccinationRecord events"
10-
task write_vaccination_records: :environment do
11-
puts "#{VaccinationRecord.count} vaccination records"
12-
puts "#{ReportableEvent.count} reportable events"
22+
namespace :consents do
23+
desc "Clear all reportable consent events"
24+
task clear: :environment do
25+
ReportableConsentEvent.delete_all
26+
end
1327

14-
VaccinationRecord.all.find_each do |vaccination|
15-
vaccination.create_or_update_reportable_vaccination_event!
28+
desc "Write reportable consent events for all Consent records"
29+
task write_all: :environment do
30+
puts "#{Consent.count} consent records"
31+
32+
Consent.all.find_each do |consent|
33+
consent.create_or_update_reportable_consent_event
34+
end
35+
puts "#{ReportableConsentEvent.count} reportable events"
1636
end
1737
end
38+
1839
end

spec/models/reportable_vaccination_event_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#
77
# id :bigint not null, primary key
88
# event_timestamp :datetime
9+
# event_timestamp_academic_year :integer
910
# event_timestamp_day :integer
1011
# event_timestamp_month :integer
1112
# event_timestamp_year :integer

0 commit comments

Comments
 (0)