Skip to content

Commit fb5cef8

Browse files
authored
Merge pull request #6412 from NHSDigital/consent-notification-team-location-2
2 parents 5794b15 + 39d39ef commit fb5cef8

5 files changed

Lines changed: 16 additions & 27 deletions

File tree

app/models/consent_notification.rb

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# patient_id :bigint not null
1212
# sent_by_user_id :bigint
1313
# session_id :bigint
14-
# team_location_id :bigint
14+
# team_location_id :bigint not null
1515
#
1616
# Indexes
1717
#
@@ -35,29 +35,13 @@ class ConsentNotification < ApplicationRecord
3535

3636
belongs_to :patient
3737
belongs_to :session, optional: true
38-
belongs_to :team_location, optional: true
38+
belongs_to :team_location
3939

40-
scope :left_joins_session_team_location, -> { joins(<<-SQL) }
41-
LEFT JOIN sessions
42-
ON sessions.id = consent_notifications.session_id
43-
LEFT JOIN team_locations AS session_team_locations
44-
ON session_team_locations.id = sessions.team_location_id
45-
SQL
40+
has_one :team, through: :team_location
4641

4742
scope :for_academic_year,
4843
->(academic_year) do
49-
joined_scope =
50-
left_joins(:team_location).left_joins_session_team_location
51-
52-
joined_scope.where(
53-
"session_team_locations.academic_year = ?",
54-
academic_year
55-
).or(
56-
joined_scope.where(
57-
"team_locations.academic_year = ?",
58-
academic_year
59-
)
60-
)
44+
joins(:team_location).where(team_locations: { academic_year: })
6145
end
6246

6347
enum :type,
@@ -66,9 +50,7 @@ class ConsentNotification < ApplicationRecord
6650

6751
scope :reminder, -> { initial_reminder.or(subsequent_reminder) }
6852

69-
def team = (team_location || session).team
70-
71-
def academic_year = (team_location || session).academic_year
53+
delegate :academic_year, to: :team_location
7254

7355
def reminder? = initial_reminder? || subsequent_reminder?
7456

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class MakeConsentNotificationTeamLocationNotNull < ActiveRecord::Migration[8.1]
4+
def change
5+
change_column_null :consent_notifications, :team_location_id, false
6+
end
7+
end

db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
t.datetime "sent_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
296296
t.bigint "sent_by_user_id"
297297
t.bigint "session_id"
298-
t.bigint "team_location_id"
298+
t.bigint "team_location_id", null: false
299299
t.integer "type", null: false
300300
t.index ["patient_id"], name: "index_consent_notifications_on_patient_id"
301301
t.index ["programme_types"], name: "index_consent_notifications_on_programme_types", using: :gin

spec/factories/consent_notifications.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# patient_id :bigint not null
1212
# sent_by_user_id :bigint
1313
# session_id :bigint
14-
# team_location_id :bigint
14+
# team_location_id :bigint not null
1515
#
1616
# Indexes
1717
#

spec/models/consent_notification_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# patient_id :bigint not null
1212
# sent_by_user_id :bigint
1313
# session_id :bigint
14-
# team_location_id :bigint
14+
# team_location_id :bigint not null
1515
#
1616
# Indexes
1717
#
@@ -31,6 +31,6 @@
3131
describe "associations" do
3232
it { should belong_to(:patient) }
3333
it { should belong_to(:session).optional(true) }
34-
it { should belong_to(:team_location).optional(true) }
34+
it { should belong_to(:team_location) }
3535
end
3636
end

0 commit comments

Comments
 (0)