Skip to content
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
20 changes: 9 additions & 11 deletions app/controllers/sessions/edit_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def show
def update
case current_step
when :confirm
@session.draft = false
@session.active = true
@session.patient_sessions.update_all(active: true)

if @session.send_consent_at.today?
Expand Down Expand Up @@ -109,16 +109,14 @@ def set_patients
@session
.location
.patients
.where(
"NOT EXISTS (:sessions)",
sessions:
Session
.select(1)
.joins(:patient_sessions)
.where(
"patient_sessions.patient_id = patients.id AND draft = false AND campaign_id = :campaign_id",
campaign_id: @session.campaign_id
)
.where.not(
Session
.joins(:patient_sessions)
.active
.where(campaign: @session.campaign)
.where("patient_sessions.patient_id = patients.id")
.arel
.exists
)
.sort_by(&:last_name)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def create

campaign = current_user.team.campaigns.first

@session = Session.create!(draft: true, campaign:)
@session = Session.create!(active: false, campaign:)

redirect_to session_edit_path(@session, :location)
end
Expand Down
3 changes: 1 addition & 2 deletions app/models/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# fk_rails_... (team_id => teams.id)
#
class Campaign < ApplicationRecord
include Draftable
include WizardStepConcern

self.inheritance_column = nil
Expand All @@ -44,8 +45,6 @@ class Campaign < ApplicationRecord

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

scope :active, -> { where(active: true) }

normalizes :name, with: ->(name) { name&.strip }

on_wizard_step :details do
Expand Down
14 changes: 14 additions & 0 deletions app/models/concerns/draftable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Draftable
extend ActiveSupport::Concern

included do
scope :active, -> { where(active: true) }
scope :draft, -> { where(active: false) }
end

def draft?
!active
end
end
2 changes: 1 addition & 1 deletion app/models/immunisation_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def record!
end

if (session = vaccination_record.session).draft?
session.update!(draft: false)
session.update!(active: true)
end

vaccination_record.update!(recorded_at:)
Expand Down
2 changes: 1 addition & 1 deletion app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def session
.sessions
.active
.or(Session.where(imported_from:))
.create_with(imported_from:, draft: true)
.create_with(imported_from:, active: false)
.find_or_create_by!(
date: session_date,
location:,
Expand Down
8 changes: 2 additions & 6 deletions app/models/patient_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#

class PatientSession < ApplicationRecord
include Draftable

audited
has_associated_audits

Expand All @@ -50,12 +52,6 @@ class PatientSession < ApplicationRecord
through: :patient,
class_name: "Consent"

scope :active, -> { where(active: true) }

def draft?
!active
end

def vaccination_record
# HACK: in future, it will be possible to have multiple vaccination records for a patient session
vaccination_records.recorded.last
Expand Down
16 changes: 4 additions & 12 deletions app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# Table name: sessions
#
# id :bigint not null, primary key
# active :boolean default(FALSE), not null
# close_consent_at :date
# date :date
# draft :boolean default(FALSE)
# send_consent_at :date
# send_reminders_at :date
# time_of_day :integer
Expand All @@ -27,7 +27,9 @@
# fk_rails_... (imported_from_id => immunisation_imports.id)
#
class Session < ApplicationRecord
include Draftable
include WizardStepConcern

audited

DEFAULT_DAYS_FOR_REMINDER = 2
Expand All @@ -46,20 +48,14 @@ class Session < ApplicationRecord

enum :time_of_day, %w[morning afternoon all_day]

scope :active, -> { where(draft: false) }
scope :draft, -> { where(draft: true) }
scope :past, -> { where(date: ..Time.zone.yesterday) }
scope :in_progress, -> { where(date: Time.zone.today) }
scope :future, -> { where(date: Time.zone.tomorrow..) }

after_initialize :set_timeline_attributes
after_validation :set_timeline_timestamps

validates :time_of_day,
inclusion: {
in: time_of_days.keys
},
unless: -> { draft? }
validates :time_of_day, inclusion: { in: time_of_days.keys }, unless: :draft?

on_wizard_step :location, exact: true do
validates :location_id, presence: true
Expand Down Expand Up @@ -98,10 +94,6 @@ class Session < ApplicationRecord
if: -> { close_consent_on == "custom" }
end

def active?
!draft
end

def health_questions
campaign.vaccines.first.health_questions
end
Expand Down
21 changes: 21 additions & 0 deletions db/migrate/20240905113128_rename_session_draft_to_active.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class RenameSessionDraftToActive < ActiveRecord::Migration[7.2]
def up
change_table :sessions, bulk: true do |t|
t.rename :draft, :active
t.change_null :active, false
end

Session.update_all("active = NOT active")
end

def down
Session.update_all("active = NOT active")

change_table :sessions, bulk: true do |t|
t.rename :active, :draft
t.change_null :draft, true
end
end
end
4 changes: 2 additions & 2 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_05_090740) do
ActiveRecord::Schema[7.2].define(version: 2024_09_05_113128) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -360,7 +360,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "campaign_id"
t.boolean "draft", default: false
t.boolean "active", default: false, null: false
t.date "send_consent_at"
t.date "send_reminders_at"
t.date "close_consent_at"
Expand Down
Binary file modified erd.pdf
Binary file not shown.
1 change: 0 additions & 1 deletion spec/components/app_session_details_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
context "for a session with the minimum amount of information" do
let(:session) do
Session.new(
draft: false,
location: create(:location, :school),
campaign: create(:campaign, :hpv),
date:,
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/patient_sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
patient { association :patient, session:, **patient_attributes }
created_by { association :user }

active { session.active? }
active { session.active }

trait :active do
active { true }
Expand Down
12 changes: 11 additions & 1 deletion spec/factories/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# Table name: sessions
#
# id :bigint not null, primary key
# active :boolean default(FALSE), not null
# close_consent_at :date
# date :date
# draft :boolean default(FALSE)
# send_consent_at :date
# send_reminders_at :date
# time_of_day :integer
Expand Down Expand Up @@ -40,6 +40,16 @@

time_of_day { %w[morning afternoon all_day].sample }

active { campaign.active }

trait :active do
active { true }
end

trait :draft do
active { false }
end

trait :in_progress do
date { Time.zone.now }
end
Expand Down
9 changes: 3 additions & 6 deletions spec/jobs/consent_reminders_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

context "with draft and active sessions" do
it "enqueues ConsentRemindersSessionBatchJob for each active sessions" do
active_session =
create(:session, draft: false, send_reminders_at: Time.zone.today)
active_session = create(:session, send_reminders_at: Time.zone.today)
_draft_session =
create(:session, draft: true, campaign: active_session.campaign)
create(:session, :draft, campaign: active_session.campaign)

described_class.perform_now
expect(ConsentRemindersSessionBatchJob).to have_been_enqueued.once
Expand All @@ -23,12 +22,10 @@

context "with sessions set to send consent today and in the future" do
it "enqueues ConsentRemindersSessionBatchJob for the session set to send consent today" do
active_session =
create(:session, draft: false, send_reminders_at: Time.zone.today)
active_session = create(:session, send_reminders_at: Time.zone.today)
_later_session =
create(
:session,
draft: false,
send_reminders_at: 2.days.from_now,
campaign: active_session.campaign
)
Expand Down
25 changes: 5 additions & 20 deletions spec/jobs/consent_requests_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
end

let(:campaign) { create(:campaign) }
let(:campaign) { create(:campaign, :active) }

context "with draft and active sessions" do
it "enqueues ConsentRequestsSessionBatchJob for each active sessions" do
active_session =
create(
:session,
draft: false,
send_consent_at: Time.zone.today,
campaign:
)
_draft_session = create(:session, draft: true, campaign:)
create(:session, send_consent_at: Time.zone.today, campaign:)
_draft_session = create(:session, :draft, campaign:)

described_class.perform_now
expect(ConsentRequestsSessionBatchJob).to have_been_enqueued.once
Expand All @@ -30,19 +25,9 @@
context "with sessions set to send consent today and in the future" do
it "enqueues ConsentRequestsSessionBatchJob for the session set to send consent today" do
active_session =
create(
:session,
draft: false,
send_consent_at: Time.zone.today,
campaign:
)
create(:session, send_consent_at: Time.zone.today, campaign:)
_later_session =
create(
:session,
draft: false,
send_consent_at: 2.days.from_now,
campaign:
)
create(:session, send_consent_at: 2.days.from_now, campaign:)

described_class.perform_now
expect(ConsentRequestsSessionBatchJob).to have_been_enqueued.once
Expand Down
8 changes: 3 additions & 5 deletions spec/jobs/session_reminders_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
end

let(:campaign) { create(:campaign) }
let(:campaign) { create(:campaign, :active) }

it "enqueues SessionRemdindersJob for each session happening tomorrow" do
tomorrow_session = create(:session, date: Date.tomorrow, campaign:)
Expand All @@ -22,10 +22,8 @@

context "with draft and active sessions" do
it "enqueues ConsentRemindersSessionBatchJob for each active sessions" do
active_session =
create(:session, draft: false, date: Date.tomorrow, campaign:)
_draft_session =
create(:session, draft: true, date: Date.tomorrow, campaign:)
active_session = create(:session, date: Date.tomorrow, campaign:)
_draft_session = create(:session, :draft, date: Date.tomorrow, campaign:)

described_class.perform_now
expect(SessionRemindersBatchJob).to have_been_enqueued.once
Expand Down
Loading