Skip to content

Commit

Permalink
Add team to consents
Browse files Browse the repository at this point in the history
To be able to compare vaccination records across teams under the same
programme, the programme table is going to be made global, removing a
reference to the team.

To support this we need to change consents to be linked to the team
so we only display them relevant to the current team.
  • Loading branch information
thomasleese committed Sep 27, 2024
1 parent b52fd57 commit 5bcd72e
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/manage_consents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
10 changes: 7 additions & 3 deletions app/models/consent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@
# patient_id :bigint not null
# programme_id :bigint not null
# recorded_by_user_id :bigint
# team_id :bigint not null
#
# Indexes
#
# index_consents_on_parent_id (parent_id)
# 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
#
# fk_rails_... (parent_id => parents.id)
# 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
Expand All @@ -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,
Expand All @@ -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[
Expand Down Expand Up @@ -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:,
Expand Down
1 change: 1 addition & 0 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion app/policies/consent_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions db/migrate/20240927111052_add_team_to_consents.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified erd.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions spec/factories/consents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@
# patient_id :bigint not null
# programme_id :bigint not null
# recorded_by_user_id :bigint
# team_id :bigint not null
#
# Indexes
#
# index_consents_on_parent_id (parent_id)
# 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
#
# fk_rails_... (parent_id => parents.id)
# 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
Expand All @@ -39,6 +42,8 @@
end

programme
team { programme.team }

patient
parent { patient.parents.first }

Expand Down
6 changes: 6 additions & 0 deletions spec/factories/patients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
:given,
:from_mum,
patient: instance,
team:,
programme:
)
]
Expand All @@ -112,6 +113,7 @@
:from_mum,
:health_question_notes,
patient: instance,
team:,
programme:
)
]
Expand All @@ -127,6 +129,7 @@
:refused,
:from_mum,
patient: instance,
team:,
programme:
)
]
Expand All @@ -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"
Expand All @@ -159,6 +163,7 @@
:refused,
:from_mum,
patient: instance,
team:,
programme:
),
association(
Expand All @@ -167,6 +172,7 @@
:given,
:from_dad,
patient: instance,
team:,
programme:
)
]
Expand Down
3 changes: 3 additions & 0 deletions spec/models/consent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@
# patient_id :bigint not null
# programme_id :bigint not null
# recorded_by_user_id :bigint
# team_id :bigint not null
#
# Indexes
#
# index_consents_on_parent_id (parent_id)
# 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
#
# fk_rails_... (parent_id => parents.id)
# 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
Expand Down

0 comments on commit 5bcd72e

Please sign in to comment.