diff --git a/app/models/session.rb b/app/models/session.rb index f2332a621..91332caaf 100644 --- a/app/models/session.rb +++ b/app/models/session.rb @@ -40,6 +40,7 @@ class Session < ApplicationRecord belongs_to :location, optional: true has_many :consent_forms + has_many :dates, class_name: "SessionDate" has_many :patient_sessions has_and_belongs_to_many :immunisation_imports diff --git a/app/models/session_date.rb b/app/models/session_date.rb new file mode 100644 index 000000000..cf8cf9011 --- /dev/null +++ b/app/models/session_date.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: session_dates +# +# id :bigint not null, primary key +# value :date not null +# session_id :bigint not null +# +# Indexes +# +# index_session_dates_on_session_id (session_id) +# index_session_dates_on_session_id_and_value (session_id,value) UNIQUE +# +# Foreign Keys +# +# fk_rails_... (session_id => sessions.id) +# +class SessionDate < ApplicationRecord + audited + + belongs_to :session + + validates :value, uniqueness: { scope: :session } +end diff --git a/db/migrate/20240925075935_create_session_dates.rb b/db/migrate/20240925075935_create_session_dates.rb new file mode 100644 index 000000000..a3ffd6d2d --- /dev/null +++ b/db/migrate/20240925075935_create_session_dates.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CreateSessionDates < ActiveRecord::Migration[7.2] + def change + # rubocop:disable Rails/CreateTableWithTimestamps + create_table :session_dates do |t| + t.references :session, foreign_key: true, null: false + t.date :value, null: false + t.index %i[session_id value], unique: true + end + # rubocop:enable Rails/CreateTableWithTimestamps + end +end diff --git a/db/schema.rb b/db/schema.rb index 71de457e1..9d4dd01e2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_24_140304) do +ActiveRecord::Schema[7.2].define(version: 2024_09_25_081048) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -461,6 +461,13 @@ t.index ["vaccine_id", "programme_id"], name: "index_programmes_vaccines_on_vaccine_id_and_programme_id" end + create_table "session_dates", force: :cascade do |t| + t.bigint "session_id", null: false + t.date "value", null: false + t.index ["session_id", "value"], name: "index_session_dates_on_session_id_and_value", unique: true + t.index ["session_id"], name: "index_session_dates_on_session_id" + end + create_table "sessions", force: :cascade do |t| t.date "date" t.bigint "location_id" @@ -629,6 +636,7 @@ add_foreign_key "programmes", "teams" add_foreign_key "programmes_sessions", "programmes" add_foreign_key "programmes_sessions", "sessions" + add_foreign_key "session_dates", "sessions" add_foreign_key "sessions", "teams" add_foreign_key "triage", "patient_sessions" add_foreign_key "triage", "programmes" diff --git a/erd.pdf b/erd.pdf index 48c948d45..a5ff1478f 100644 Binary files a/erd.pdf and b/erd.pdf differ diff --git a/spec/factories/session_dates.rb b/spec/factories/session_dates.rb new file mode 100644 index 000000000..fe00a034f --- /dev/null +++ b/spec/factories/session_dates.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: session_dates +# +# id :bigint not null, primary key +# value :date not null +# session_id :bigint not null +# +# Indexes +# +# index_session_dates_on_session_id (session_id) +# index_session_dates_on_session_id_and_value (session_id,value) UNIQUE +# +# Foreign Keys +# +# fk_rails_... (session_id => sessions.id) +# +FactoryBot.define do + factory :session_date do + session + date { Date.current } + end +end diff --git a/spec/models/session_date_spec.rb b/spec/models/session_date_spec.rb new file mode 100644 index 000000000..ce53c6b3e --- /dev/null +++ b/spec/models/session_date_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: session_dates +# +# id :bigint not null, primary key +# value :date not null +# session_id :bigint not null +# +# Indexes +# +# index_session_dates_on_session_id (session_id) +# index_session_dates_on_session_id_and_value (session_id,value) UNIQUE +# +# Foreign Keys +# +# fk_rails_... (session_id => sessions.id) +# +describe SessionDate do + subject(:session_date) { build(:session_date) } + + it { should be_valid } +end