Skip to content

Commit

Permalink
Remove vaccine types
Browse files Browse the repository at this point in the history
We no longer need a vaccine type on the model as this will come from the
programme that the vaccines are attached to. This makes it impossible to
add vaccines of the wrong type to the wrong programme by taking the type
from a single value, and avoids the need to keep the vaccine and
programme types in sync.
  • Loading branch information
thomasleese committed Sep 27, 2024
1 parent 2fc51ff commit 9945c56
Show file tree
Hide file tree
Showing 21 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/components/app_outcome_banner_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def show_location?
end

def vaccine_summary
type = I18n.t("vaccines.#{vaccination_record.vaccine.type}")
type = vaccination_record.programme.name
brand = vaccination_record.vaccine.brand
batch = vaccination_record.batch.name
"#{type} (#{brand}, #{batch})"
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}") },
label: { text: t("vaccinations.form.label.#{@vaccination_record.programme.type}") },
link_errors: true,
checked: @vaccination_record.persisted? && @vaccination_record.administered?,
) do %>
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/import_issues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def set_import_issues
.with_pending_changes
.distinct
.includes(
:vaccine,
:batch,
:patient_session,
session: :location,
patient: %i[cohort school]
patient: %i[cohort school],
vaccine: :programme
)
.strict_loading
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/vaccination_records_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def vaccination_records
:immunisation_imports,
:performed_by_user,
:programme,
:vaccine,
patient: [:cohort, :school, { parent_relationships: :parent }],
session: %i[dates location]
session: %i[dates location],
vaccine: :programme
)
.where(programme:)
.order(:recorded_at)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/vaccines_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module VaccinesHelper
def vaccine_heading(vaccine)
sprintf("%s (%s)", vaccine.brand, t(vaccine.type, scope: "vaccines"))
"#{vaccine.brand} (#{vaccine.programme.name})"
end
end
2 changes: 1 addition & 1 deletion app/models/dps_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def csv
:performed_by_user,
:session,
:team,
:vaccine
vaccine: :programme
)
.order(:recorded_at)
.strict_loading
Expand Down
4 changes: 2 additions & 2 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ def maximum_dose_sequence
end

def requires_care_setting?
vaccine&.hpv?
@programme.hpv?
end

def requires_performed_by?
vaccine&.flu?
administered && @programme.flu?
end

def parse_date(key)
Expand Down
16 changes: 8 additions & 8 deletions app/models/vaccine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# nivs_name :text not null
# snomed_product_code :string not null
# snomed_product_term :string not null
# type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# programme_id :bigint not null
Expand Down Expand Up @@ -51,18 +50,17 @@ class Vaccine < ApplicationRecord
validates :snomed_product_term, presence: true, uniqueness: true

enum :method, %i[injection nasal], validate: true
enum :type, { flu: "flu", hpv: "hpv" }, validate: true

scope :active, -> { where(discontinued: false) }

delegate :first_health_question, to: :health_questions

def contains_gelatine?
flu? && nasal?
programme.flu? && nasal?
end

def common_delivery_sites
if hpv?
if programme.hpv?
%w[left_arm_upper_position right_arm_upper_position]
else
raise NotImplementedError,
Expand All @@ -71,10 +69,12 @@ def common_delivery_sites
end

def maximum_dose_sequence
{ "flu" => 1, "hpv" => 3 }.fetch(type)
{ "flu" => 1, "hpv" => 3 }.fetch(programme.type)
end

alias_method :seasonal?, :flu?
def seasonal?
programme.flu?
end

AVAILABLE_DELIVERY_SITES_BY_METHOD = {
"injection" =>
Expand All @@ -93,7 +93,7 @@ def available_delivery_sites
}.freeze

def available_delivery_methods
AVAILABLE_DELIVERY_METHODS_BY_TYPE.fetch(type)
AVAILABLE_DELIVERY_METHODS_BY_TYPE.fetch(programme.type)
end

SNOMED_PROCEDURE_CODE_AND_TERM_BY_TYPE = {
Expand All @@ -105,6 +105,6 @@ def available_delivery_methods
}.freeze

def snomed_procedure_code_and_term
SNOMED_PROCEDURE_CODE_AND_TERM_BY_TYPE.fetch(type)
SNOMED_PROCEDURE_CODE_AND_TERM_BY_TYPE.fetch(programme.type)
end
end
2 changes: 1 addition & 1 deletion app/views/vaccines/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Vaccine
</dt>
<dd class="nhsuk-summary-list__value">
<%= t("vaccines.#{@vaccine.type}") %>
<%= @vaccine.programme.name %>
</dd>
</div>
<div class="nhsuk-summary-list__row">
Expand Down
2 changes: 0 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,6 @@ en:
given: Vaccination recorded for
not_given: Record updated for
vaccines:
flu: Flu
hpv: HPV
index:
title: Vaccines
wicked:
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20240927142305_remove_type_from_vaccines.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RemoveTypeFromVaccines < ActiveRecord::Migration[7.2]
def change
remove_column :vaccines, :type, :string, null: false
end
end
3 changes: 1 addition & 2 deletions 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_134753) do
ActiveRecord::Schema[7.2].define(version: 2024_09_27_142305) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -587,7 +587,6 @@
end

create_table "vaccines", force: :cascade do |t|
t.string "type", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "brand", null: false
Expand Down
Binary file modified erd.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/tasks/add_health_questions.rake
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ task :add_health_questions,
next
end

puts "\nThese will be the health questions for #{programme.name}'s #{vaccine.type} vaccine #{vaccine.brand}:"
puts "\nThese will be the health questions for #{programme.name}'s vaccine #{vaccine.brand}:"
unless replace
existing_health_questions.each do |health_question|
puts Rainbow(" [old] #{health_question.title}").black
Expand Down
3 changes: 1 addition & 2 deletions lib/tasks/vaccines.rake
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ namespace :vaccines do
vaccine.method = data["method"]
vaccine.nivs_name = data["nivs_name"]
vaccine.snomed_product_term = data["snomed_product_term"]
vaccine.type = data["type"]
vaccine.programme = programme

vaccine.save!

next if vaccine.flu? || vaccine.health_questions.exists?
next if programme.flu? || vaccine.health_questions.exists?

vaccine.health_questions.create!(
title: "Does your child have any severe allergies?",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@
end

describe "dose number row" do
context "for HPV vaccine" do
let(:vaccine) { create(:vaccine, :hpv) }

context "for an HPV programme" do
before { vaccination_record.dose_sequence = 2 }

it do
Expand All @@ -129,8 +127,8 @@
end
end

context "for a seasonal vaccine (e.g. flu)" do
let(:vaccine) { create(:vaccine, :flu) }
context "for a seasonal programme" do
let(:programme) { create(:programme, :flu) }

it do
expect(subject).not_to have_css(
Expand Down
4 changes: 1 addition & 3 deletions spec/factories/programmes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
transient { batch_count { 1 } }

type { %w[flu hpv].sample }
vaccines do
[association(:vaccine, type:, batch_count:, programme: instance)]
end
vaccines { [association(:vaccine, batch_count:, programme: instance)] }

trait :hpv do
type { "hpv" }
Expand Down
9 changes: 5 additions & 4 deletions spec/factories/vaccines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# nivs_name :text not null
# snomed_product_code :string not null
# snomed_product_term :string not null
# type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# programme_id :bigint not null
Expand All @@ -34,10 +33,12 @@
#
FactoryBot.define do
factory :vaccine do
transient { batch_count { 1 } }
transient do
batch_count { 1 }
type { %w[flu hpv].sample }
end

type { %w[flu hpv].sample }
programme { Programme.find_or_create_by!(type:) }
programme { association :programme, type: }

brand { Faker::Commerce.product_name }
manufacturer { Faker::Company.name }
Expand Down
4 changes: 2 additions & 2 deletions spec/models/health_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
end

it "ignores health questions outside of the scoped collection" do
create :health_question
create(:health_question, vaccine:)

hqs.first.update! next_question: hqs.second
hqs.second.update! next_question: hqs.third
Expand All @@ -78,7 +78,7 @@
end

it "ignores health questions outside of the scoped collection" do
create :health_question
create(:health_question, vaccine:)

hqs.first.update! next_question: hqs.second
hqs.second.update! next_question: hqs.third
Expand Down
2 changes: 1 addition & 1 deletion spec/models/vaccination_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

let(:patient_session) { create(:patient_session, programme:) }
let(:vaccine) { programme.vaccines.first }
let(:different_vaccine) { create(:vaccine) }
let(:different_vaccine) { create(:vaccine, programme:) }
let(:batch) { create(:batch, vaccine: different_vaccine) }

it "has an error" do
Expand Down
2 changes: 0 additions & 2 deletions spec/models/vaccine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# nivs_name :text not null
# snomed_product_code :string not null
# snomed_product_term :string not null
# type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# programme_id :bigint not null
Expand All @@ -36,7 +35,6 @@
describe Vaccine, type: :model do
describe "validation" do
it { should validate_inclusion_of(:method).in_array(%w[injection nasal]) }
it { should validate_inclusion_of(:type).in_array(%w[flu hpv]) }
end

describe "#contains_gelatine?" do
Expand Down

0 comments on commit 9945c56

Please sign in to comment.