Skip to content

Commit

Permalink
Update Rake tasks related to vaccines
Browse files Browse the repository at this point in the history
Now that vaccines can only belong to one programme we can simplify the
vaccines and their creation, and we don't need a task to create
programmes.
  • Loading branch information
thomasleese committed Sep 27, 2024
1 parent d88a2dc commit e0a538d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 81 deletions.
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
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
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])
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

0 comments on commit e0a538d

Please sign in to comment.