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
26 changes: 8 additions & 18 deletions app/controllers/concerns/vaccination_mailer_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ def send_vaccination_confirmation(vaccination_record)
parents = parents_for_vaccination_mailer(vaccination_record)
return if parents.empty?

patient = vaccination_record.patient_session.patient
sent_by = current_user

mailer_action =
if vaccination_record.administered?
:confirmation_administered
Expand All @@ -20,7 +17,7 @@ def send_vaccination_confirmation(vaccination_record)
text_template_name = :"vaccination_#{mailer_action}"

parents.each do |parent|
params = { parent:, patient:, vaccination_record:, sent_by: }
params = { parent:, vaccination_record:, sent_by: try(:current_user) }

if parent.email.present?
VaccinationMailer.with(params).public_send(mailer_action).deliver_later
Expand All @@ -34,11 +31,8 @@ def send_vaccination_deletion(vaccination_record)
parents = parents_for_vaccination_mailer(vaccination_record)
return if parents.empty?

patient = vaccination_record.patient_session.patient
sent_by = current_user

parents.each do |parent|
params = { parent:, patient:, vaccination_record:, sent_by: }
params = { parent:, vaccination_record:, sent_by: try(:current_user) }

if parent.email.present?
VaccinationMailer.with(params).deleted.deliver_later
Expand All @@ -54,17 +48,13 @@ def parents_for_vaccination_mailer(vaccination_record)

consents = patient_session.latest_consents

if consents.any?(&:via_self_consent?)
if consents.any?(&:notify_parents)
patient.parents.select(&:contactable?)
parents =
if consents.any?(&:via_self_consent?)
consents.any?(&:notify_parents) ? patient.parents : []
else
[]
consents.select(&:response_given?).filter_map(&:parent)
end
else
consents
.select(&:response_given?)
.filter_map(&:parent)
.select(&:contactable?)
end

parents.select(&:contactable?)
end
end
14 changes: 13 additions & 1 deletion app/controllers/draft_vaccination_records_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,22 @@ def handle_date_and_time
def handle_confirm
return unless @draft_vaccination_record.save

performed_at_date_changed =
@vaccination_record.performed_at&.to_date !=
@draft_vaccination_record.performed_at.to_date

@draft_vaccination_record.write_to!(@vaccination_record)

should_notify_parents =
@vaccination_record.confirmation_sent? &&
(
@vaccination_record.outcome_changed? ||
@vaccination_record.batch_id_changed? || performed_at_date_changed
)

@vaccination_record.save!

send_vaccination_confirmation(@vaccination_record)
send_vaccination_confirmation(@vaccination_record) if should_notify_parents

heading =
if @vaccination_record.administered?
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/vaccinations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def destroy

@vaccination_record.discard!

send_vaccination_deletion(@vaccination_record)
if @vaccination_record.confirmation_sent?
send_vaccination_deletion(@vaccination_record)
end

redirect_to session_patient_path(id: @patient.id),
flash: {
Expand Down
27 changes: 27 additions & 0 deletions app/jobs/vaccination_confirmations_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class VaccinationConfirmationsJob < ApplicationJob
include VaccinationMailerConcern

queue_as :notifications

def perform
# Find the oldest record that has had a confirmation sent, and send confirmations for all subsequent records
since =
VaccinationRecord
.kept
.where.not(confirmation_sent_at: nil)
.maximum(:created_at) || 24.hours.ago
academic_year = Date.current.academic_year

VaccinationRecord
.kept
.where("created_at >= ?", since)
.where(confirmation_sent_at: nil)
.select { _1.academic_year == academic_year }
.each do |vaccation_record|
send_vaccination_confirmation(vaccation_record)
vaccation_record.update!(confirmation_sent_at: Time.current)
end
end
end
9 changes: 9 additions & 0 deletions app/models/vaccination_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: vaccination_records
#
# id :bigint not null, primary key
# confirmation_sent_at :datetime
# delivery_method :integer
# delivery_site :integer
# discarded_at :datetime
Expand Down Expand Up @@ -162,6 +163,10 @@ def not_administered?
!administered?
end

def confirmation_sent?
confirmation_sent_at != nil
end

def retryable_reason?
not_well? || contraindications? || absent_from_session? ||
absent_from_school? || refused?
Expand All @@ -173,6 +178,10 @@ def dose_volume_ml
vaccine.dose_volume_ml * 1 if vaccine.present?
end

def academic_year
performed_at.to_date.academic_year
end

private

def requires_location_name?
Expand Down
5 changes: 5 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@
cron: "every day at 2am",
class: "TrimActiveRecordSessionsJob",
description: "Remove ActiveRecord sessions older than 30 days"
},
vaccination_confirmations: {
cron: "every day at 7pm",
class: "VaccinationConfirmationsJob",
description: "Send vaccination confirmation emails to parents"
}
}
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddConfirmationSentAtToVaccinationRecords < ActiveRecord::Migration[7.2]
def change
add_column :vaccination_records, :confirmation_sent_at, :datetime
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_11_18_125157) do
ActiveRecord::Schema[7.2].define(version: 2024_11_20_140814) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "plpgsql"
Expand Down Expand Up @@ -693,6 +693,7 @@
t.bigint "programme_id", null: false
t.string "location_name"
t.datetime "discarded_at"
t.datetime "confirmation_sent_at"
t.index ["batch_id"], name: "index_vaccination_records_on_batch_id"
t.index ["discarded_at"], name: "index_vaccination_records_on_discarded_at"
t.index ["patient_session_id"], name: "index_vaccination_records_on_patient_session_id"
Expand Down
Binary file modified erd.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def initialize(current_user:)
).with(
params: {
parent:,
patient:,
vaccination_record:,
sent_by: current_user
},
Expand All @@ -50,7 +49,7 @@ def initialize(current_user:)
it "sends a text message" do
expect { send_vaccination_confirmation }.to have_enqueued_text(
:vaccination_confirmation_administered
).with(parent:, patient:, vaccination_record:, sent_by: current_user)
).with(parent:, vaccination_record:, sent_by: current_user)
end
end

Expand All @@ -71,7 +70,6 @@ def initialize(current_user:)
).with(
params: {
parent:,
patient:,
vaccination_record:,
sent_by: current_user
},
Expand All @@ -82,7 +80,7 @@ def initialize(current_user:)
it "sends a text message" do
expect { send_vaccination_confirmation }.to have_enqueued_text(
:vaccination_confirmation_not_administered
).with(parent:, patient:, vaccination_record:, sent_by: current_user)
).with(parent:, vaccination_record:, sent_by: current_user)
end
end

Expand All @@ -103,7 +101,6 @@ def initialize(current_user:)
).with(
params: {
parent:,
patient:,
vaccination_record:,
sent_by: current_user
},
Expand All @@ -114,7 +111,7 @@ def initialize(current_user:)
it "sends a text message" do
expect { send_vaccination_confirmation }.to have_enqueued_text(
:vaccination_confirmation_administered
).with(parent:, patient:, vaccination_record:, sent_by: current_user)
).with(parent:, vaccination_record:, sent_by: current_user)
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/factories/vaccination_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: vaccination_records
#
# id :bigint not null, primary key
# confirmation_sent_at :datetime
# delivery_method :integer
# delivery_site :integer
# discarded_at :datetime
Expand Down Expand Up @@ -98,5 +99,9 @@
trait :discarded do
discarded_at { Time.current }
end

trait :confirmation_sent do
confirmation_sent_at { Time.current }
end
end
end
69 changes: 60 additions & 9 deletions spec/features/delete_vaccination_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

describe "Delete vaccination record" do
before { Flipper.enable(:release_1b) }

after { Flipper.disable(:release_1b) }

scenario "User deletes a vaccination record" do
scenario "User doesn't delete the record" do
given_an_hpv_programme_is_underway
and_an_administered_vaccination_record_exists

Expand All @@ -16,6 +15,49 @@
when_i_dont_delete_the_vaccination_record
then_i_see_the_patient
and_they_are_already_vaccinated
end

scenario "User deletes a record and checks activity log" do
given_an_hpv_programme_is_underway
and_an_administered_vaccination_record_exists

when_i_go_to_a_patient_that_is_vaccinated
and_i_click_on_delete_vaccination_record
then_i_see_the_delete_vaccination_page

when_i_delete_the_vaccination_record
then_i_see_the_patient
and_i_see_a_successful_message
and_they_can_be_vaccinated

when_i_click_on_the_log
then_i_see_the_delete_vaccination
end

scenario "User deletes a record before confirmation is sent" do
given_an_hpv_programme_is_underway
and_an_administered_vaccination_record_exists

when_i_go_to_a_patient_that_is_vaccinated
and_i_click_on_delete_vaccination_record
then_i_see_the_delete_vaccination_page

when_i_delete_the_vaccination_record
then_i_see_the_patient
and_i_see_a_successful_message
and_they_can_be_vaccinated

when_i_click_on_the_log
then_i_see_the_delete_vaccination
and_the_parent_doesnt_receives_an_email
end

scenario "User deletes a record after confirmation is sent" do
given_an_hpv_programme_is_underway
and_an_administered_vaccination_record_exists
and_a_confirmation_email_has_been_sent

when_i_go_to_a_patient_that_is_vaccinated
and_i_click_on_delete_vaccination_record
then_i_see_the_delete_vaccination_page

Expand All @@ -29,7 +71,7 @@
and_the_parent_receives_an_email
end

scenario "User deletes a vaccination record on a closed session date" do
scenario "User tries to delete a record for a closed session date" do
given_an_hpv_programme_is_underway
and_an_administered_vaccination_record_exists
and_the_session_has_closed
Expand Down Expand Up @@ -70,12 +112,17 @@ def and_an_administered_vaccination_record_exists

batch = create(:batch, organisation: @organisation, vaccine:)

create(
:vaccination_record,
programme: @programme,
patient_session: @patient_session,
batch:
)
@vaccination_record =
create(
:vaccination_record,
programme: @programme,
patient_session: @patient_session,
batch:
)
end

def and_a_confirmation_email_has_been_sent
@vaccination_record.update(confirmation_sent_at: Time.current)
end

def and_the_session_has_closed
Expand Down Expand Up @@ -137,6 +184,10 @@ def and_the_parent_receives_an_email
expect_email_to(@patient.parents.first.email, :vaccination_deleted)
end

def and_the_parent_doesnt_receives_an_email
expect(sent_emails).to be_empty
end

def then_i_cant_click_on_delete_vaccination_record
expect(page).not_to have_content("Delete vaccination record")
end
Expand Down
Loading
Loading