diff --git a/app/controllers/manage_consents_controller.rb b/app/controllers/manage_consents_controller.rb index 0dd8aaa97..abe36cbc1 100644 --- a/app/controllers/manage_consents_controller.rb +++ b/app/controllers/manage_consents_controller.rb @@ -239,6 +239,7 @@ def create_params { patient: @patient, programme: @session.programmes.first, # TODO: handle multiple programmes + team: @session.team, recorded_by: current_user }.tap do |attrs| attrs[:route] = :self_consent if @patient_session.gillick_competent? diff --git a/app/models/consent.rb b/app/models/consent.rb index 927cdd13d..42023f8b4 100644 --- a/app/models/consent.rb +++ b/app/models/consent.rb @@ -17,6 +17,7 @@ # patient_id :bigint not null # programme_id :bigint not null # recorded_by_user_id :bigint +# team_id :bigint not null # # Indexes # @@ -24,6 +25,7 @@ # index_consents_on_patient_id (patient_id) # index_consents_on_programme_id (programme_id) # index_consents_on_recorded_by_user_id (recorded_by_user_id) +# index_consents_on_team_id (team_id) # # Foreign Keys # @@ -31,6 +33,7 @@ # fk_rails_... (patient_id => patients.id) # fk_rails_... (programme_id => programmes.id) # fk_rails_... (recorded_by_user_id => users.id) +# fk_rails_... (team_id => teams.id) # class Consent < ApplicationRecord @@ -44,6 +47,9 @@ class Consent < ApplicationRecord attr_reader :new_or_existing_parent attr_accessor :triage + belongs_to :team + belongs_to :programme + has_one :consent_form belongs_to :parent, -> { recorded }, optional: true belongs_to :draft_parent, @@ -52,14 +58,11 @@ class Consent < ApplicationRecord optional: true, foreign_key: :parent_id belongs_to :patient - belongs_to :programme belongs_to :recorded_by, class_name: "User", optional: true, foreign_key: :recorded_by_user_id - has_one :team, through: :programme - enum :response, %w[given refused not_provided], prefix: true enum :reason_for_refusal, %w[ @@ -169,6 +172,7 @@ def self.from_consent_form!(consent_form, patient_session) consent = create!( consent_form:, + team: consent_form.session.team, programme: consent_form.programme, patient:, parent:, diff --git a/app/models/team.rb b/app/models/team.rb index 3d08ce47c..a80ae695d 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -27,6 +27,7 @@ class Team < ApplicationRecord has_many :cohort_imports has_many :cohorts + has_many :consents has_many :locations has_many :programmes has_many :schools, -> { school }, class_name: "Location" diff --git a/app/policies/consent_policy.rb b/app/policies/consent_policy.rb index ec30f4ec1..9a05824f2 100644 --- a/app/policies/consent_policy.rb +++ b/app/policies/consent_policy.rb @@ -8,7 +8,7 @@ def initialize(user, scope) end def resolve - @scope.joins(:programme).where(programme: { team_id: @user.teams.ids }) + @scope.where(team: @user.teams) end end end diff --git a/db/migrate/20240927111052_add_team_to_consents.rb b/db/migrate/20240927111052_add_team_to_consents.rb new file mode 100644 index 000000000..5a180c595 --- /dev/null +++ b/db/migrate/20240927111052_add_team_to_consents.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddTeamToConsents < ActiveRecord::Migration[7.2] + def up + add_reference :consents, :team, foreign_key: true + + Consent.all.find_each do |consent| + consent.update!(team_id: consent.programme.team_id) + end + + change_column_null :consents, :team_id, false + end + + def down + remove_reference :consents, :team + end +end diff --git a/db/schema.rb b/db/schema.rb index 25b610f25..68407e98a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_09_27_085819) do +ActiveRecord::Schema[7.2].define(version: 2024_09_27_111052) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -147,10 +147,12 @@ t.jsonb "health_answers", default: [] t.bigint "recorded_by_user_id" t.bigint "parent_id" + t.bigint "team_id", null: false t.index ["parent_id"], name: "index_consents_on_parent_id" t.index ["patient_id"], name: "index_consents_on_patient_id" t.index ["programme_id"], name: "index_consents_on_programme_id" t.index ["recorded_by_user_id"], name: "index_consents_on_recorded_by_user_id" + t.index ["team_id"], name: "index_consents_on_team_id" end create_table "dps_exports", force: :cascade do |t| @@ -624,6 +626,7 @@ add_foreign_key "consents", "parents" add_foreign_key "consents", "patients" add_foreign_key "consents", "programmes" + add_foreign_key "consents", "teams" add_foreign_key "consents", "users", column: "recorded_by_user_id" add_foreign_key "dps_exports", "programmes" add_foreign_key "gillick_assessments", "patient_sessions" diff --git a/erd.pdf b/erd.pdf index 8b050b802..4627d2190 100644 Binary files a/erd.pdf and b/erd.pdf differ diff --git a/spec/factories/consents.rb b/spec/factories/consents.rb index 4d3abf332..320461f57 100644 --- a/spec/factories/consents.rb +++ b/spec/factories/consents.rb @@ -17,6 +17,7 @@ # patient_id :bigint not null # programme_id :bigint not null # recorded_by_user_id :bigint +# team_id :bigint not null # # Indexes # @@ -24,6 +25,7 @@ # index_consents_on_patient_id (patient_id) # index_consents_on_programme_id (programme_id) # index_consents_on_recorded_by_user_id (recorded_by_user_id) +# index_consents_on_team_id (team_id) # # Foreign Keys # @@ -31,6 +33,7 @@ # fk_rails_... (patient_id => patients.id) # fk_rails_... (programme_id => programmes.id) # fk_rails_... (recorded_by_user_id => users.id) +# fk_rails_... (team_id => teams.id) # FactoryBot.define do factory :consent do @@ -39,6 +42,8 @@ end programme + team { programme.team } + patient parent { patient.parents.first } diff --git a/spec/factories/patients.rb b/spec/factories/patients.rb index 954703a69..ff231a7c7 100644 --- a/spec/factories/patients.rb +++ b/spec/factories/patients.rb @@ -96,6 +96,7 @@ :given, :from_mum, patient: instance, + team:, programme: ) ] @@ -112,6 +113,7 @@ :from_mum, :health_question_notes, patient: instance, + team:, programme: ) ] @@ -127,6 +129,7 @@ :refused, :from_mum, patient: instance, + team:, programme: ) ] @@ -142,6 +145,7 @@ :refused, :from_mum, patient: instance, + team:, programme:, reason_for_refusal: "already_vaccinated", reason_for_refusal_notes: "Already had the vaccine at the GP" @@ -159,6 +163,7 @@ :refused, :from_mum, patient: instance, + team:, programme: ), association( @@ -167,6 +172,7 @@ :given, :from_dad, patient: instance, + team:, programme: ) ] diff --git a/spec/models/consent_spec.rb b/spec/models/consent_spec.rb index 55e0f4e4d..6c8092a78 100644 --- a/spec/models/consent_spec.rb +++ b/spec/models/consent_spec.rb @@ -17,6 +17,7 @@ # patient_id :bigint not null # programme_id :bigint not null # recorded_by_user_id :bigint +# team_id :bigint not null # # Indexes # @@ -24,6 +25,7 @@ # index_consents_on_patient_id (patient_id) # index_consents_on_programme_id (programme_id) # index_consents_on_recorded_by_user_id (recorded_by_user_id) +# index_consents_on_team_id (team_id) # # Foreign Keys # @@ -31,6 +33,7 @@ # fk_rails_... (patient_id => patients.id) # fk_rails_... (programme_id => programmes.id) # fk_rails_... (recorded_by_user_id => users.id) +# fk_rails_... (team_id => teams.id) # describe Consent do