Skip to content

Commit

Permalink
Add Session#academic_year
Browse files Browse the repository at this point in the history
This adds a new column to sessions for capturing the academic year of
the session, which will eventually form part of a unique index between
the academic year, team and location.

The immunisation import has also been changed to create sessions for the
academic year, and attach multiple dates to it.

After this, the next step is to update the sessions pages in the service
to display the information as in the prototype.
  • Loading branch information
thomasleese committed Sep 25, 2024
1 parent 242e942 commit 3a598ae
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 17 deletions.
7 changes: 6 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ def create
team = current_user.team

@session =
Session.create!(active: false, team:, programmes: team.programmes)
Session.create!(
active: false,
academic_year: Date.current.academic_year,
team:,
programmes: team.programmes
)

redirect_to session_edit_path(@session, :location)
end
Expand Down
25 changes: 13 additions & 12 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,20 @@ def session
return unless valid?

@session ||=
if (session = Session.for_date(session_date).find_by(team:, location:))
unless session.programmes.include?(@programme)
session.programmes << @programme
Session
.create_with(active: false)
.find_or_create_by!(
team:,
location:,
academic_year: session_date.academic_year
)
.tap do |session|
unless session.programmes.include?(@programme)
session.programmes << @programme
end

session.dates.find_or_create_by!(value: session_date)
end
session
else
ActiveRecord::Base.transaction do
session = Session.create!(active: false, team:, location:)
session.dates.create!(value: session_date)
session.programmes << @programme
session
end
end
end

def patient_session
Expand Down
1 change: 1 addition & 0 deletions app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: sessions
#
# id :bigint not null, primary key
# academic_year :integer not null
# active :boolean default(FALSE), not null
# close_consent_at :date
# send_consent_reminders_at :date
Expand Down
20 changes: 20 additions & 0 deletions db/migrate/20240925132918_add_academic_year_to_sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class AddAcademicYearToSessions < ActiveRecord::Migration[7.2]
def up
add_column :sessions, :academic_year, :integer

Session.all.find_each do |session|
session.update!(
academic_year:
(session.dates.map(&:value).min || Date.current).academic_year
)
end

change_column_null :sessions, :academic_year, false
end

def down
remove_column :sessions, :academic_year
end
end
3 changes: 2 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_25_121323) do
ActiveRecord::Schema[7.2].define(version: 2024_09_25_132918) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -477,6 +477,7 @@
t.date "send_consent_reminders_at"
t.date "close_consent_at"
t.bigint "team_id", null: false
t.integer "academic_year", null: false
t.index ["team_id"], name: "index_sessions_on_team_id"
end

Expand Down
Binary file modified erd.pdf
Binary file not shown.
8 changes: 5 additions & 3 deletions spec/factories/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: sessions
#
# id :bigint not null, primary key
# academic_year :integer not null
# active :boolean default(FALSE), not null
# close_consent_at :date
# send_consent_reminders_at :date
Expand All @@ -29,6 +30,7 @@
programme { association :programme }
end

academic_year { date.academic_year }
programmes { [programme] }
team { programmes.first&.team || association(:team) }
location { association :location, :school }
Expand All @@ -49,15 +51,15 @@
end

trait :in_progress do
date { Time.zone.now }
date { Date.current }
end

trait :in_future do
date { Time.zone.now + 1.week }
date { Date.current + 1.week }
end

trait :in_past do
date { Time.zone.now - 1.week }
date { Date.current - 1.week }
end

trait :minimal do
Expand Down
1 change: 1 addition & 0 deletions spec/models/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: sessions
#
# id :bigint not null, primary key
# academic_year :integer not null
# active :boolean default(FALSE), not null
# close_consent_at :date
# send_consent_reminders_at :date
Expand Down

0 comments on commit 3a598ae

Please sign in to comment.