diff --git a/app/models/team_programme.rb b/app/models/team_programme.rb new file mode 100644 index 000000000..292a7ec32 --- /dev/null +++ b/app/models/team_programme.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: team_programmes +# +# id :bigint not null, primary key +# programme_id :bigint not null +# team_id :bigint not null +# +# Indexes +# +# index_team_programmes_on_programme_id (programme_id) +# index_team_programmes_on_team_id (team_id) +# index_team_programmes_on_team_id_and_programme_id (team_id,programme_id) UNIQUE +# +# Foreign Keys +# +# fk_rails_... (programme_id => programmes.id) +# fk_rails_... (team_id => teams.id) +# +class TeamProgramme < ApplicationRecord + audited + + belongs_to :programme + belongs_to :team +end diff --git a/db/migrate/20240927083819_create_team_programmes.rb b/db/migrate/20240927083819_create_team_programmes.rb new file mode 100644 index 000000000..73fbb768a --- /dev/null +++ b/db/migrate/20240927083819_create_team_programmes.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CreateTeamProgrammes < ActiveRecord::Migration[7.2] + def change + # rubocop:disable Rails/CreateTableWithTimestamps + create_table :team_programmes do |t| + t.references :team, foreign_key: true, null: false + t.references :programme, foreign_key: true, null: false + t.index %i[team_id programme_id], unique: true + end + # rubocop:enable Rails/CreateTableWithTimestamps + end +end diff --git a/db/schema.rb b/db/schema.rb index 450760dca..25b610f25 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -486,6 +486,14 @@ t.index ["team_id"], name: "index_sessions_on_team_id" end + create_table "team_programmes", force: :cascade do |t| + t.bigint "team_id", null: false + t.bigint "programme_id", null: false + t.index ["programme_id"], name: "index_team_programmes_on_programme_id" + t.index ["team_id", "programme_id"], name: "index_team_programmes_on_team_id_and_programme_id", unique: true + t.index ["team_id"], name: "index_team_programmes_on_team_id" + end + create_table "teams", force: :cascade do |t| t.text "name", null: false t.datetime "created_at", null: false @@ -649,6 +657,8 @@ add_foreign_key "programmes_sessions", "sessions" add_foreign_key "session_dates", "sessions" add_foreign_key "sessions", "teams" + add_foreign_key "team_programmes", "programmes" + add_foreign_key "team_programmes", "teams" add_foreign_key "triage", "patient_sessions" add_foreign_key "triage", "programmes" add_foreign_key "triage", "users", column: "performed_by_user_id" diff --git a/erd.pdf b/erd.pdf index e93c2857e..8b050b802 100644 Binary files a/erd.pdf and b/erd.pdf differ diff --git a/lib/tasks/programmes.rake b/lib/tasks/programmes.rake index 1a8e80d4d..cc1b04c40 100644 --- a/lib/tasks/programmes.rake +++ b/lib/tasks/programmes.rake @@ -19,6 +19,8 @@ namespace :programmes do programme = Programme.create!(team:, type:, vaccines:) + TeamProgramme.create!(team:, programme:) + puts "New #{programme.name} programme with ID #{programme.id} created." puts "Vaccines: #{vaccines.map(&:brand).join(", ")}" end diff --git a/spec/factories/team_programmes.rb b/spec/factories/team_programmes.rb new file mode 100644 index 000000000..5f8f1e553 --- /dev/null +++ b/spec/factories/team_programmes.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: team_programmes +# +# id :bigint not null, primary key +# programme_id :bigint not null +# team_id :bigint not null +# +# Indexes +# +# index_team_programmes_on_programme_id (programme_id) +# index_team_programmes_on_team_id (team_id) +# index_team_programmes_on_team_id_and_programme_id (team_id,programme_id) UNIQUE +# +# Foreign Keys +# +# fk_rails_... (programme_id => programmes.id) +# fk_rails_... (team_id => teams.id) +# +FactoryBot.define do + factory :team_programme do + team + programme + end +end diff --git a/spec/models/team_programme_spec.rb b/spec/models/team_programme_spec.rb new file mode 100644 index 000000000..57171c9f4 --- /dev/null +++ b/spec/models/team_programme_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: team_programmes +# +# id :bigint not null, primary key +# programme_id :bigint not null +# team_id :bigint not null +# +# Indexes +# +# index_team_programmes_on_programme_id (programme_id) +# index_team_programmes_on_team_id (team_id) +# index_team_programmes_on_team_id_and_programme_id (team_id,programme_id) UNIQUE +# +# Foreign Keys +# +# fk_rails_... (programme_id => programmes.id) +# fk_rails_... (team_id => teams.id) +# +describe TeamProgramme do + subject(:team_programme) { build(:team_programme) } + + it { should be_valid } +end