Skip to content

Commit

Permalink
Update following programme/session change
Browse files Browse the repository at this point in the history
The relationship between programmes and sessions has been changed to a
many-to-many relationship (effectively a session can administer vaccines
for multiple programmes).

This commit updates all the code paths that relate to session and
programmes to use the new relationship in most cases, and continuing the
current assumption around sessions where it's necessary, which will be
fixed in follow up work.
  • Loading branch information
thomasleese committed Sep 25, 2024
1 parent 7982e7d commit c718f55
Show file tree
Hide file tree
Showing 65 changed files with 317 additions and 221 deletions.
4 changes: 2 additions & 2 deletions app/components/app_session_summary_card_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
end

summary_list.with_row do |row|
row.with_key { "Vaccine" }
row.with_value { vaccine }
row.with_key { "Vaccines" }
row.with_value { vaccines }
end

summary_list.with_row do |row|
Expand Down
4 changes: 2 additions & 2 deletions app/components/app_session_summary_card_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def school
helpers.session_location(@session)
end

def vaccine
@session.programme.name
def vaccines
@session.programmes.map(&:name)
end

def date
Expand Down
2 changes: 1 addition & 1 deletion app/components/app_vaccinate_form_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<%= f.govuk_radio_buttons_fieldset(:administered, legend: nil) do %>
<%= f.govuk_radio_button(
:administered, true,
label: { text: t("vaccinations.form.label.#{@vaccination_record.vaccine.type.downcase}") },
label: { text: t("vaccinations.form.label.#{@vaccination_record.vaccine.type}") },
link_errors: true,
checked: @vaccination_record.persisted? && @vaccination_record.administered?,
) do %>
Expand Down
4 changes: 2 additions & 2 deletions app/components/app_vaccinate_form_component.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class AppVaccinateFormComponent < ViewComponent::Base
def initialize(patient_session:, section:, tab:, vaccination_record: nil)
def initialize(patient_session:, section:, tab:, vaccination_record:)
super

@patient_session = patient_session
Expand Down Expand Up @@ -35,7 +35,7 @@ def session
end

def programme_name
@patient_session.programme.name
@vaccination_record.programme.name
end

def vaccine
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/consents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
.active
.strict_loading
.includes(
:programme,
:programmes,
:gillick_assessment,
{ consents: :parent },
:patient,
Expand Down Expand Up @@ -48,7 +48,11 @@ def index
end

def show
@consent = @session.programme.consents.recorded.find(params[:consent_id])
@consent =
Consent
.where(programme: @session.programmes)
.recorded
.find(params[:consent_id])
end

private
Expand Down
10 changes: 3 additions & 7 deletions app/controllers/dev_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ def reset
def random_consent_form
Faker::Config.locale = "en-GB"
@session = Session.find(params.fetch(:session_id))
@vaccine = @session.programme.vaccines.first
programme = @session.programmes.first
@vaccine = programme.vaccines.first
@consent_form =
FactoryBot.build(
:consent_form,
:draft,
programme: @session.programme,
session: @session
)
FactoryBot.build(:consent_form, :draft, programme:, session: @session)
@consent_form.health_answers = @vaccine.health_questions.to_health_answers
@consent_form.save!
@consent_form.each_health_answer do |health_answer|
Expand Down
11 changes: 7 additions & 4 deletions app/controllers/manage_consents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ManageConsentsController < ApplicationController
if: -> { wizard_value(step).present? }

def create
@consent = Consent.create! create_params
@consent = Consent.create!(create_params)

set_steps # The wizard_steps can change after certain attrs change
setup_wizard_translated # Next/previous steps can change after steps change
Expand Down Expand Up @@ -157,8 +157,11 @@ def handle_agree
response_was_given = @consent.response_given?
@consent.assign_attributes(update_params)

programme = @session.programmes.first # TODO: handle multiple programmes

if !response_was_given && @consent.response_given?
@consent.health_answers = @session.health_questions.to_health_answers
@consent.health_answers =
programme.vaccines.first.health_questions.to_health_answers
@consent.reason_for_refusal = nil
elsif response_was_given && !@consent.response_given?
@consent.health_answers = []
Expand Down Expand Up @@ -227,15 +230,15 @@ def set_patient_session
def set_triage
@triage =
Triage.find_or_initialize_by(
programme: @session.programme,
programme: @session.programmes.first, # TODO: handle multiple programmes
patient_session: @patient_session
)
end

def create_params
{
patient: @patient,
programme: @session.programme,
programme: @session.programmes.first, # TODO: handle multiple programmes
recorded_by: current_user
}.tap do |attrs|
attrs[:route] = :self_consent if @patient_session.gillick_competent?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def set_secondary_navigation
end

def set_privacy_policy_url
@privacy_policy_url = @session.programme.team.privacy_policy_url
@privacy_policy_url = @session.team.privacy_policy_url
end
end
end
8 changes: 6 additions & 2 deletions app/controllers/parent_interface/consent_forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ class ConsentFormsController < ConsentForms::BaseController
before_action :check_if_past_deadline, except: %i[deadline_passed]

def start
# TODO: handle multiple programmes
@programme = @session.programmes.first
end

def create
consent_form =
@session.consent_forms.create!(programme: @session.programme)
# TODO: handle multiple programmes
programme = @session.programmes.first

consent_form = @session.consent_forms.create!(programme:)

session[:consent_form_id] = consent_form.id

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions/edit_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def set_patients
Session
.joins(:patient_sessions)
.active
.where(programme: @session.programme)
.where(team: @session.team)
.where("patient_sessions.patient_id = patients.id")
.arel
.exists
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ class SessionsController < ApplicationController
def create
skip_policy_scope

programme = current_user.team.programmes.first
team = current_user.team

@session =
Session.create!(active: false, team: current_user.team, programme:)
Session.create!(active: false, team:, programmes: team.programmes)

redirect_to session_edit_path(@session, :location)
end

def index
@sessions_by_type =
policy_scope(Session).active.in_progress.group_by(&:type)
@sessions = policy_scope(Session).active.in_progress

render layout: "full"
end

def show
@patient_sessions =
@session.patient_sessions.strict_loading.includes(
:programme,
:programmes,
:gillick_assessment,
{ consents: :parent },
:triage,
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/triage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def index
.active
.strict_loading
.includes(
:programme,
:programmes,
:gillick_assessment,
:patient,
:triage,
Expand Down Expand Up @@ -84,7 +84,7 @@ def set_patient_session
def set_triage
@triage =
Triage.new(
programme: @session.programme,
programme: @session.programmes.first, # TODO: handle multiple programmes
patient_session: @patient_session
)
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/vaccinations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def index
.active
.strict_loading
.includes(
:programme,
:programmes,
:gillick_assessment,
:patient,
:triage,
Expand Down Expand Up @@ -161,7 +161,7 @@ def set_todays_batch
end

def set_batches
@batches = @session.programme.batches.order(expiry: :asc, name: :asc)
@batches = @session.batches.order(expiry: :asc, name: :asc)
end

def set_section_and_tab
Expand Down
4 changes: 0 additions & 4 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ def session_location(session, part_of_sentence: false)
part_of_sentence ? "unknown location" : "Unknown location"
end
end

def session_name(session)
"#{session.programme.name} session at #{session_location(session, part_of_sentence: true)}"
end
end
17 changes: 13 additions & 4 deletions app/lib/govuk_notify_personalisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(
parent: nil,
patient: nil,
patient_session: nil,
programme: nil,
session: nil,
vaccination_record: nil
)
Expand All @@ -18,7 +19,13 @@ def initialize(
@consent_form = consent_form
@parent = parent || consent&.parent
@patient = patient || consent&.patient || patient_session&.patient
@programme =
programme || vaccination_record&.programme || consent_form&.programme ||
consent&.programme
@session = session || consent_form&.session || patient_session&.session
@team =
programme&.team || session&.team || patient_session&.team ||
consent_form&.team || consent&.team || vaccination_record&.team
@vaccination_record = vaccination_record
end

Expand Down Expand Up @@ -61,7 +68,9 @@ def self.call(*args, **kwargs)
:consent_form,
:parent,
:patient,
:programme,
:session,
:team,
:vaccination_record

def batch_name
Expand Down Expand Up @@ -111,7 +120,7 @@ def parent_name
end

def programme_name
session.programme.name
programme&.name
end

def reason_did_not_vaccinate
Expand Down Expand Up @@ -168,15 +177,15 @@ def survey_deadline_date
end

def team_email
session.programme.team.email
team.email
end

def team_name
session.programme.team.name
team.name
end

def team_phone
session.programme.team.phone
team.phone
end

def today_or_date_of_vaccination
Expand Down
13 changes: 9 additions & 4 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def parent
@parent ||= params[:parent]
end

def programme
@programme ||= params[:programme]
end

def session
@session ||= params[:session]
end
Expand All @@ -45,11 +49,11 @@ def to
end

def reply_to_id
programme =
session&.programme || consent&.programme ||
consent_form&.session&.programme || patient_session&.programme
team =
programme&.team || session&.team || patient_session&.team ||
consent_form&.team || consent&.team || vaccination_record&.team

programme.team.reply_to_id
team.reply_to_id
end

def personalisation
Expand All @@ -59,6 +63,7 @@ def personalisation
parent:,
patient:,
patient_session:,
programme:,
session:,
vaccination_record:
)
Expand Down
2 changes: 1 addition & 1 deletion app/models/consent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Consent < ApplicationRecord
optional: true,
foreign_key: :recorded_by_user_id

scope :submitted_for_programme, ->(programme) { recorded.where(programme:) }
has_one :team, through: :programme

enum :response, %w[given refused not_provided], prefix: true
enum :reason_for_refusal,
Expand Down
18 changes: 11 additions & 7 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,17 @@ def session
return unless valid?

@session ||=
Session.create_with(active: false).find_or_create_by!(
team:,
programme: @programme,
date: session_date,
location:,
time_of_day: :all_day
)
Session
.create_with(active: false)
.find_or_create_by!(
team:,
date: session_date,
location:,
time_of_day: :all_day
)
.tap do
_1.programmes << @programme unless _1.programmes.include?(@programme)
end
end

def patient_session
Expand Down
10 changes: 7 additions & 3 deletions app/models/patient_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ class PatientSession < ApplicationRecord
class_name: "GillickAssessment"

has_one :team, through: :session
has_many :programmes, through: :session

has_many :triage, -> { order(:updated_at) }
has_many :vaccination_records
has_many :consents,
->(patient_session) do
submitted_for_programme(patient_session.programme).includes(
recorded.where(programme: patient_session.programmes).includes(
:parent
)
end,
Expand All @@ -69,10 +71,12 @@ def draft_vaccination_record
# HACK: this code will need to be revisited in future as it only really works for HPV, where we only have one
# vaccine. It is likely to fail for the Doubles programme as that has 2 vaccines. It is also likely to fail for
# the flu programme for the SAIS teams that offer both nasal and injectable vaccines.
vaccine = programme&.vaccines&.first

programme = programmes.first

vaccination_records
.draft
.create_with(programme:, vaccine:)
.create_with(programme:, vaccine: programme.vaccines.first)
.find_or_initialize_by(recorded_at: nil)
end

Expand Down
Loading

0 comments on commit c718f55

Please sign in to comment.