Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach vaccines to programmes directly #1866

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,9 @@ The `cis2` feature flag also needs to be enabled in Flipper for CIS2 logins to w

## Rake tasks

- `programmes:create[type]`
- `schools:add_to_team[team_id,urn]`
- `teams:create_hpv[email,name,phone,ods_code,privacy_policy_url,reply_to_id]`
- `vaccines:add_to_programme[programme_id,vaccine_nivs_name]`
- `vaccines:seed`
- `vaccines:seed[type]`

See the [Rake tasks documentation](docs/rake-tasks.md) for more information.

Expand Down
2 changes: 0 additions & 2 deletions app/models/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Batch < ApplicationRecord

has_and_belongs_to_many :immunisation_imports

has_many :programmes, through: :vaccine

validates :name, presence: true
validates :expiry, presence: true
end
23 changes: 1 addition & 22 deletions app/models/programme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Programme < ApplicationRecord
audited

has_and_belongs_to_many :sessions
has_and_belongs_to_many :vaccines

has_many :consent_forms
has_many :consents
Expand All @@ -28,6 +27,7 @@ class Programme < ApplicationRecord
has_many :team_programmes
has_many :triages
has_many :vaccination_records
has_many :vaccines

has_many :batches, through: :vaccines
has_many :patient_sessions, through: :sessions
Expand All @@ -36,34 +36,13 @@ class Programme < ApplicationRecord

enum :type, { flu: "flu", hpv: "hpv" }, validate: true

validate :vaccines_match_type

def name
human_enum_name(:type)
end

def vaccine_ids
@vaccine_ids ||= vaccines.map(&:id)
end

def vaccine_ids=(ids)
self.vaccines = Vaccine.where(id: ids)
end

YEAR_GROUPS_BY_TYPE = { "flu" => (0..11).to_a, "hpv" => (8..11).to_a }.freeze

def year_groups
YEAR_GROUPS_BY_TYPE.fetch(type)
end

private

def vaccines_match_type
errors.add(:vaccines, :blank) if vaccines.empty?

vaccine_types = vaccines.map(&:type).uniq
unless vaccine_types.empty? || vaccine_types == [type]
errors.add(:vaccines, :match_type)
end
end
end
8 changes: 8 additions & 0 deletions app/models/vaccine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,28 @@
# type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# programme_id :bigint not null
#
# Indexes
#
# index_vaccines_on_gtin (gtin) UNIQUE
# index_vaccines_on_manufacturer_and_brand (manufacturer,brand) UNIQUE
# index_vaccines_on_nivs_name (nivs_name) UNIQUE
# index_vaccines_on_programme_id (programme_id)
# index_vaccines_on_snomed_product_code (snomed_product_code) UNIQUE
# index_vaccines_on_snomed_product_term (snomed_product_term) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (programme_id => programmes.id)
#
class Vaccine < ApplicationRecord
self.inheritance_column = nil

audited

belongs_to :programme

has_and_belongs_to_many :programmes
has_many :health_questions, dependent: :destroy
has_many :batches, -> { order(:name) }
Expand Down
6 changes: 5 additions & 1 deletion app/policies/batch_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def initialize(user, scope)
end

def resolve
@scope.joins(:programmes).where(programmes: @user.programmes)
@scope.joins(vaccine: :programme).where(
vaccine: {
programme: @user.programmes
}
)
end
end
end
2 changes: 1 addition & 1 deletion app/policies/vaccine_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(:programmes).where(programmes: @user.programmes)
@scope.joins(:programme).where(programme: @user.programmes)
end
end
end
19 changes: 19 additions & 0 deletions db/migrate/20240927134753_add_programme_to_vaccines.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class AddProgrammeToVaccines < ActiveRecord::Migration[7.2]
def up
add_reference :vaccines, :programme, foreign_key: true

Vaccine.all.find_each do |vaccine|
vaccine.update!(
programme: Programme.find_or_create_by!(type: vaccine.type)
)
end

change_column_null :vaccines, :programme_id, false
end

def down
remove_reference :vaccines, :programme
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_124718) do
ActiveRecord::Schema[7.2].define(version: 2024_09_27_134753) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -599,9 +599,11 @@
t.string "snomed_product_term", null: false
t.text "nivs_name", null: false
t.boolean "discontinued", default: false, null: false
t.bigint "programme_id", null: false
t.index ["gtin"], name: "index_vaccines_on_gtin", unique: true
t.index ["manufacturer", "brand"], name: "index_vaccines_on_manufacturer_and_brand", unique: true
t.index ["nivs_name"], name: "index_vaccines_on_nivs_name", unique: true
t.index ["programme_id"], name: "index_vaccines_on_programme_id"
t.index ["snomed_product_code"], name: "index_vaccines_on_snomed_product_code", unique: true
t.index ["snomed_product_term"], name: "index_vaccines_on_snomed_product_term", unique: true
end
Expand Down Expand Up @@ -668,4 +670,5 @@
add_foreign_key "vaccination_records", "programmes"
add_foreign_key "vaccination_records", "users", column: "performed_by_user_id"
add_foreign_key "vaccination_records", "vaccines"
add_foreign_key "vaccines", "programmes"
end
17 changes: 2 additions & 15 deletions docs/rake-tasks.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# Rake Tasks

## Programmes

### `programmes:create[type]`

- `type` - Either `flu` or `hpv`.

This creates a new programme.

## Schools

### `schools:add_to_team[team_id,urn]`
Expand All @@ -34,14 +26,9 @@ This creates a new team with an HPV programme.

## Vaccines

### `vaccines:add_to_programme[programme_id, vaccine_nivs_name]`

- `programme_id` - The ID of the programme.
- `vaccine_nivs_name` - The NIVS name of the vaccine.

This adds a vaccine to a programme.
### `vaccines:seed[type]`

### `vaccines:seed`
- `type` - The type of vaccine, either `flu` or `hpv`. (optional)

This creates the default set of vaccine records, or if they already exist, updates any existing vaccine records to match the default set.

Expand Down
Binary file modified erd.pdf
Binary file not shown.
22 changes: 9 additions & 13 deletions lib/tasks/add_health_questions.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@ desc <<-DESC
Add health questions to a vaccine.

Usage:
rake add_health_questions[team_id,vaccine_id,replace]
rake add_health_questions[programme,vaccine_id,replace]

The vaccine must belong to the team given, this is a safety check.
The vaccine must belong to the programme given, this is a safety check.

Use "replace" for the replace arg to replace the existing health questions.

Example:

rake add_health_questions[1,1,replace]
rake add_health_questions[hpv,1,replace]
DESC
task :add_health_questions,
%i[team_id vaccine_id replace] => :environment do |_task, args|
team = Team.find(args[:team_id])
vaccine =
team
.programmes
.flat_map(&:vaccines)
.find { |v| v.id == args[:vaccine_id].to_i }
raise "Vaccine not found for the given team" if vaccine.nil?
%i[programme vaccine_id replace] => :environment do |_task, args|
programme = Programme.find_by!(type: args[:programme])
vaccine = programme.vaccines.find_by(id: args[:vaccine_id])
tvararu marked this conversation as resolved.
Show resolved Hide resolved
raise "Vaccine not found for the given programme" if vaccine.nil?
existing_health_questions = vaccine.health_questions.in_order
puts "Existing health questions for #{team.name}'s #{vaccine.type} vaccine #{vaccine.brand}"
puts "Existing health questions for #{programme.name}'s vaccine #{vaccine.brand}"
if existing_health_questions.any?
existing_health_questions.each do |health_question|
puts Rainbow(" #{health_question.title}").yellow
Expand Down Expand Up @@ -65,7 +61,7 @@ task :add_health_questions,
next
end

puts "\nThese will be the health questions for #{team.name}'s #{vaccine.type} vaccine #{vaccine.brand}:"
puts "\nThese will be the health questions for #{programme.name}'s #{vaccine.type} vaccine #{vaccine.brand}:"
unless replace
existing_health_questions.each do |health_question|
puts Rainbow(" [old] #{health_question.title}").black
Expand Down
25 changes: 0 additions & 25 deletions lib/tasks/programmes.rake

This file was deleted.

6 changes: 3 additions & 3 deletions lib/tasks/teams.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ namespace :teams do
:environment do |_task, args|
include TaskHelpers

Rake::Task["programmes:create"].invoke("hpv")

raise "Ensure vaccines exist before creating a team." unless Vaccine.exists?

if args.to_a.empty? && $stdin.isatty && $stdout.isatty
Expand All @@ -38,6 +36,8 @@ namespace :teams do
end

ActiveRecord::Base.transaction do
programme = Programme.find_or_create_by!(type: "hpv")

team =
Team.create!(
email:,
Expand All @@ -48,7 +48,7 @@ namespace :teams do
reply_to_id:
)

TeamProgramme.create!(team:, programme: Programme.find_by!(type: "hpv"))
TeamProgramme.create!(team:, programme:)

puts "New #{team.name} team with ID #{team.id} created."
end
Expand Down
30 changes: 8 additions & 22 deletions lib/tasks/vaccines.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace :vaccines do
desc "Seed the vaccine table from the built-in vaccine data."
task seed: :environment do
task :seed, %i[type] => :environment do |_task, args|
type = args[:type]

all_data = YAML.load_file(Rails.root.join("config/vaccines.yml"))

all_data.each_value do |data|
next if type.present? && data["type"] != type

programme = Programme.find_or_create_by!(type: data["type"])

vaccine =
Vaccine.find_or_initialize_by(
snomed_product_code: data["snomed_product_code"]
Expand All @@ -19,6 +25,7 @@ namespace :vaccines do
vaccine.nivs_name = data["nivs_name"]
vaccine.snomed_product_term = data["snomed_product_term"]
vaccine.type = data["type"]
vaccine.programme = programme

vaccine.save!

Expand All @@ -39,25 +46,4 @@ namespace :vaccines do
)
end
end

desc "Add a vaccine to a programme."
task :add_to_programme,
%i[programme_id vaccine_nivs_name] => :environment do |_, args|
programme = Programme.find_by(id: args[:programme_id])
vaccine = Vaccine.find_by(nivs_name: args[:vaccine_nivs_name])

if programme.nil? || vaccine.nil?
raise "Could not find programme or vaccine."
end

if programme.vaccines.include?(vaccine)
raise "Vaccine is already part of the programme."
end

if vaccine.type != programme.type
raise "Vaccine is not suitable for this programme type."
end

programme.vaccines << vaccine
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

let(:administered_at) { Time.zone.local(2024, 9, 6, 12) }
let(:location) { create(:location, :school, name: "Hogwarts") }
let(:programme) { create(:programme, type: vaccine&.type || :hpv) }
let(:programme) { create(:programme, :hpv) }
let(:session) { create(:session, programme:, location:) }
let(:patient_session) { create(:patient_session, session:) }
let(:vaccine) { create(:vaccine, :gardasil_9) }
let(:vaccine) { programme.vaccines.first }
let(:batch) do
create(:batch, name: "ABC", expiry: Date.new(2020, 1, 1), vaccine:)
end
Expand Down
Loading
Loading