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

Remove team from programmes #1864

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ The `cis2` feature flag also needs to be enabled in Flipper for CIS2 logins to w

## Rake tasks

- `programmes:create[team_id,type]`
- `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]`
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/programmes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ProgrammesController < ApplicationController
layout "full"

def index
@programmes = programmes
@programmes = policy_scope(Programme)
end

def show
Expand All @@ -32,11 +32,7 @@ def sessions

private

def programmes
@programmes ||= policy_scope(Programme)
end

def set_programme
@programme = programmes.find(params[:id])
@programme = policy_scope(Programme).find(params[:id])
end
end
4 changes: 2 additions & 2 deletions app/lib/govuk_notify_personalisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def initialize(
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
session&.team || patient_session&.team || consent_form&.team ||
consent&.team || vaccination_record&.team
@vaccination_record = vaccination_record
end

Expand Down
4 changes: 2 additions & 2 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def to

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

team.reply_to_id
end
Expand Down
12 changes: 3 additions & 9 deletions app/models/programme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,31 @@
# type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# team_id :integer not null
#
# Indexes
#
# index_programmes_on_team_id_and_type (team_id,type) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (team_id => teams.id)
# index_programmes_on_type (type) UNIQUE
#
class Programme < ApplicationRecord
self.inheritance_column = nil

audited

belongs_to :team

has_and_belongs_to_many :sessions
has_and_belongs_to_many :vaccines

has_many :consent_forms
has_many :consents
has_many :dps_exports
has_many :immunisation_imports
has_many :team_programmes
has_many :triages
has_many :vaccination_records
has_many :teams

has_many :batches, through: :vaccines
has_many :patient_sessions, through: :sessions
has_many :patients, through: :patient_sessions
has_many :teams, through: :team_programmes

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

Expand Down
4 changes: 3 additions & 1 deletion app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def open_for_consent?
def programmes_part_of_team
return if programmes.empty?

errors.add(:programmes, :inclusion) if programmes.map(&:team).uniq != [team]
unless programmes.all? { team.programmes.include?(_1) }
errors.add(:programmes, :inclusion)
end
end

def set_programmes
Expand Down
3 changes: 2 additions & 1 deletion app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ class Team < ApplicationRecord
has_many :cohorts
has_many :consents
has_many :locations
has_many :programmes
has_many :team_programmes
has_many :schools, -> { school }, class_name: "Location"
has_many :sessions

has_many :patient_sessions, through: :sessions
has_many :programmes, through: :team_programmes
has_many :vaccination_records, through: :patient_sessions

has_and_belongs_to_many :users
Expand Down
2 changes: 1 addition & 1 deletion app/policies/programme_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.where(team: @user.teams)
@scope.where(id: @user.programmes.ids)
end
end
end
28 changes: 28 additions & 0 deletions db/migrate/20240927124718_remove_team_from_programmes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class RemoveTeamFromProgrammes < ActiveRecord::Migration[7.2]
def up
Programme.all.find_each do |programme|
TeamProgramme.create!(programme:, team_id: programme.team_id)
end

remove_reference :programmes, :team, foreign_key: true, null: false

add_index :programmes, :type, unique: true
end

def down
add_reference :programmes, :team, foreign_key: true

Programme.all.find_each do |programme|
programme.update!(
team_id: TeamProgramme.find_by(programme:)&.team_id || Team.first.id
)
end

change_column_null :programmes, :team_id, false

remove_index :programmes, :type
add_index :programmes, %i[type team_id], unique: true
end
end
6 changes: 2 additions & 4 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_111052) do
ActiveRecord::Schema[7.2].define(version: 2024_09_27_124718) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -450,9 +450,8 @@
create_table "programmes", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "team_id", null: false
t.string "type", null: false
t.index ["team_id", "type"], name: "index_programmes_on_team_id_and_type", unique: true
t.index ["type"], name: "index_programmes_on_type", unique: true
end

create_table "programmes_sessions", id: false, force: :cascade do |t|
Expand Down Expand Up @@ -655,7 +654,6 @@
add_foreign_key "patient_sessions", "users", column: "created_by_user_id"
add_foreign_key "patients", "cohorts"
add_foreign_key "patients", "locations", column: "school_id"
add_foreign_key "programmes", "teams"
add_foreign_key "programmes_sessions", "programmes"
add_foreign_key "programmes_sessions", "sessions"
add_foreign_key "session_dates", "sessions"
Expand Down
5 changes: 2 additions & 3 deletions docs/rake-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

## Programmes

### `programmes:create[team_id,type]`
### `programmes:create[type]`

- `team_id` - The ID of the team.
- `type` - Either `flu` or `hpv`.

This creates a new programme attached to a particular team.
This creates a new programme.

## Schools

Expand Down
Binary file modified erd.pdf
Binary file not shown.
8 changes: 3 additions & 5 deletions lib/tasks/programmes.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

namespace :programmes do
desc "Create a new programme for a team."
task :create, %i[team_id type] => :environment do |_, args|
task :create, %i[type] => :environment do |_, args|
team = Team.find_by(id: args[:team_id])
type = args[:type]

raise "Could not find team." if team.nil?
raise "Invalid type." unless %w[flu hpv].include?(type)

if Programme.exists?(team:, type:)
if Programme.exists?(type:)
raise "A programme of this type already exists for this team."
end

vaccines = Vaccine.active.where(type:).to_a

raise "There are no vaccines for this type of programme." if vaccines.empty?

programme = Programme.create!(team:, type:, vaccines:)

TeamProgramme.create!(team:, programme:)
programme = Programme.create!(type:, vaccines:)

puts "New #{programme.name} programme with ID #{programme.id} created."
puts "Vaccines: #{vaccines.map(&:brand).join(", ")}"
Expand Down
6 changes: 4 additions & 2 deletions lib/tasks/teams.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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 Down Expand Up @@ -46,9 +48,9 @@ namespace :teams do
reply_to_id:
)

puts "New #{team.name} team with ID #{team.id} created."
TeamProgramme.create!(team:, programme: Programme.find_by!(type: "hpv"))

Rake::Task["programmes:create"].invoke(team.id, "hpv")
puts "New #{team.name} team with ID #{team.id} created."
end
end
end
4 changes: 2 additions & 2 deletions spec/components/app_activity_log_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
describe AppActivityLogComponent do
subject { page }

let(:team) { create(:team) }
let(:programme) { create(:programme, :hpv) }
let(:team) { create(:team, programmes: [programme]) }
let(:patient_session) do
create(
:patient_session,
Expand All @@ -29,7 +30,6 @@
let(:user) do
create(:user, teams: [team], family_name: "Joy", given_name: "Nurse")
end
let(:programme) { create(:programme, :hpv, team:) }
let(:location) { create(:location, :school, name: "Hogwarts") }
let(:session) { create(:session, programme:, location:) }
let(:patient) { create(:patient, school: location) }
Expand Down
7 changes: 4 additions & 3 deletions spec/components/app_session_table_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

let(:component) { described_class.new(sessions) }

let(:programme) { create(:programme, :hpv) }
let(:sessions) do
[
create(
:session,
academic_year: 2024,
date: Date.new(2024, 10, 1),
location: create(:location, :school, name: "Waterloo Road"),
programme: create(:programme, :hpv)
programme:
),
create(:session, location: nil)
] + create_list(:session, 8)
create(:session, programme:, location: nil)
] + create_list(:session, 8, programme:)
end

before { create_list(:patient, 5, session: sessions.first) }
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/concerns/patient_tabs_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@
end

describe "#count_patient_sessions" do
let(:session) { create(:session) }
let(:session) { create(:session, programme:) }
let(:no_consent_patient_sessions) do
create_list(:patient_session, 2, session:)
create_list(:patient_session, 2, programme:, session:)
end
let(:refuser_patient_session) do
create(:patient_session, :consent_refused, session:)
create(:patient_session, :consent_refused, programme:, session:)
end

it "counts patient session groups" do
Expand Down
4 changes: 3 additions & 1 deletion spec/factories/consents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
end

programme
team { programme.team }
team do
programme.teams.first || association(:team, programmes: [programme])
end

patient
parent { patient.parents.first }
Expand Down
6 changes: 4 additions & 2 deletions spec/factories/example_programmes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
transient do
user { create(:user) }

team { user.team || create(:team, users: [user]) }

# this name and URN matches the data in spec/fixtures/cohort_import/valid_cohort.csv
location do
create(:location, :school, name: "Surrey Primary", urn: "123456", team:)
Expand All @@ -20,9 +22,9 @@
batch_count { 4 }
end

team { user.team || create(:team, users: [user]) }
after(:create) do |programme, context|
create(:team_programme, programme:, team: context.team)

after(:create) do |_programme, context|
create_list(:location, 20, :school, team: context.team)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/factories/gillick_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#
FactoryBot.define do
factory :gillick_assessment do
assessor { create :user }
patient_session { create :patient_session }
assessor
patient_session
competent

trait :not_competent do
Expand Down
6 changes: 5 additions & 1 deletion spec/factories/patient_sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@
end

trait :unable_to_vaccinate_not_gillick_competent do
gillick_assessment { association :gillick_assessment, :not_competent }
gillick_assessment do
association :gillick_assessment,
:not_competent,
patient_session: instance
end

patient do
association :patient,
Expand Down
4 changes: 3 additions & 1 deletion spec/factories/patients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
transient do
session { nil }
programme { session&.programmes&.first }
team { programme&.team || association(:team) }
team do
session&.team || association(:team, programmes: [programme].compact)
end

parents { [create(:parent, :recorded, last_name:)] }
end
Expand Down
9 changes: 1 addition & 8 deletions spec/factories/programmes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@
# type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# team_id :integer not null
#
# Indexes
#
# index_programmes_on_team_id_and_type (team_id,type) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (team_id => teams.id)
# index_programmes_on_type (type) UNIQUE
#
FactoryBot.define do
factory :programme do
transient { batch_count { 1 } }

team

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

Expand Down
Loading
Loading