diff --git a/app/controllers/api/testing/refresh_reporting_controller.rb b/app/controllers/api/testing/refresh_reporting_controller.rb index 22753cea58..9a7b22b52d 100644 --- a/app/controllers/api/testing/refresh_reporting_controller.rb +++ b/app/controllers/api/testing/refresh_reporting_controller.rb @@ -3,10 +3,10 @@ class API::Testing::RefreshReportingController < API::Testing::BaseController def create if params[:wait].present? - ReportingAPI::RefreshJob.perform_now + ReportingAPI::RefreshJob.new.perform render status: :ok else - ReportingAPI::RefreshJob.perform_later + ReportingAPI::RefreshJob.perform_async render status: :accepted end end diff --git a/app/controllers/api/testing/vaccinations_search_in_nhs_controller.rb b/app/controllers/api/testing/vaccinations_search_in_nhs_controller.rb index 7a18a3b691..6c98b20b45 100644 --- a/app/controllers/api/testing/vaccinations_search_in_nhs_controller.rb +++ b/app/controllers/api/testing/vaccinations_search_in_nhs_controller.rb @@ -6,11 +6,11 @@ class API::Testing::VaccinationsSearchInNHSController < API::Testing::BaseContro def create if params[:wait].present? - EnqueueVaccinationsSearchInNHSJob.perform_now + EnqueueVaccinationsSearchInNHSJob.new.perform wait_for_search_jobs_to_complete render status: :ok else - EnqueueVaccinationsSearchInNHSJob.perform_later + EnqueueVaccinationsSearchInNHSJob.perform_async render status: :accepted end end diff --git a/app/controllers/class_imports_controller.rb b/app/controllers/class_imports_controller.rb index d33c02922e..26c8fcaffc 100644 --- a/app/controllers/class_imports_controller.rb +++ b/app/controllers/class_imports_controller.rb @@ -29,7 +29,7 @@ def create ) if @class_import.save - ProcessImportJob.perform_later(@class_import) + ProcessImportSidekiqJob.perform_async(@class_import.to_global_id.to_s) redirect_to imports_path, flash: { success: "Import processing started" } else render :new, status: :unprocessable_content and return diff --git a/app/controllers/cohort_imports_controller.rb b/app/controllers/cohort_imports_controller.rb index f4a7035220..c82d291c3d 100644 --- a/app/controllers/cohort_imports_controller.rb +++ b/app/controllers/cohort_imports_controller.rb @@ -27,7 +27,7 @@ def create ) if @cohort_import.save - ProcessImportJob.perform_later(@cohort_import) + ProcessImportSidekiqJob.perform_async(@cohort_import.to_global_id.to_s) redirect_to imports_path, flash: { success: "Import processing started" } else render :new, status: :unprocessable_content and return diff --git a/app/controllers/consent_forms_controller.rb b/app/controllers/consent_forms_controller.rb index 9da1c7fcf2..9521c2e3e6 100644 --- a/app/controllers/consent_forms_controller.rb +++ b/app/controllers/consent_forms_controller.rb @@ -118,9 +118,9 @@ def create_patient end if patient.nhs_number.nil? - PatientNHSNumberLookupJob.perform_later(patient) + PatientNHSNumberLookupSidekiqJob.perform_async(patient.id) else - PatientUpdateFromPDSJob.perform_later(patient) + PatientUpdateFromPDSSidekiqJob.perform_async(patient.id, nil) end flash[:success] = "#{patient.full_name}’s record created from a consent \ diff --git a/app/controllers/immunisation_imports_controller.rb b/app/controllers/immunisation_imports_controller.rb index d6c07a3cdb..dfdcbe76a6 100644 --- a/app/controllers/immunisation_imports_controller.rb +++ b/app/controllers/immunisation_imports_controller.rb @@ -23,7 +23,9 @@ def create ) if @immunisation_import.save - ProcessImportJob.perform_later(@immunisation_import) + ProcessImportSidekiqJob.perform_async( + @immunisation_import.to_global_id.to_s + ) redirect_to imports_path, flash: { success: "Import processing started" } else render :new, status: :unprocessable_content and return diff --git a/app/controllers/parent_interface/consent_forms/edit_controller.rb b/app/controllers/parent_interface/consent_forms/edit_controller.rb index 04adc70834..08449ba6cb 100644 --- a/app/controllers/parent_interface/consent_forms/edit_controller.rb +++ b/app/controllers/parent_interface/consent_forms/edit_controller.rb @@ -96,7 +96,7 @@ def handle_ethnicity_completion!(model) # parent decides to answer "Yes" or "No" (they might not) # to the ethnicity questions, the job will make sure the # ethnicity information is copied to the matched patient. - ProcessConsentFormJob.perform_later(model.id) + ProcessConsentFormSidekiqJob.perform_async(model.id) redirect_to submitted_parent_interface_consent_form_path(model) end diff --git a/app/controllers/parent_interface/consent_forms_controller.rb b/app/controllers/parent_interface/consent_forms_controller.rb index 9a7b1ffece..d06dbf3fec 100644 --- a/app/controllers/parent_interface/consent_forms_controller.rb +++ b/app/controllers/parent_interface/consent_forms_controller.rb @@ -67,7 +67,7 @@ def record # answering. Running the job ensures the consent form is still # processed and, if ethnicity was provided, it gets copied # onto the matched patient. - ProcessConsentFormJob.perform_later(@consent_form.id) + ProcessConsentFormSidekiqJob.perform_async(@consent_form.id) if Flipper.enabled?(:ethnicity_capture) redirect_to parent_interface_consent_form_edit_path( diff --git a/app/controllers/patients/edit_controller.rb b/app/controllers/patients/edit_controller.rb index 967d0335c9..ceb13dd370 100644 --- a/app/controllers/patients/edit_controller.rb +++ b/app/controllers/patients/edit_controller.rb @@ -22,7 +22,9 @@ def update_nhs_number @patient.invalidated_at = nil if @patient.save - PatientUpdateFromPDSJob.perform_later(@patient) + if @patient.nhs_number.present? + PatientUpdateFromPDSSidekiqJob.perform_async(@patient.id, nil) + end redirect_to edit_patient_path(@patient) else diff --git a/app/controllers/sessions/manage_consent_reminders_controller.rb b/app/controllers/sessions/manage_consent_reminders_controller.rb index 07806715bb..07e05942fc 100644 --- a/app/controllers/sessions/manage_consent_reminders_controller.rb +++ b/app/controllers/sessions/manage_consent_reminders_controller.rb @@ -7,7 +7,10 @@ def show end def create - SendManualSchoolConsentRemindersJob.perform_now(@session, current_user:) + SendManualSchoolConsentRemindersJob.perform_async( + @session.id, + current_user.id + ) redirect_to session_path(@session), flash: { diff --git a/app/forms/bulk_remove_parents_form.rb b/app/forms/bulk_remove_parents_form.rb index 934966fd5d..a26c3ed57a 100644 --- a/app/forms/bulk_remove_parents_form.rb +++ b/app/forms/bulk_remove_parents_form.rb @@ -31,7 +31,7 @@ def save! import .parent_relationship_ids .each_slice(BATCH_SIZE) do |batch_ids| - BulkRemoveParentRelationshipsJob.perform_later( + BulkRemoveParentRelationshipsSidekiqJob.perform_async( import.to_global_id.to_s, batch_ids, current_user.id, diff --git a/app/jobs/bulk_remove_parent_relationships_sidekiq_job.rb b/app/jobs/bulk_remove_parent_relationships_sidekiq_job.rb new file mode 100644 index 0000000000..d26bb0453e --- /dev/null +++ b/app/jobs/bulk_remove_parent_relationships_sidekiq_job.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class BulkRemoveParentRelationshipsSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :imports + + def perform( + import_global_id, + parent_relationship_ids_batch, + user_id, + remove_option + ) + BulkRemoveParentRelationshipsJob.new.perform( + import_global_id, + parent_relationship_ids_batch, + user_id, + remove_option + ) + end +end diff --git a/app/jobs/commit_patient_changesets_job.rb b/app/jobs/commit_patient_changesets_job.rb index 43d1d45c41..4bf5f5193e 100644 --- a/app/jobs/commit_patient_changesets_job.rb +++ b/app/jobs/commit_patient_changesets_job.rb @@ -100,7 +100,7 @@ def trigger_re_review(import) import.calculating_re_review! import.changesets.needs_re_review.each do |changeset| changeset.calculating_review! - ReviewPatientChangesetJob.perform_later(changeset.id) + ReviewPatientChangesetSidekiqJob.perform_async(changeset.id) end end end diff --git a/app/jobs/email_delivery_sidekiq_job.rb b/app/jobs/email_delivery_sidekiq_job.rb new file mode 100644 index 0000000000..64ea544d40 --- /dev/null +++ b/app/jobs/email_delivery_sidekiq_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class EmailDeliverySidekiqJob < NotifyDeliverySidekiqJob + include GovukNotifyThrottlingConcern + + def perform(template_name, params) + EmailDeliveryJob.new.perform(template_name, **fetch_params(params)) + end +end diff --git a/app/jobs/enqueue_automated_careplus_reports_job.rb b/app/jobs/enqueue_automated_careplus_reports_job.rb index 2be20986dd..a6d17dab2d 100644 --- a/app/jobs/enqueue_automated_careplus_reports_job.rb +++ b/app/jobs/enqueue_automated_careplus_reports_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class EnqueueAutomatedCareplusReportsJob < ApplicationJobActiveJob - queue_as :careplus +class EnqueueAutomatedCareplusReportsJob < ApplicationJobSidekiq + sidekiq_options queue: :careplus def perform ids = Team.eligible_for_automated_careplus_reports.ids diff --git a/app/jobs/enqueue_location_position_updater_job.rb b/app/jobs/enqueue_location_position_updater_job.rb index e27165ac01..59c1194b56 100644 --- a/app/jobs/enqueue_location_position_updater_job.rb +++ b/app/jobs/enqueue_location_position_updater_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class EnqueueLocationPositionUpdaterJob < ApplicationJobActiveJob - queue_as :third_party_data_imports +class EnqueueLocationPositionUpdaterJob < ApplicationJobSidekiq + sidekiq_options queue: :third_party_data_imports def perform ids = Location.where(position: nil).has_address.ids diff --git a/app/jobs/enqueue_patients_aged_out_of_schools_job.rb b/app/jobs/enqueue_patients_aged_out_of_schools_job.rb index 3304069786..589a686976 100644 --- a/app/jobs/enqueue_patients_aged_out_of_schools_job.rb +++ b/app/jobs/enqueue_patients_aged_out_of_schools_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class EnqueuePatientsAgedOutOfSchoolsJob < ApplicationJobActiveJob - queue_as :patients +class EnqueuePatientsAgedOutOfSchoolsJob < ApplicationJobSidekiq + sidekiq_options queue: :patients def perform academic_year = AcademicYear.pending diff --git a/app/jobs/enqueue_process_unmatched_consent_forms_job.rb b/app/jobs/enqueue_process_unmatched_consent_forms_job.rb index 3436735aa2..a6d1c7e056 100644 --- a/app/jobs/enqueue_process_unmatched_consent_forms_job.rb +++ b/app/jobs/enqueue_process_unmatched_consent_forms_job.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true -class EnqueueProcessUnmatchedConsentFormsJob < ApplicationJobActiveJob +class EnqueueProcessUnmatchedConsentFormsJob < ApplicationJobSidekiq include SingleConcurrencyConcern - queue_as :consents + sidekiq_options queue: :consents def perform ConsentForm.unmatched.find_each do |consent_form| - ProcessConsentFormJob.perform_later(consent_form.id) + ProcessConsentFormSidekiqJob.perform_async(consent_form.id) end end end diff --git a/app/jobs/enqueue_school_consent_reminders_job.rb b/app/jobs/enqueue_school_consent_reminders_job.rb index 4fe4f50597..07bef07662 100644 --- a/app/jobs/enqueue_school_consent_reminders_job.rb +++ b/app/jobs/enqueue_school_consent_reminders_job.rb @@ -1,17 +1,16 @@ # frozen_string_literal: true -class EnqueueSchoolConsentRemindersJob < ApplicationJobActiveJob - queue_as :notifications +class EnqueueSchoolConsentRemindersJob < ApplicationJobSidekiq + sidekiq_options queue: :notifications def perform - sessions = + session_ids = Session .send_consent_reminders .joins(:location) .merge(Location.gias_school) + .ids - sessions.find_each do |session| - SendAutomaticSchoolConsentRemindersJob.perform_later(session) - end + SendAutomaticSchoolConsentRemindersSidekiqJob.perform_bulk(session_ids.zip) end end diff --git a/app/jobs/enqueue_school_consent_requests_job.rb b/app/jobs/enqueue_school_consent_requests_job.rb index 44b42cada9..a5e4879045 100644 --- a/app/jobs/enqueue_school_consent_requests_job.rb +++ b/app/jobs/enqueue_school_consent_requests_job.rb @@ -1,14 +1,16 @@ # frozen_string_literal: true -class EnqueueSchoolConsentRequestsJob < ApplicationJobActiveJob - queue_as :notifications +class EnqueueSchoolConsentRequestsJob < ApplicationJobSidekiq + sidekiq_options queue: :notifications def perform - sessions = - Session.send_consent_requests.joins(:location).merge(Location.gias_school) + session_ids = + Session + .send_consent_requests + .joins(:location) + .merge(Location.gias_school) + .ids - sessions.find_each do |session| - SendSchoolConsentRequestsJob.perform_later(session) - end + SendSchoolConsentRequestsSidekiqJob.perform_bulk(session_ids.zip) end end diff --git a/app/jobs/enqueue_school_session_reminders_job.rb b/app/jobs/enqueue_school_session_reminders_job.rb index 434ef9c167..6fdbeb1e53 100644 --- a/app/jobs/enqueue_school_session_reminders_job.rb +++ b/app/jobs/enqueue_school_session_reminders_job.rb @@ -1,18 +1,17 @@ # frozen_string_literal: true -class EnqueueSchoolSessionRemindersJob < ApplicationJobActiveJob - queue_as :notifications +class EnqueueSchoolSessionRemindersJob < ApplicationJobSidekiq + sidekiq_options queue: :notifications def perform - sessions = + session_ids = Session .includes(:session_programme_year_groups) .has_date(Date.tomorrow) .joins(:location) .merge(Location.gias_school) + .ids - sessions.find_each do |session| - SendSchoolSessionRemindersJob.perform_later(session) - end + SendSchoolSessionRemindersSidekiqJob.perform_bulk(session_ids.zip) end end diff --git a/app/jobs/enqueue_update_patients_from_pds_job.rb b/app/jobs/enqueue_update_patients_from_pds_job.rb index e6c420a9a9..525ce99b3b 100644 --- a/app/jobs/enqueue_update_patients_from_pds_job.rb +++ b/app/jobs/enqueue_update_patients_from_pds_job.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -class EnqueueUpdatePatientsFromPDSJob < ApplicationJobActiveJob +class EnqueueUpdatePatientsFromPDSJob < ApplicationJobSidekiq include SingleConcurrencyConcern - queue_as :pds + sidekiq_options queue: :pds def perform scope = Patient.not_deceased diff --git a/app/jobs/enqueue_vaccinations_search_in_nhs_job.rb b/app/jobs/enqueue_vaccinations_search_in_nhs_job.rb index 52d0200b51..4d673880f0 100644 --- a/app/jobs/enqueue_vaccinations_search_in_nhs_job.rb +++ b/app/jobs/enqueue_vaccinations_search_in_nhs_job.rb @@ -7,10 +7,10 @@ # sessions, starting from 2 days before invitations or consent requests are sent # out and ending once the last date of the sessions has passed. For all other # patients we want to ensure a search is performed every 28 days at most. -class EnqueueVaccinationsSearchInNHSJob < ApplicationJobActiveJob - queue_as :immunisations_api_search +class EnqueueVaccinationsSearchInNHSJob < ApplicationJobSidekiq + sidekiq_options queue: :immunisations_api_search - def perform(programme_types: nil) + def perform(programme_types = nil) programme_types ||= Programme.all.map(&:type) patient_ids = [] diff --git a/app/jobs/gias_import_job.rb b/app/jobs/gias_import_job.rb index da419e913a..9a4f8e15f4 100644 --- a/app/jobs/gias_import_job.rb +++ b/app/jobs/gias_import_job.rb @@ -1,16 +1,16 @@ # frozen_string_literal: true -class GIASImportJob < ApplicationJobActiveJob +class GIASImportJob < ApplicationJobSidekiq include SingleConcurrencyConcern - queue_as :third_party_data_imports + sidekiq_options queue: :third_party_data_imports - def perform(dry_run: false) + def perform GIAS.download results = GIAS.check_import GIAS.log_import_check_results(results) - GIAS.import unless dry_run + GIAS.import end end diff --git a/app/jobs/important_notice_generator_job.rb b/app/jobs/important_notice_generator_job.rb index 2f4f8f0b88..322c16819b 100644 --- a/app/jobs/important_notice_generator_job.rb +++ b/app/jobs/important_notice_generator_job.rb @@ -11,7 +11,7 @@ class ImportantNoticeGeneratorJob < ApplicationJobActiveJob restricted: :restricted_at }.freeze - def perform(patient_ids = nil) + def perform(patient_ids) scope = Patient.includes(:teams, vaccination_records: %i[team]) if patient_ids.present? diff --git a/app/jobs/important_notice_generator_sidekiq_job.rb b/app/jobs/important_notice_generator_sidekiq_job.rb new file mode 100644 index 0000000000..5e2709639a --- /dev/null +++ b/app/jobs/important_notice_generator_sidekiq_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ImportantNoticeGeneratorSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :cache + + def perform(patient_ids) + ImportantNoticeGeneratorJob.new.perform(patient_ids) + end +end diff --git a/app/jobs/invalidate_self_consents_job.rb b/app/jobs/invalidate_self_consents_job.rb index 7097e601e9..7f6829fa42 100644 --- a/app/jobs/invalidate_self_consents_job.rb +++ b/app/jobs/invalidate_self_consents_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class InvalidateSelfConsentsJob < ApplicationJobActiveJob - queue_as :consents +class InvalidateSelfConsentsJob < ApplicationJobSidekiq + sidekiq_options queue: :consents def perform programmes = Programme.all diff --git a/app/jobs/notify_delivery_sidekiq_job.rb b/app/jobs/notify_delivery_sidekiq_job.rb new file mode 100644 index 0000000000..8922cc2e0d --- /dev/null +++ b/app/jobs/notify_delivery_sidekiq_job.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require "notifications/client" + +# This is a temporary copy of `NotifyDeliveryJob` that will be removed when +# all jobs have been converted to Sidekiq. +class NotifyDeliverySidekiqJob < ApplicationJobSidekiq + TEAM_ONLY_API_KEY_MESSAGE = + "Can’t send to this recipient using a team-only API key" + + sidekiq_options queue: :notifications + + def fetch_params(params) + { + academic_year: params["academic_year"], + consent: (id = params["consent_id"]) && Consent.find(id), + consent_form: (id = params["consent_form_id"]) && ConsentForm.find(id), + disease_types: params["disease_types"], + parent: (id = params["parent_id"]) && Parent.find(id), + patient: (id = params["patient_id"]) && Patient.find(id), + programme_types: params["programme_types"] || [], + sent_by: (id = params["sent_by_user_id"]) && User.find(id), + session: (id = params["session_id"]) && Session.find(id), + team: (id = params["team_id"]) && Team.find(id), + team_location: (id = params["team_location_id"]) && TeamLocation.find(id), + vaccination_record: + (id = params["vaccination_record_id"]) && VaccinationRecord.find(id) + } + end + + def self.client + @client ||= + Notifications::Client.new( + Settings.govuk_notify["#{Settings.govuk_notify.mode}_key"] + ) + end + + def self.deliveries + @deliveries ||= [] + end + + def self.send_via_notify? = Settings.govuk_notify&.enabled + + def self.send_via_test? = Rails.env.test? + + class UnknownTemplate < StandardError + end +end diff --git a/app/jobs/patient_nhs_number_lookup_sidekiq_job.rb b/app/jobs/patient_nhs_number_lookup_sidekiq_job.rb new file mode 100644 index 0000000000..d1e03b85f0 --- /dev/null +++ b/app/jobs/patient_nhs_number_lookup_sidekiq_job.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class PatientNHSNumberLookupSidekiqJob < ApplicationJobSidekiq + include PDSThrottlingConcern + + sidekiq_options queue: :pds + + def perform(patient_id) + patient = Patient.find(patient_id) + PatientNHSNumberLookupJob.new.perform(patient) + end +end diff --git a/app/jobs/patient_update_from_pds_job.rb b/app/jobs/patient_update_from_pds_job.rb index 56cee6ba81..cfbc2bf32b 100644 --- a/app/jobs/patient_update_from_pds_job.rb +++ b/app/jobs/patient_update_from_pds_job.rb @@ -46,10 +46,20 @@ def perform(patient, search_results = []) end rescue NHS::PDS::PatientNotFound patient.update!(nhs_number: nil) - PDSCascadingSearchJob.perform_later(patient) + PDSCascadingSearchSidekiqJob.perform_async( + patient.to_global_id.to_s, + nil, + nil, + nil + ) rescue NHS::PDS::InvalidatedResource, NHS::PDS::InvalidNHSNumber patient.invalidate! - PDSCascadingSearchJob.perform_later(patient) + PDSCascadingSearchSidekiqJob.perform_async( + patient.to_global_id.to_s, + nil, + nil, + nil + ) end class MissingNHSNumber < StandardError diff --git a/app/jobs/patient_update_from_pds_sidekiq_job.rb b/app/jobs/patient_update_from_pds_sidekiq_job.rb new file mode 100644 index 0000000000..8f4e58db68 --- /dev/null +++ b/app/jobs/patient_update_from_pds_sidekiq_job.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class PatientUpdateFromPDSSidekiqJob < ApplicationJobSidekiq + include PDSThrottlingConcern + + sidekiq_options queue: :pds + + def perform(patient_id, search_results) + patient = Patient.find(patient_id) + search_results ||= [] + PatientUpdateFromPDSJob.new.perform(patient, search_results) + end +end diff --git a/app/jobs/patients_clear_registration_job.rb b/app/jobs/patients_clear_registration_job.rb index 354da6b171..2c5990583e 100644 --- a/app/jobs/patients_clear_registration_job.rb +++ b/app/jobs/patients_clear_registration_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class PatientsClearRegistrationJob < ApplicationJobActiveJob - queue_as :patients +class PatientsClearRegistrationJob < ApplicationJobSidekiq + sidekiq_options queue: :patients def perform academic_year = AcademicYear.pending diff --git a/app/jobs/patients_refused_consent_already_vaccinated_job.rb b/app/jobs/patients_refused_consent_already_vaccinated_job.rb index 538cbb4dd9..6cc2aa7ece 100644 --- a/app/jobs/patients_refused_consent_already_vaccinated_job.rb +++ b/app/jobs/patients_refused_consent_already_vaccinated_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class PatientsRefusedConsentAlreadyVaccinatedJob < ApplicationJobActiveJob - queue_as :patients +class PatientsRefusedConsentAlreadyVaccinatedJob < ApplicationJobSidekiq + sidekiq_options queue: :patients def perform return unless should_perform? diff --git a/app/jobs/pds_cascading_search_job.rb b/app/jobs/pds_cascading_search_job.rb index 68c63e91f4..9819eea9ca 100644 --- a/app/jobs/pds_cascading_search_job.rb +++ b/app/jobs/pds_cascading_search_job.rb @@ -50,9 +50,12 @@ def perform(searchable, step_name: nil, search_results: nil, queue: nil) next_step == "save_nhs_number_if_unique" searchable.save! if searchable.is_a?(PatientChangeset) - ProcessPatientChangesetJob.perform_later(searchable.id) + ProcessPatientChangesetSidekiqJob.perform_async(searchable.id) else - PatientUpdateFromPDSJob.perform_later(searchable, search_results) + PatientUpdateFromPDSSidekiqJob.perform_async( + searchable.id, + search_results + ) end elsif next_step.in?(STEPS.keys) raise "Recursive step detected: #{next_step}" if next_step == step_name @@ -165,11 +168,11 @@ def search_for_patient( def enqueue_next_search(searchable, step_name, search_results, queue) searchable.save! - PDSCascadingSearchJob.set(queue:).perform_later( - searchable, - step_name:, - search_results:, - queue: + PDSCascadingSearchSidekiqJob.set(queue:).perform_async( + searchable.to_global_id.to_s, + step_name, + search_results, + queue ) end diff --git a/app/jobs/pds_cascading_search_sidekiq_job.rb b/app/jobs/pds_cascading_search_sidekiq_job.rb new file mode 100644 index 0000000000..ff9416f45e --- /dev/null +++ b/app/jobs/pds_cascading_search_sidekiq_job.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class PDSCascadingSearchSidekiqJob < ApplicationJobSidekiq + include PDSThrottlingConcern + + sidekiq_options queue: :pds + + def perform(searchable_global_id, step_name, search_results, queue) + searchable = GlobalID::Locator.locate(searchable_global_id) + PDSCascadingSearchJob.new.perform( + searchable, + step_name:, + search_results:, + queue: + ) + end +end diff --git a/app/jobs/process_consent_form_sidekiq_job.rb b/app/jobs/process_consent_form_sidekiq_job.rb new file mode 100644 index 0000000000..ba8b67216b --- /dev/null +++ b/app/jobs/process_consent_form_sidekiq_job.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class ProcessConsentFormSidekiqJob < ApplicationJobSidekiq + include PDSThrottlingConcern + + sidekiq_options queue: :consents + + # We may enqueue this job more than once for the same ConsentForm during the parent + # consent journey (e.g. once when the consent is recorded, and again after the optional + # ethnicity flow is completed). Sidekiq does not guarantee ordering, so those jobs + # could otherwise run concurrently and race each other while reading/updating the + # same ConsentForm/Patient (and while calling out to PDS). + # + # Throttling concurrency to 1 per consent_form ID ensures only one job for a given + # ConsentForm is processed at a time. This makes the job safe to re-run to pick up + # newly submitted data like ethnicity. + sidekiq_throttle( + concurrency: { + limit: 1, + key_suffix: ->(consent_form_id) { consent_form_id } + } + ) + + def perform(consent_form_id) + ProcessConsentFormJob.new.perform(consent_form_id) + end +end diff --git a/app/jobs/process_import_sidekiq_job.rb b/app/jobs/process_import_sidekiq_job.rb new file mode 100644 index 0000000000..530abd27c9 --- /dev/null +++ b/app/jobs/process_import_sidekiq_job.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class ProcessImportSidekiqJob < ApplicationJobSidekiq + include SingleConcurrencyConcern + + sidekiq_options queue: :imports + + def perform(import_global_id) + import = GlobalID::Locator.locate(import_global_id) + ProcessImportJob.new.perform(import) + end +end diff --git a/app/jobs/process_patient_changeset_job.rb b/app/jobs/process_patient_changeset_job.rb index 6d61b0e0ec..31a5afe446 100644 --- a/app/jobs/process_patient_changeset_job.rb +++ b/app/jobs/process_patient_changeset_job.rb @@ -28,7 +28,7 @@ def perform(patient_changeset_id) return if import.changesets_are_invalid? end - ReviewPatientChangesetJob.perform_later(patient_changeset.id) + ReviewPatientChangesetSidekiqJob.perform_async(patient_changeset.id) end private diff --git a/app/jobs/process_patient_changeset_sidekiq_job.rb b/app/jobs/process_patient_changeset_sidekiq_job.rb new file mode 100644 index 0000000000..577ce7c5b8 --- /dev/null +++ b/app/jobs/process_patient_changeset_sidekiq_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ProcessPatientChangesetSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :imports + + def perform(patient_changeset_id) + ProcessPatientChangesetJob.new.perform(patient_changeset_id) + end +end diff --git a/app/jobs/remove_import_csv_job.rb b/app/jobs/remove_import_csv_job.rb index 9f30f74f71..55d8e8ef93 100644 --- a/app/jobs/remove_import_csv_job.rb +++ b/app/jobs/remove_import_csv_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class RemoveImportCSVJob < ApplicationJobActiveJob - queue_as :imports +class RemoveImportCSVJob < ApplicationJobSidekiq + sidekiq_options queue: :imports def perform [ClassImport, CohortImport, ImmunisationImport].each do |import_type| diff --git a/app/jobs/reporting_api/refresh_job.rb b/app/jobs/reporting_api/refresh_job.rb index 30ddc0823d..a3d61d0bf3 100644 --- a/app/jobs/reporting_api/refresh_job.rb +++ b/app/jobs/reporting_api/refresh_job.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -class ReportingAPI::RefreshJob < ApplicationJobActiveJob - def perform - ReportingAPI::Total.refresh! - end +class ReportingAPI::RefreshJob < ApplicationJobSidekiq + def perform = ReportingAPI::Total.refresh! end diff --git a/app/jobs/review_class_import_school_move_sidekiq_job.rb b/app/jobs/review_class_import_school_move_sidekiq_job.rb new file mode 100644 index 0000000000..6e42e83d21 --- /dev/null +++ b/app/jobs/review_class_import_school_move_sidekiq_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ReviewClassImportSchoolMoveSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :imports + + def perform(import_id) + ReviewClassImportSchoolMoveJob.new.perform(import_id) + end +end diff --git a/app/jobs/review_patient_changeset_job.rb b/app/jobs/review_patient_changeset_job.rb index 05dff8dfb9..b8ca0407f4 100644 --- a/app/jobs/review_patient_changeset_job.rb +++ b/app/jobs/review_patient_changeset_job.rb @@ -19,7 +19,7 @@ def perform(patient_changeset_id) if all_jobs_finished_and_import_valid(import) if import.is_a?(ClassImport) - ReviewClassImportSchoolMoveJob.perform_later(import.id) + ReviewClassImportSchoolMoveSidekiqJob.perform_async(import.id) elsif import.calculating_re_review? import.in_re_review! else diff --git a/app/jobs/review_patient_changeset_sidekiq_job.rb b/app/jobs/review_patient_changeset_sidekiq_job.rb new file mode 100644 index 0000000000..87af4bb4f0 --- /dev/null +++ b/app/jobs/review_patient_changeset_sidekiq_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ReviewPatientChangesetSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :imports + + def perform(patient_changeset_id) + ReviewPatientChangesetJob.new.perform(patient_changeset_id) + end +end diff --git a/app/jobs/send_automatic_school_consent_reminders_sidekiq_job.rb b/app/jobs/send_automatic_school_consent_reminders_sidekiq_job.rb new file mode 100644 index 0000000000..598e814424 --- /dev/null +++ b/app/jobs/send_automatic_school_consent_reminders_sidekiq_job.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class SendAutomaticSchoolConsentRemindersSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :notifications + + def perform(session_id) + session = Session.find(session_id) + SendAutomaticSchoolConsentRemindersJob.new.perform(session) + end +end diff --git a/app/jobs/send_manual_school_consent_reminders_job.rb b/app/jobs/send_manual_school_consent_reminders_job.rb index d5374f47db..e2118bf0f4 100644 --- a/app/jobs/send_manual_school_consent_reminders_job.rb +++ b/app/jobs/send_manual_school_consent_reminders_job.rb @@ -1,19 +1,18 @@ # frozen_string_literal: true -class SendManualSchoolConsentRemindersJob < ApplicationJobActiveJob +class SendManualSchoolConsentRemindersJob < ApplicationJobSidekiq include SendSchoolConsentNotificationConcern - queue_as :notifications + sidekiq_options queue: :notifications + + def perform(session_id, sent_by_user_id) + session = Session.find(session_id) + sent_by = User.find(sent_by_user_id) - def perform(session, current_user:) patient_programmes_eligible_for_notification( session: ) do |patient, programmes| - patient.notifier.send_consent_reminder( - programmes, - session:, - sent_by: current_user - ) + patient.notifier.send_consent_reminder(programmes, session:, sent_by:) end end end diff --git a/app/jobs/send_school_consent_requests_sidekiq_job.rb b/app/jobs/send_school_consent_requests_sidekiq_job.rb new file mode 100644 index 0000000000..d4663cc581 --- /dev/null +++ b/app/jobs/send_school_consent_requests_sidekiq_job.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class SendSchoolConsentRequestsSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :notifications + + def perform(session_id) + session = Session.find(session_id) + SendSchoolConsentRequestsJob.new.perform(session) + end +end diff --git a/app/jobs/send_school_session_reminders_sidekiq_job.rb b/app/jobs/send_school_session_reminders_sidekiq_job.rb new file mode 100644 index 0000000000..c2144fd3d8 --- /dev/null +++ b/app/jobs/send_school_session_reminders_sidekiq_job.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class SendSchoolSessionRemindersSidekiqJob < ApplicationJobSidekiq + sidekiq_options queue: :notifications + + def perform(session_id) + session = Session.find(session_id) + SendSchoolSessionRemindersJob.new.perform(session) + end +end diff --git a/app/jobs/send_vaccination_confirmations_job.rb b/app/jobs/send_vaccination_confirmations_job.rb index 3e71b0302f..8c2076e32d 100644 --- a/app/jobs/send_vaccination_confirmations_job.rb +++ b/app/jobs/send_vaccination_confirmations_job.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class SendVaccinationConfirmationsJob < ApplicationJobActiveJob - queue_as :notifications +class SendVaccinationConfirmationsJob < ApplicationJobSidekiq + sidekiq_options queue: :notifications def perform # Find the oldest record that has had a confirmation sent, and send confirmations for all subsequent records diff --git a/app/jobs/sms_delivery_sidekiq_job.rb b/app/jobs/sms_delivery_sidekiq_job.rb new file mode 100644 index 0000000000..056cd45dac --- /dev/null +++ b/app/jobs/sms_delivery_sidekiq_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class SMSDeliverySidekiqJob < NotifyDeliverySidekiqJob + include GovukNotifyThrottlingConcern + + def perform(template_name, params) + SMSDeliveryJob.new.perform(template_name, **fetch_params(params)) + end +end diff --git a/app/jobs/trim_active_record_sessions_job.rb b/app/jobs/trim_active_record_sessions_job.rb index 4b6316fd9a..6f3939a4fb 100644 --- a/app/jobs/trim_active_record_sessions_job.rb +++ b/app/jobs/trim_active_record_sessions_job.rb @@ -2,7 +2,7 @@ # Based off https://github.com/rails/activerecord-session_store/blob/9198a952916df925f36ac2beab247296ee5c0341/lib/tasks/database.rake#L14-L20 -class TrimActiveRecordSessionsJob < ApplicationJobActiveJob +class TrimActiveRecordSessionsJob < ApplicationJobSidekiq def perform ActiveRecord::SessionStore::Session.where( "updated_at < ?", diff --git a/app/lib/already_had_notification_sender.rb b/app/lib/already_had_notification_sender.rb index e6e91f3655..b7d1ea168f 100644 --- a/app/lib/already_had_notification_sender.rb +++ b/app/lib/already_had_notification_sender.rb @@ -29,21 +29,17 @@ def call ).parents_with_consent parents_with_consent.each do |parent, consent| + params = { + "parent_id" => parent.id, + "vaccination_record_id" => vaccination_record.id, + "consent_id" => consent.id + } + if parent.phone_receive_updates - SMSDeliveryJob.perform_later( - :vaccination_already_had, - parent:, - vaccination_record:, - consent: - ) + SMSDeliverySidekiqJob.perform_async("vaccination_already_had", params) end - EmailDeliveryJob.perform_later( - :vaccination_already_had, - parent:, - vaccination_record:, - consent: - ) + EmailDeliverySidekiqJob.perform_async("vaccination_already_had", params) consent.update!( patient_already_vaccinated_notification_sent_at: Time.current diff --git a/app/lib/notifier/consent.rb b/app/lib/notifier/consent.rb index f8405f9027..e4aa2407a2 100644 --- a/app/lib/notifier/consent.rb +++ b/app/lib/notifier/consent.rb @@ -10,10 +10,14 @@ def initialize(consent) def send_confirmation(session:, triage:, sent_by:) return unless send_notification? - params = { consent:, session:, sent_by: } + params = { + "consent_id" => consent.id, + "session_id" => session.id, + "sent_by_user_id" => sent_by.id + } if triage - send_triage_email(triage, params) + send_triage_email(triage, session, params) elsif consent.requires_triage? send_consent_email(:triage, params) elsif consent.response_refused? @@ -34,38 +38,38 @@ def send_notification? !consent.via_self_consent? end - def send_triage_email(triage, params) - template = triage_email_template(triage, params[:session]) - EmailDeliveryJob.perform_later(template, **params) + def send_triage_email(triage, session, params) + template_name = triage_email_template(triage, session) + EmailDeliverySidekiqJob.perform_async(template_name, params) end def triage_email_template(triage, session) if triage.safe_to_vaccinate? if programme.mmr? && patient_on_last_dose?(session) - :triage_vaccination_will_happen_mmr_second_dose + "triage_vaccination_will_happen_mmr_second_dose" else - :triage_vaccination_will_happen + "triage_vaccination_will_happen" end elsif triage.do_not_vaccinate? - :triage_vaccination_wont_happen + "triage_vaccination_wont_happen" elsif triage.delay_vaccination? - :triage_delay_vaccination + "triage_delay_vaccination" elsif triage.invite_to_clinic? - resolve_email_template(:triage_vaccination_at_clinic, triage.team) + resolve_email_template("triage_vaccination_at_clinic", triage.team) elsif triage.keep_in_triage? - :consent_confirmation_triage + "consent_confirmation_triage" end end def send_consent_email(type, params) - template = :"consent_confirmation_#{type}" - EmailDeliveryJob.perform_later(template, **params) + template_name = "consent_confirmation_#{type}" + EmailDeliverySidekiqJob.perform_async(template_name, params) end def send_consent_sms(type, consent, params) if consent.parent.phone_receive_updates - template = :"consent_confirmation_#{type}" - SMSDeliveryJob.perform_later(template, **params) + template_name = "consent_confirmation_#{type}" + SMSDeliverySidekiqJob.perform_async(template_name, params) end end @@ -76,7 +80,7 @@ def send_consent_email_and_sms(type, consent, params) def resolve_email_template(template_name, team) ods_code = team.organisation.ods_code.downcase - template_names = [:"#{template_name}_#{ods_code}", template_name] + template_names = ["#{template_name}_#{ods_code}", template_name] template_names.find { NotifyTemplate.exists?(it, channel: :email) } end diff --git a/app/lib/notifier/consent_form.rb b/app/lib/notifier/consent_form.rb index 3d9d7e1bbf..2b2712b7a5 100644 --- a/app/lib/notifier/consent_form.rb +++ b/app/lib/notifier/consent_form.rb @@ -31,22 +31,24 @@ def send_confirmation def send_unknown_contact_details_warning(patient:) patient.parents.each do |parent| + params = { + "consent_form_id" => consent_form.id, + "parent_id" => parent.id, + "patient_id" => patient.id + } + if parent.email.present? - EmailDeliveryJob.perform_later( - :consent_unknown_contact_details_warning, - consent_form:, - parent:, - patient: + EmailDeliverySidekiqJob.perform_async( + "consent_unknown_contact_details_warning", + params ) end next unless parent.phone.present? && parent.phone_receive_updates - SMSDeliveryJob.perform_later( - :consent_unknown_contact_details_warning, - consent_form:, - parent:, - patient: + SMSDeliverySidekiqJob.perform_async( + "consent_unknown_contact_details_warning", + params ) end end @@ -56,32 +58,50 @@ def send_unknown_contact_details_warning(patient:) attr_reader :consent_form def send_confirmation_given(programme_types: nil, disease_types: nil) - params = { consent_form: } - params[:programme_types] = programme_types if programme_types - params[:disease_types] = disease_types if disease_types + params = { "consent_form_id" => consent_form.id } + params["programme_types"] = programme_types if programme_types + params["disease_types"] = disease_types if disease_types if consent_form.health_answers_require_triage? - EmailDeliveryJob.perform_later(:consent_confirmation_triage, **params) + EmailDeliverySidekiqJob.perform_async( + "consent_confirmation_triage", + params + ) elsif consent_form.session&.clinic? || consent_form.session&.completed? - EmailDeliveryJob.perform_later(:consent_confirmation_clinic, **params) + EmailDeliverySidekiqJob.perform_async( + "consent_confirmation_clinic", + params + ) else - EmailDeliveryJob.perform_later(:consent_confirmation_given, **params) + EmailDeliverySidekiqJob.perform_async( + "consent_confirmation_given", + params + ) if consent_form.parent_phone_receive_updates - SMSDeliveryJob.perform_later(:consent_confirmation_given, **params) + SMSDeliverySidekiqJob.perform_async( + "consent_confirmation_given", + params + ) end end end def send_confirmation_refused(programme_types: nil, disease_types: nil) - params = { consent_form: } - params[:programme_types] = programme_types if programme_types - params[:disease_types] = disease_types if disease_types + params = { "consent_form_id" => consent_form.id } + params["programme_types"] = programme_types if programme_types + params["disease_types"] = disease_types if disease_types - EmailDeliveryJob.perform_later(:consent_confirmation_refused, **params) + EmailDeliverySidekiqJob.perform_async( + "consent_confirmation_refused", + params + ) if consent_form.parent_phone_receive_updates - SMSDeliveryJob.perform_later(:consent_confirmation_refused, **params) + SMSDeliverySidekiqJob.perform_async( + "consent_confirmation_refused", + params + ) end end end diff --git a/app/lib/notifier/patient.rb b/app/lib/notifier/patient.rb index 0da844685f..82e0d81465 100644 --- a/app/lib/notifier/patient.rb +++ b/app/lib/notifier/patient.rb @@ -148,11 +148,18 @@ def send_clinic_invitation( template_name = find_clinic_template_name(type, team:) - params = { academic_year:, patient:, programme_types:, sent_by:, team: } + base_params = { + "academic_year" => academic_year, + "patient_id" => patient.id, + "programme_types" => programme_types, + "sent_by_user_id" => sent_by.id, + "team_id" => team.id + } parents.each do |parent| - EmailDeliveryJob.perform_later(template_name, parent:, **params) - SMSDeliveryJob.perform_later(template_name, parent:, **params) + params = base_params.merge("parent_id" => parent.id) + EmailDeliverySidekiqJob.perform_async(template_name, params) + SMSDeliverySidekiqJob.perform_async(template_name, params) end clinic_notification @@ -226,16 +233,22 @@ def send_consent_notification( disease_types = programmes_to_send_for.flat_map(&:disease_types).presence parents.each do |parent| - params = { disease_types:, parent:, patient:, programme_types:, sent_by: } + params = { + "disease_types" => disease_types, + "parent_id" => parent.id, + "patient_id" => patient.id, + "programme_types" => programme_types, + "sent_by_user_id" => sent_by&.id + } if session - params[:session] = session + params["session_id"] = session.id else - params[:team_location] = team_location + params["team_location_id"] = team_location.id end - EmailDeliveryJob.perform_later(email_template, **params) - SMSDeliveryJob.perform_later(sms_template, **params) + EmailDeliverySidekiqJob.perform_async(email_template, params) + SMSDeliverySidekiqJob.perform_async(sms_template, params) end PatientStatusUpdaterJob.perform_async(patient.id) @@ -254,9 +267,9 @@ def generate_consent_templates( base_template = if is_school && CONSENT_REMINDER_TYPES.include?(type) - :consent_school_reminder + "consent_school_reminder" else - :"consent_#{is_school ? "school" : "clinic"}_#{type}" + "consent_#{is_school ? "school" : "clinic"}_#{type}" end # We can only handle a single programme group or variant in the template. @@ -300,7 +313,7 @@ def generate_consent_templates( ) template || base_template elsif is_school - :consent_school_reminder + "consent_school_reminder" end [email_template, sms_template] @@ -321,7 +334,7 @@ def resolve_consent_template( combinations .lazy - .map { |parts| :"#{base_template}_#{parts.join("_")}" } + .map { |parts| "#{base_template}_#{parts.join("_")}" } .detect { NotifyTemplate.exists?(it, channel:) } end @@ -351,10 +364,8 @@ def programmes_to_send_clinic_invitation_for( end def find_clinic_template_name(type, team:) - template_names = [ - :"clinic_#{type}_#{team.organisation.ods_code.downcase}", - :"clinic_#{type}" - ] + ods_code = team.organisation.ods_code.downcase + template_names = ["clinic_#{type}_#{ods_code}", "clinic_#{type}"] template_names.find { NotifyTemplate.exists?(it, channel: :email) } end diff --git a/app/lib/notifier/vaccination_record.rb b/app/lib/notifier/vaccination_record.rb index 5350ec9740..14b976163b 100644 --- a/app/lib/notifier/vaccination_record.rb +++ b/app/lib/notifier/vaccination_record.rb @@ -10,18 +10,22 @@ def send_confirmation(sent_by:) template_name = if vaccination_record.administered? - :vaccination_administered + "vaccination_administered" else - :vaccination_not_administered + "vaccination_not_administered" end parents.each do |parent| - params = { parent:, vaccination_record:, sent_by: } + params = { + "parent_id" => parent.id, + "vaccination_record_id" => vaccination_record.id, + "sent_by_user_id" => sent_by&.id + } - EmailDeliveryJob.perform_later(template_name, **params) + EmailDeliverySidekiqJob.perform_async(template_name, params) if parent.phone_receive_updates - SMSDeliveryJob.perform_later(template_name, **params) + SMSDeliverySidekiqJob.perform_async(template_name, params) end end end @@ -30,12 +34,13 @@ def send_deletion(sent_by:) return if parents.empty? parents.each do |parent| - EmailDeliveryJob.perform_later( - :vaccination_deleted, - parent:, - vaccination_record:, - sent_by: - ) + params = { + "parent_id" => parent.id, + "vaccination_record_id" => vaccination_record.id, + "sent_by_user_id" => sent_by&.id + } + + EmailDeliverySidekiqJob.perform_async("vaccination_deleted", params) end end diff --git a/app/lib/notify_template.rb b/app/lib/notify_template.rb index b33a65e52c..ee60ef9910 100644 --- a/app/lib/notify_template.rb +++ b/app/lib/notify_template.rb @@ -18,7 +18,7 @@ def find(name, channel:) def find_by_id(template_id, channel:) return nil if template_id.blank? - local_templates(channel).values.find { _1.id == template_id.to_s } + local_templates(channel).values.find { it.id == template_id.to_s } end def exists?(name, channel:) diff --git a/app/lib/team_merger.rb b/app/lib/team_merger.rb index cd930833b2..c88ba090d9 100644 --- a/app/lib/team_merger.rb +++ b/app/lib/team_merger.rb @@ -394,9 +394,7 @@ def migrate_generic_locations(merged_team) end def refresh_materialized_views - ReportingAPI::RefreshJob.perform_later - rescue StandardError => e - Rails.logger.warn "TeamMerge: could not refresh materialized views: #{e.message}" + ReportingAPI::RefreshJob.perform_async end def append_migration_counts diff --git a/app/lib/update_patients_from_pds.rb b/app/lib/update_patients_from_pds.rb index 9312c67568..10b208936a 100644 --- a/app/lib/update_patients_from_pds.rb +++ b/app/lib/update_patients_from_pds.rb @@ -11,9 +11,17 @@ def call patients.find_each do |patient| if patient.nhs_number.nil? - PDSCascadingSearchJob.set(queue:).perform_later(patient) + PDSCascadingSearchSidekiqJob.set(queue:).perform_async( + patient.to_global_id.to_s, + nil, + nil, + nil + ) else - PatientUpdateFromPDSJob.set(queue:).perform_later(patient) + PatientUpdateFromPDSSidekiqJob.set(queue:).perform_async( + patient.id, + nil + ) end end end diff --git a/app/models/class_import.rb b/app/models/class_import.rb index 45292375d8..72f89b63ad 100644 --- a/app/models/class_import.rb +++ b/app/models/class_import.rb @@ -79,7 +79,7 @@ def postprocess_rows! missed_patients = unknown_patients - valid_patients if missed_patients.any? && (processed? || partially_processed?) update_columns(status: :calculating_re_review, processed_at: nil) - ReviewClassImportSchoolMoveJob.perform_later(id) + ReviewClassImportSchoolMoveSidekiqJob.perform_async(id) end unknown_patients = valid_patients diff --git a/app/models/patient.rb b/app/models/patient.rb index 735cba29ec..755328f2f5 100644 --- a/app/models/patient.rb +++ b/app/models/patient.rb @@ -872,7 +872,7 @@ def should_generate_important_notice? def generate_important_notice_if_needed if should_generate_important_notice? - ImportantNoticeGeneratorJob.perform_later([id]) + ImportantNoticeGeneratorSidekiqJob.perform_async([id]) end end end diff --git a/app/models/patient_import.rb b/app/models/patient_import.rb index 603a723f4a..ad85120523 100644 --- a/app/models/patient_import.rb +++ b/app/models/patient_import.rb @@ -156,7 +156,7 @@ def process_no_postcode_changesets(changesets) created_at: Time.current } cs.calculating_review! - ReviewPatientChangesetJob.perform_later(cs.id) + ReviewPatientChangesetSidekiqJob.perform_async(cs.id) end end @@ -170,15 +170,17 @@ def enqueue_review_jobs(changesets) review_changesets.each do |cs| cs.calculating_review! - ReviewPatientChangesetJob.perform_later(cs.id) + ReviewPatientChangesetSidekiqJob.perform_async(cs.id) end end def enqueue_pds_cascading_searches(changesets) - changesets.find_each do |cs| - PDSCascadingSearchJob.set(queue: :imports).perform_later( - cs, - queue: "imports" + changesets.find_each do |changeset| + PDSCascadingSearchSidekiqJob.set(queue: :imports).perform_async( + changeset.to_global_id.to_s, + nil, + nil, + "imports" ) end end diff --git a/app/models/school_move.rb b/app/models/school_move.rb index d280dd221e..3d0e51fa3a 100644 --- a/app/models/school_move.rb +++ b/app/models/school_move.rb @@ -190,6 +190,6 @@ def update_patient_statuses! end def update_important_notices! - ImportantNoticeGeneratorJob.perform_later([patient.id]) + ImportantNoticeGeneratorSidekiqJob.perform_async([patient.id]) end end diff --git a/app/models/session_notification.rb b/app/models/session_notification.rb index 01534d7f6f..ea4c63edeb 100644 --- a/app/models/session_notification.rb +++ b/app/models/session_notification.rb @@ -89,20 +89,20 @@ def self.create_and_send!( parents.each do |parent| params = { - parent:, - patient:, - programme_types: programmes.map(&:type), - session:, - sent_by: current_user + "parent_id" => parent.id, + "patient_id" => patient.id, + "programme_types" => programmes.map(&:type), + "session_id" => session.id, + "sent_by_user_id" => current_user&.id } - template_name = :"session_#{type}" + template_name = "session_#{type}" - EmailDeliveryJob.perform_later(template_name, **params) + EmailDeliverySidekiqJob.perform_async(template_name, params) next unless parent.phone_receive_updates - SMSDeliveryJob.perform_later(template_name, **params) + SMSDeliverySidekiqJob.perform_async(template_name, params) end end end diff --git a/app/models/vaccination_record.rb b/app/models/vaccination_record.rb index ca0f90f1ea..de8fe9097a 100644 --- a/app/models/vaccination_record.rb +++ b/app/models/vaccination_record.rb @@ -385,7 +385,7 @@ def should_generate_important_notice? def generate_important_notice_if_needed if should_generate_important_notice? - ImportantNoticeGeneratorJob.perform_later([patient_id]) + ImportantNoticeGeneratorSidekiqJob.perform_async([patient_id]) end end end diff --git a/spec/factories/patients.rb b/spec/factories/patients.rb index e9e1908dc8..47184ed40a 100644 --- a/spec/factories/patients.rb +++ b/spec/factories/patients.rb @@ -176,21 +176,21 @@ date_of_death { Date.current } date_of_death_recorded_at { Time.current } after(:create) do |instance| - ImportantNoticeGeneratorJob.perform_now([instance.id]) + ImportantNoticeGeneratorJob.new.perform([instance.id]) end end trait :invalidated do invalidated_at { Time.current } after(:create) do |instance| - ImportantNoticeGeneratorJob.perform_now([instance.id]) + ImportantNoticeGeneratorJob.new.perform([instance.id]) end end trait :restricted do restricted_at { Time.current } after(:create) do |instance| - ImportantNoticeGeneratorJob.perform_now([instance.id]) + ImportantNoticeGeneratorJob.new.perform([instance.id]) end end diff --git a/spec/factories/vaccination_records.rb b/spec/factories/vaccination_records.rb index 378d305a38..fdb0606815 100644 --- a/spec/factories/vaccination_records.rb +++ b/spec/factories/vaccination_records.rb @@ -139,7 +139,7 @@ patient_scope: Patient.where(id: vaccination_record.patient_id) ) - ImportantNoticeGeneratorJob.perform_now([vaccination_record.patient_id]) + ImportantNoticeGeneratorJob.new.perform([vaccination_record.patient_id]) end trait :sourced_from_nhs_immunisations_api do diff --git a/spec/features/bulk_remove_parents_spec.rb b/spec/features/bulk_remove_parents_spec.rb index eea0b4e7e1..7dff07a7a0 100644 --- a/spec/features/bulk_remove_parents_spec.rb +++ b/spec/features/bulk_remove_parents_spec.rb @@ -187,7 +187,9 @@ def then_i_should_see_the_parent_relationships_without_associated_consents_are_b end def and_the_bulk_remove_job_should_be_enqueued_with_remove_option(option) - expect(BulkRemoveParentRelationshipsJob).to have_been_enqueued.with( + expect( + BulkRemoveParentRelationshipsSidekiqJob + ).to have_enqueued_sidekiq_job.with( @class_import.to_global_id.to_s, @class_import.parent_relationship_ids, @user.id, @@ -196,7 +198,7 @@ def and_the_bulk_remove_job_should_be_enqueued_with_remove_option(option) end def when_the_bulk_remove_job_is_processed - perform_enqueued_jobs + Sidekiq::Job.drain_all end def then_all_parent_relationships_from_the_import_should_be_deleted diff --git a/spec/features/doubles_vaccination_administered_spec.rb b/spec/features/doubles_vaccination_administered_spec.rb index 9b6574a2a2..30b77bdc69 100644 --- a/spec/features/doubles_vaccination_administered_spec.rb +++ b/spec/features/doubles_vaccination_administered_spec.rb @@ -128,7 +128,7 @@ def then_i_see_the_patient_is_vaccinated_for_td_ipv end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccinations diff --git a/spec/features/flu_vaccination_administered_spec.rb b/spec/features/flu_vaccination_administered_spec.rb index 491347af0f..bb5b708531 100644 --- a/spec/features/flu_vaccination_administered_spec.rb +++ b/spec/features/flu_vaccination_administered_spec.rb @@ -339,7 +339,7 @@ def and_the_vaccination_record_is_synced_to_nhs end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination diff --git a/spec/features/hpv_vaccination_administered_spec.rb b/spec/features/hpv_vaccination_administered_spec.rb index 0a34cbe960..40530f32b7 100644 --- a/spec/features/hpv_vaccination_administered_spec.rb +++ b/spec/features/hpv_vaccination_administered_spec.rb @@ -297,7 +297,7 @@ def then_i_see_the_patient end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination diff --git a/spec/features/hpv_vaccination_clinic_spec.rb b/spec/features/hpv_vaccination_clinic_spec.rb index 372c413ff8..03e88cf248 100644 --- a/spec/features/hpv_vaccination_clinic_spec.rb +++ b/spec/features/hpv_vaccination_clinic_spec.rb @@ -120,7 +120,7 @@ def then_i_see_that_the_status_is_vaccinated end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination diff --git a/spec/features/hpv_vaccination_delayed_spec.rb b/spec/features/hpv_vaccination_delayed_spec.rb index 0c35930819..2ea57439d1 100644 --- a/spec/features/hpv_vaccination_delayed_spec.rb +++ b/spec/features/hpv_vaccination_delayed_spec.rb @@ -109,7 +109,7 @@ def then_i_see_the_patient_has_no_outcome_yet end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_delay diff --git a/spec/features/import_child_pds_lookup_extravaganza_spec.rb b/spec/features/import_child_pds_lookup_extravaganza_spec.rb index 1083c22ef8..4f69f57d27 100644 --- a/spec/features/import_child_pds_lookup_extravaganza_spec.rb +++ b/spec/features/import_child_pds_lookup_extravaganza_spec.rb @@ -599,7 +599,7 @@ def then_i_should_see_the_import_page end def and_i_should_see_one_new_patient_created - perform_enqueued_jobs + Sidekiq::Job.drain_all expect(Patient.count).to eq(7) end @@ -724,10 +724,8 @@ def and_emma_has_correct_parent_data end def then_school_moves_are_created_appropriately - perform_enqueued_jobs - perform_enqueued_jobs - - Sidekiq::Job.drain_all # PatientsAgedOutOfSchoolJob is Sidekiq-only + Sidekiq::Job.drain_all + Sidekiq::Job.drain_all charlie = Patient.find_by(given_name: "Charlie") charlie_move = SchoolMoveLogEntry.find_by(patient: charlie) diff --git a/spec/features/import_child_records_spec.rb b/spec/features/import_child_records_spec.rb index 46d7ea2fb3..d06d852455 100644 --- a/spec/features/import_child_records_spec.rb +++ b/spec/features/import_child_records_spec.rb @@ -259,9 +259,9 @@ def then_i_should_see_the_imports_page_with_the_processing_flash end def when_i_wait_for_the_background_job_to_complete - perform_enqueued_jobs(only: ProcessImportJob) - perform_enqueued_jobs(only: PDSCascadingSearchJob) - perform_enqueued_jobs(only: ProcessPatientChangesetJob) + ProcessImportSidekiqJob.drain + PDSCascadingSearchSidekiqJob.drain + ProcessPatientChangesetSidekiqJob.drain end def then_i_should_see_the_holding_page diff --git a/spec/features/import_class_list_review_spec.rb b/spec/features/import_class_list_review_spec.rb index c82629b15a..000f8742c2 100644 --- a/spec/features/import_class_list_review_spec.rb +++ b/spec/features/import_class_list_review_spec.rb @@ -446,7 +446,7 @@ def and_i_can_see_the_import_is_cancelled def when_i_cancel_the_import click_on "Cancel and delete upload" click_on_most_recent_import(ClassImport) - perform_enqueued_jobs_while_exists(only: ReviewClassImportSchoolMoveJob) + perform_enqueued_jobs_while_exists(ReviewClassImportSchoolMoveSidekiqJob) end def and_i_approve_the_import @@ -456,7 +456,7 @@ def and_i_approve_the_import click_on("Approve and import changed records") end wait_for_import_to_commit(ClassImport) - perform_enqueued_jobs_while_exists(only: ReviewClassImportSchoolMoveJob) + perform_enqueued_jobs_while_exists(ReviewClassImportSchoolMoveSidekiqJob) end alias_method :when_i_approve_the_import, :and_i_approve_the_import @@ -538,7 +538,7 @@ def when_i_go_back_to_re_review def and_i_ignore_changes click_on "Ignore changes" - perform_enqueued_jobs_while_exists(only: ReviewClassImportSchoolMoveJob) + perform_enqueued_jobs_while_exists(ReviewClassImportSchoolMoveSidekiqJob) end def then_the_re_review_patients_are_not_imported diff --git a/spec/features/import_class_lists_spec.rb b/spec/features/import_class_lists_spec.rb index 50668d5cf5..69d9cefd8a 100644 --- a/spec/features/import_class_lists_spec.rb +++ b/spec/features/import_class_lists_spec.rb @@ -239,7 +239,7 @@ def then_i_should_see_the_imports_page_with_the_processing_flash end def when_i_wait_for_the_background_job_to_complete - perform_enqueued_jobs + Sidekiq::Job.drain_all end def then_i_should_see_the_holding_page diff --git a/spec/features/important_notices_spec.rb b/spec/features/important_notices_spec.rb index 044706c5c4..7494331704 100644 --- a/spec/features/important_notices_spec.rb +++ b/spec/features/important_notices_spec.rb @@ -188,7 +188,7 @@ def when_i_stub_pds_to_return_restricted_patient end def when_pds_updates_the_patient_record - PatientUpdateFromPDSJob.perform_now(@patient) + PatientUpdateFromPDSJob.new.perform(@patient, []) end def when_pds_updates_patient_as_not_restricted @@ -258,7 +258,7 @@ def and_i_confirm_the_school_move_back_to_team_one end def and_the_important_notice_job_is_performed - perform_enqueued_jobs(only: ImportantNoticeGeneratorJob) + ImportantNoticeGeneratorSidekiqJob.drain end def when_i_visit_the_notices_page_as_superuser diff --git a/spec/features/manage_children_spec.rb b/spec/features/manage_children_spec.rb index 7cb2ec59fe..f722f6b6e2 100644 --- a/spec/features/manage_children_spec.rb +++ b/spec/features/manage_children_spec.rb @@ -571,7 +571,7 @@ def and_i_see_the_child_is_home_schooled def and_the_important_notice_is_dismissed notice = @patient.important_notices.find_by(type: :invalidated) - perform_enqueued_jobs_while_exists(only: ImportantNoticeGeneratorJob) + perform_enqueued_jobs_while_exists(ImportantNoticeGeneratorSidekiqJob) expect(notice.reload.dismissed_at).to be_present end diff --git a/spec/features/menacwy_vaccination_administered_spec.rb b/spec/features/menacwy_vaccination_administered_spec.rb index e8006462a2..36fe4531a9 100644 --- a/spec/features/menacwy_vaccination_administered_spec.rb +++ b/spec/features/menacwy_vaccination_administered_spec.rb @@ -42,7 +42,7 @@ when_i_confirm_the_details then_i_see_a_success_message and_i_no_longer_see_the_patient_in_the_record_tab - and_the_vaccination_record_is_not_synced_to_nhs + and_the_vaccination_record_is_synced_to_nhs when_i_go_back and_i_save_changes @@ -209,7 +209,7 @@ def and_i_see_the_vaccination_details end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination @@ -242,8 +242,8 @@ def and_a_text_is_sent_to_the_parent_confirming_the_vaccination ) end - def and_the_vaccination_record_is_not_synced_to_nhs - perform_enqueued_jobs - expect(@stubbed_post_request).not_to have_been_requested + def and_the_vaccination_record_is_synced_to_nhs + Sidekiq::Job.drain_all + expect(@stubbed_post_request).to have_been_requested end end diff --git a/spec/features/mmr_vaccination_administered_spec.rb b/spec/features/mmr_vaccination_administered_spec.rb index c420adc51c..400ffb443d 100644 --- a/spec/features/mmr_vaccination_administered_spec.rb +++ b/spec/features/mmr_vaccination_administered_spec.rb @@ -297,7 +297,7 @@ def then_i_see_the_right_programme_on_the_entries end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination diff --git a/spec/features/mmrv_vaccination_administered_spec.rb b/spec/features/mmrv_vaccination_administered_spec.rb index a9b601dfd1..acbe0bdce7 100644 --- a/spec/features/mmrv_vaccination_administered_spec.rb +++ b/spec/features/mmrv_vaccination_administered_spec.rb @@ -260,7 +260,7 @@ def then_i_see_the_right_entries end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination diff --git a/spec/features/parental_consent_clinic_spec.rb b/spec/features/parental_consent_clinic_spec.rb index c2070897c5..d2e92aaede 100644 --- a/spec/features/parental_consent_clinic_spec.rb +++ b/spec/features/parental_consent_clinic_spec.rb @@ -257,14 +257,14 @@ def when_i_submit_the_consent_form def then_i_see_a_confirmation_page_in_school expect(page).to have_content("is due to get the HPV vaccination at school") - perform_enqueued_jobs # match consent form with patient + Sidekiq::Job.drain_all # match consent form with patient end def then_i_see_a_confirmation_page_in_clinic expect(page).to have_content("is due to get the HPV vaccination") expect(page).not_to have_content("at school") - perform_enqueued_jobs # match consent form with patient + Sidekiq::Job.drain_all # match consent form with patient expect_email_to "jane@example.com", :consent_confirmation_clinic, :any end @@ -347,7 +347,7 @@ def and_the_patient_has_a_parent_with_known_contact_details end def then_the_existing_parent_receives_a_contact_details_mismatch_warning - perform_enqueued_jobs + Sidekiq::Job.drain_all expect_email_to "original@example.com", :consent_unknown_contact_details_warning, :any diff --git a/spec/features/parental_consent_create_patient_spec.rb b/spec/features/parental_consent_create_patient_spec.rb index 42d8c05cf7..34cd9a534e 100644 --- a/spec/features/parental_consent_create_patient_spec.rb +++ b/spec/features/parental_consent_create_patient_spec.rb @@ -163,7 +163,7 @@ def then_i_see_the_consent_confirmation_page end def and_i_wait_for_background_jobs_to_complete - perform_enqueued_jobs + Sidekiq::Job.drain_all end def when_the_nurse_checks_the_unmatched_consent_responses diff --git a/spec/features/parental_consent_flu_spec.rb b/spec/features/parental_consent_flu_spec.rb index 7ca7a132a0..f9d02a0c3f 100644 --- a/spec/features/parental_consent_flu_spec.rb +++ b/spec/features/parental_consent_flu_spec.rb @@ -243,10 +243,9 @@ def and_i_choose_an_ethnic_group end def and_i_choose_an_ethnic_background - perform_enqueued_jobs do - choose "White and Black Caribbean" - click_button "Continue" - end + choose "White and Black Caribbean" + click_button "Continue" + Sidekiq::Job.drain_all end def and_the_ethnicity_information_is_shown @@ -256,10 +255,9 @@ def and_the_ethnicity_information_is_shown end def and_i_refuse_to_answer_questions_on_ethnicity - perform_enqueued_jobs do - choose "No, skip the ethnicity questions" - click_on "Continue" - end + choose "No, skip the ethnicity questions" + click_on "Continue" + Sidekiq::Job.drain_all end def then_i_see_the_consent_confirmation_page diff --git a/spec/features/parental_consent_hpv_spec.rb b/spec/features/parental_consent_hpv_spec.rb index 5fe69a49b9..0e844987cf 100644 --- a/spec/features/parental_consent_hpv_spec.rb +++ b/spec/features/parental_consent_hpv_spec.rb @@ -140,7 +140,7 @@ def then_i_see_the_confirmation_page end def when_i_wait_for_the_background_jobs_to_complete - perform_enqueued_jobs(only: ProcessConsentFormJob) + ProcessConsentFormSidekiqJob.drain end def then_i_get_a_confirmation_email_and_scheduled_survey_email diff --git a/spec/features/parental_consent_inexact_auto_match_spec.rb b/spec/features/parental_consent_inexact_auto_match_spec.rb index 75f46e89aa..db2c13c9c2 100644 --- a/spec/features/parental_consent_inexact_auto_match_spec.rb +++ b/spec/features/parental_consent_inexact_auto_match_spec.rb @@ -98,7 +98,7 @@ def when_a_parent_gives_consent_with_three_of_four_fields_matching_the_cohort end def and_the_nurse_checks_the_consent_responses - 2.times { perform_enqueued_jobs } + 2.times { Sidekiq::Job.drain_all } sign_in @team.users.first visit sessions_path diff --git a/spec/features/parental_consent_manual_consent_reminders_send_spec.rb b/spec/features/parental_consent_manual_consent_reminders_send_spec.rb index 57240c836e..9ddaa809e8 100644 --- a/spec/features/parental_consent_manual_consent_reminders_send_spec.rb +++ b/spec/features/parental_consent_manual_consent_reminders_send_spec.rb @@ -151,6 +151,7 @@ def and_i_do_not_see_the_send_reminders_button def when_i_click_send_reminders click_on "Send manual consent reminders" + SendManualSchoolConsentRemindersJob.drain end def then_i_see_the_success_message diff --git a/spec/features/parental_consent_no_session_spec.rb b/spec/features/parental_consent_no_session_spec.rb index 160250b5d1..28a86159a9 100644 --- a/spec/features/parental_consent_no_session_spec.rb +++ b/spec/features/parental_consent_no_session_spec.rb @@ -119,7 +119,7 @@ def then_i_see_the_confirmation_page end def when_i_wait_for_the_background_jobs_to_complete - perform_enqueued_jobs(only: ProcessConsentFormJob) + ProcessConsentFormSidekiqJob.drain end def then_i_get_a_confirmation_email_and_scheduled_survey_email diff --git a/spec/features/parental_consent_refused_spec.rb b/spec/features/parental_consent_refused_spec.rb index c23620b5bb..2c080e9f09 100644 --- a/spec/features/parental_consent_refused_spec.rb +++ b/spec/features/parental_consent_refused_spec.rb @@ -198,7 +198,7 @@ def then_i_see_the_confirmation_page end def when_i_wait_for_the_background_jobs_to_complete - perform_enqueued_jobs(only: ProcessConsentFormJob) + ProcessConsentFormSidekiqJob.drain end def and_i_receive_an_email_confirming_that_my_child_wont_be_vaccinated diff --git a/spec/features/patient_invalidation_spec.rb b/spec/features/patient_invalidation_spec.rb index d20e0946b0..2f7c545f5f 100644 --- a/spec/features/patient_invalidation_spec.rb +++ b/spec/features/patient_invalidation_spec.rb @@ -57,9 +57,10 @@ def when_a_pds_check_is_completed_which_returns_that_the_patient_is_invalid # Move time forward to ensure deletion sync happens after the initial sync travel_to(1.hour.from_now) + stub_pds_search_to_return_no_patients stub_pds_get_nhs_number_to_return_an_invalidated_patient - PatientUpdateFromPDSJob.perform_now(@patient) + PatientUpdateFromPDSJob.new.perform(@patient, []) end def then_the_patient_has_been_invalidated diff --git a/spec/features/scheduled_consent_requests_and_reminders_spec.rb b/spec/features/scheduled_consent_requests_and_reminders_spec.rb index f37a467820..b093ecbcfb 100644 --- a/spec/features/scheduled_consent_requests_and_reminders_spec.rb +++ b/spec/features/scheduled_consent_requests_and_reminders_spec.rb @@ -188,8 +188,7 @@ def when_1_more_day_passes end def then_no_consent_requests_have_been_sent - EnqueueSchoolConsentRequestsJob.perform_now - perform_enqueued_jobs + EnqueueSchoolConsentRequestsJob.new.perform Sidekiq::Job.drain_all expect(email_deliveries).to be_empty @@ -215,8 +214,7 @@ def when_7_more_days_pass end def then_all_four_parents_received_all_programme_consent_requests - EnqueueSchoolConsentRequestsJob.perform_now - perform_enqueued_jobs + EnqueueSchoolConsentRequestsJob.new.perform Sidekiq::Job.drain_all parent_emails.each do |email| @@ -264,8 +262,7 @@ def then_all_four_parents_received_all_programme_consent_requests end def then_all_four_parents_received_all_programme_reminders - EnqueueSchoolConsentRemindersJob.perform_now - perform_enqueued_jobs + EnqueueSchoolConsentRemindersJob.new.perform Sidekiq::Job.drain_all parent_emails.each do |email| diff --git a/spec/features/schools_spec.rb b/spec/features/schools_spec.rb index b6a51b7493..822f38ac18 100644 --- a/spec/features/schools_spec.rb +++ b/spec/features/schools_spec.rb @@ -255,7 +255,7 @@ def then_i_see_the_invitation_confirmation end def and_the_parents_receive_invitations - perform_enqueued_jobs + Sidekiq::Job.drain_all expect(email_deliveries.count).to eq(@patients.count) diff --git a/spec/features/self_consent_spec.rb b/spec/features/self_consent_spec.rb index dd4b4d82a0..ca34dd32ed 100644 --- a/spec/features/self_consent_spec.rb +++ b/spec/features/self_consent_spec.rb @@ -282,6 +282,6 @@ def and_the_child_should_be_safe_to_vaccinate end def and_enqueued_jobs_run_with_no_errors - expect { perform_enqueued_jobs }.not_to raise_error + expect { Sidekiq::Job.drain_all }.not_to raise_error end end diff --git a/spec/features/session_school_reminder_spec.rb b/spec/features/session_school_reminder_spec.rb index 741603bdc7..378ae8603d 100644 --- a/spec/features/session_school_reminder_spec.rb +++ b/spec/features/session_school_reminder_spec.rb @@ -38,8 +38,7 @@ def and_a_patient_has_given_consent end def when_school_session_reminders_are_sent - EnqueueSchoolSessionRemindersJob.perform_now - perform_enqueued_jobs + EnqueueSchoolSessionRemindersJob.new.perform Sidekiq::Job.drain_all end diff --git a/spec/features/sessions_school_spec.rb b/spec/features/sessions_school_spec.rb index b985855365..29c77b4488 100644 --- a/spec/features/sessions_school_spec.rb +++ b/spec/features/sessions_school_spec.rb @@ -458,7 +458,7 @@ def then_i_see_the_invitation_confirmation end def and_the_parent_receives_an_invitation - perform_enqueued_jobs + Sidekiq::Job.drain_all expect(email_deliveries).to include( matching_notify_email( @@ -560,7 +560,7 @@ def given_my_team_is_running_an_hpv_vaccination_programme_for_ods_code( end def then_the_parent_receives_an_rt5_clinic_invitation - perform_enqueued_jobs + Sidekiq::Job.drain_all expect(email_deliveries).to include( matching_notify_email( to: @parent.email, @@ -576,7 +576,7 @@ def then_the_parent_receives_an_rt5_clinic_invitation end def then_the_parent_receives_a_ryg_clinic_invitation - perform_enqueued_jobs + Sidekiq::Job.drain_all expect(email_deliveries).to include( matching_notify_email( to: @parent.email, diff --git a/spec/features/td_ipv_vaccination_administered_spec.rb b/spec/features/td_ipv_vaccination_administered_spec.rb index 6631494381..94fbe8d2f7 100644 --- a/spec/features/td_ipv_vaccination_administered_spec.rb +++ b/spec/features/td_ipv_vaccination_administered_spec.rb @@ -42,7 +42,7 @@ when_i_confirm_the_details then_i_see_a_success_message and_i_no_longer_see_the_patient_in_the_record_tab - and_the_vaccination_record_is_not_synced_to_nhs + and_the_vaccination_record_is_synced_to_nhs when_i_go_back and_i_save_changes @@ -209,7 +209,7 @@ def and_i_see_the_vaccination_details end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination @@ -240,8 +240,8 @@ def and_a_text_is_sent_to_the_parent_confirming_the_vaccination ) end - def and_the_vaccination_record_is_not_synced_to_nhs - perform_enqueued_jobs - expect(@stubbed_post_request).not_to have_been_requested + def and_the_vaccination_record_is_synced_to_nhs + Sidekiq::Job.drain_all + expect(@stubbed_post_request).to have_been_requested end end diff --git a/spec/features/triage_then_parental_consent_refused_spec.rb b/spec/features/triage_then_parental_consent_refused_spec.rb index dadfeaea70..c836a238ae 100644 --- a/spec/features/triage_then_parental_consent_refused_spec.rb +++ b/spec/features/triage_then_parental_consent_refused_spec.rb @@ -119,7 +119,7 @@ def then_i_see_the_confirmation_page end def when_i_wait_for_background_jobs_to_complete - perform_enqueued_jobs + Sidekiq::Job.drain_all end def and_i_go_to_the_patient_with_conflicting_consent diff --git a/spec/features/vaccination_offline_spec.rb b/spec/features/vaccination_offline_spec.rb index 1fb08f72e8..1163b79205 100644 --- a/spec/features/vaccination_offline_spec.rb +++ b/spec/features/vaccination_offline_spec.rb @@ -591,7 +591,7 @@ def then_the_clinic_location_is_displayed end def when_vaccination_confirmations_are_sent - SendVaccinationConfirmationsJob.perform_now + SendVaccinationConfirmationsJob.new.perform end def then_an_email_is_sent_to_the_parent_confirming_the_vaccination diff --git a/spec/jobs/enqueue_location_position_updater_job_spec.rb b/spec/jobs/enqueue_location_position_updater_job_spec.rb index 670d3f1dc1..80de0ee05f 100644 --- a/spec/jobs/enqueue_location_position_updater_job_spec.rb +++ b/spec/jobs/enqueue_location_position_updater_job_spec.rb @@ -1,35 +1,33 @@ # frozen_string_literal: true describe EnqueueLocationPositionUpdaterJob do - describe "#perform" do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } - let!(:location_with_address_no_position) do - create(:community_clinic, position: nil) - end + let!(:location_with_address_no_position) do + create(:community_clinic, position: nil) + end - let!(:location_with_position) { create(:community_clinic) } + let!(:location_with_position) { create(:community_clinic) } - let!(:location_without_address) do - create(:community_clinic, :without_address) - end + let!(:location_without_address) do + create(:community_clinic, :without_address) + end - it "enqueues jobs for locations with address but no position" do - expect { perform_now }.to enqueue_sidekiq_job( - LocationPositionUpdaterJob - ).with(location_with_address_no_position.id) - end + it "enqueues jobs for locations with address but no position" do + expect { perform }.to enqueue_sidekiq_job(LocationPositionUpdaterJob).with( + location_with_address_no_position.id + ) + end - it "does not enqueue jobs for locations with position" do - expect { perform_now }.not_to enqueue_sidekiq_job( - LocationPositionUpdaterJob - ).with(location_with_position.id) - end + it "does not enqueue jobs for locations with position" do + expect { perform }.not_to enqueue_sidekiq_job( + LocationPositionUpdaterJob + ).with(location_with_position.id) + end - it "does not enqueue jobs for locations without address" do - expect { perform_now }.not_to enqueue_sidekiq_job( - LocationPositionUpdaterJob - ).with(location_without_address.id) - end + it "does not enqueue jobs for locations without address" do + expect { perform }.not_to enqueue_sidekiq_job( + LocationPositionUpdaterJob + ).with(location_without_address.id) end end diff --git a/spec/jobs/enqueue_patients_aged_out_of_schools_job_spec.rb b/spec/jobs/enqueue_patients_aged_out_of_schools_job_spec.rb index 3ecb719e5b..4b1b659083 100644 --- a/spec/jobs/enqueue_patients_aged_out_of_schools_job_spec.rb +++ b/spec/jobs/enqueue_patients_aged_out_of_schools_job_spec.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true describe EnqueuePatientsAgedOutOfSchoolsJob do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } let!(:school_with_team) { create(:gias_school, team: create(:team)) } before { create(:gias_school) } it "queues jobs for the schools with teams" do - expect { perform_now }.to enqueue_sidekiq_job( + expect { perform }.to enqueue_sidekiq_job( PatientsAgedOutOfSchoolJob ).once.with(school_with_team.id) end diff --git a/spec/jobs/enqueue_process_unmatched_consent_forms_job_spec.rb b/spec/jobs/enqueue_process_unmatched_consent_forms_job_spec.rb index 82ed67ad56..100832a436 100644 --- a/spec/jobs/enqueue_process_unmatched_consent_forms_job_spec.rb +++ b/spec/jobs/enqueue_process_unmatched_consent_forms_job_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe EnqueueProcessUnmatchedConsentFormsJob do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } let(:programme) { Programme.hpv } let(:team) { create(:team, programmes: [programme]) } @@ -20,26 +20,26 @@ end it "enqueues a job for each unmatched consent form" do - expect { perform_now }.to have_enqueued_job(ProcessConsentFormJob).exactly( - 1 - ).times + expect { perform }.to enqueue_sidekiq_job( + ProcessConsentFormSidekiqJob + ).exactly(1).times end it "enqueues a job for the unmatched consent form" do - expect { perform_now }.to have_enqueued_job(ProcessConsentFormJob).with( - unmatched_consent_form.id - ) + expect { perform }.to enqueue_sidekiq_job( + ProcessConsentFormSidekiqJob + ).with(unmatched_consent_form.id) end it "does not enqueue a job for the draft consent form" do - expect { perform_now }.not_to have_enqueued_job(ProcessConsentFormJob).with( - draft_consent_form.id - ) + expect { perform }.not_to enqueue_sidekiq_job( + ProcessConsentFormSidekiqJob + ).with(draft_consent_form.id) end it "does not enqueue a job for the archived consent form" do - expect { perform_now }.not_to have_enqueued_job(ProcessConsentFormJob).with( - archived_consent_form.id - ) + expect { perform }.not_to enqueue_sidekiq_job( + ProcessConsentFormSidekiqJob + ).with(archived_consent_form.id) end end diff --git a/spec/jobs/enqueue_school_consent_reminders_job_spec.rb b/spec/jobs/enqueue_school_consent_reminders_job_spec.rb index 8cc7ff43b5..57c1c2b656 100644 --- a/spec/jobs/enqueue_school_consent_reminders_job_spec.rb +++ b/spec/jobs/enqueue_school_consent_reminders_job_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe EnqueueSchoolConsentRemindersJob do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } let(:programmes) { [Programme.sample] } let(:team) { create(:team, programmes:) } @@ -27,8 +27,8 @@ let(:today) { dates.first - 2.weeks } it "doesn't queue any jobs" do - expect { perform_now }.not_to have_enqueued_job( - SendAutomaticSchoolConsentRemindersJob + expect { perform }.not_to enqueue_sidekiq_job( + SendAutomaticSchoolConsentRemindersSidekiqJob ) end end @@ -37,17 +37,17 @@ let(:today) { dates.first - 1.week } it "queues a job for the session" do - expect { perform_now }.to have_enqueued_job( - SendAutomaticSchoolConsentRemindersJob - ).with(session) + expect { perform }.to enqueue_sidekiq_job( + SendAutomaticSchoolConsentRemindersSidekiqJob + ).with(session.id) end context "when location is a generic clinic" do let(:location) { create(:generic_clinic, team:) } it "doesn't queue any jobs" do - expect { perform_now }.not_to have_enqueued_job( - SendAutomaticSchoolConsentRemindersJob + expect { perform }.not_to enqueue_sidekiq_job( + SendAutomaticSchoolConsentRemindersSidekiqJob ) end end @@ -57,17 +57,17 @@ let(:today) { dates.last - 1.week } it "queues a job for the session" do - expect { perform_now }.to have_enqueued_job( - SendAutomaticSchoolConsentRemindersJob - ).with(session) + expect { perform }.to enqueue_sidekiq_job( + SendAutomaticSchoolConsentRemindersSidekiqJob + ).with(session.id) end context "when location is a generic clinic" do let(:location) { create(:generic_clinic, team:) } it "doesn't queue any jobs" do - expect { perform_now }.not_to have_enqueued_job( - SendAutomaticSchoolConsentRemindersJob + expect { perform }.not_to enqueue_sidekiq_job( + SendAutomaticSchoolConsentRemindersSidekiqJob ) end end diff --git a/spec/jobs/enqueue_school_consent_requests_job_spec.rb b/spec/jobs/enqueue_school_consent_requests_job_spec.rb index 8259c3ecf9..86d73b2696 100644 --- a/spec/jobs/enqueue_school_consent_requests_job_spec.rb +++ b/spec/jobs/enqueue_school_consent_requests_job_spec.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true describe EnqueueSchoolConsentRequestsJob do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } context "when session is unscheduled" do let(:session) { create(:session, :unscheduled) } it "doesn't queue any jobs" do - expect { perform_now }.not_to have_enqueued_job( - SendSchoolConsentRequestsJob + expect { perform }.not_to enqueue_sidekiq_job( + SendSchoolConsentRequestsSidekiqJob ) end end @@ -19,8 +19,8 @@ end it "doesn't queue any jobs" do - expect { perform_now }.not_to have_enqueued_job( - SendSchoolConsentRequestsJob + expect { perform }.not_to enqueue_sidekiq_job( + SendSchoolConsentRequestsSidekiqJob ) end end @@ -35,9 +35,9 @@ end it "queues a job for the session" do - expect { perform_now }.to have_enqueued_job( - SendSchoolConsentRequestsJob - ).with(session) + expect { perform }.to enqueue_sidekiq_job( + SendSchoolConsentRequestsSidekiqJob + ).with(session.id) end context "when location is a generic clinic" do @@ -47,8 +47,8 @@ end it "doesn't queue any jobs" do - expect { perform_now }.not_to have_enqueued_job( - SendSchoolConsentRequestsJob + expect { perform }.not_to enqueue_sidekiq_job( + SendSchoolConsentRequestsSidekiqJob ) end end diff --git a/spec/jobs/enqueue_school_session_reminders_job_spec.rb b/spec/jobs/enqueue_school_session_reminders_job_spec.rb index c098a8ea48..8a12b14232 100644 --- a/spec/jobs/enqueue_school_session_reminders_job_spec.rb +++ b/spec/jobs/enqueue_school_session_reminders_job_spec.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true describe EnqueueSchoolSessionRemindersJob do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } context "with a session from last week" do let(:session) { create(:session, :completed) } it "doesn't queue a job" do - expect { perform_now }.not_to have_enqueued_job( - SendSchoolSessionRemindersJob - ).with(session) + expect { perform }.not_to enqueue_sidekiq_job( + SendSchoolSessionRemindersSidekiqJob + ) end end @@ -17,9 +17,9 @@ let(:session) { create(:session, :today) } it "doesn't queue a job" do - expect { perform_now }.not_to have_enqueued_job( - SendSchoolSessionRemindersJob - ).with(session) + expect { perform }.not_to enqueue_sidekiq_job( + SendSchoolSessionRemindersSidekiqJob + ) end end @@ -27,9 +27,9 @@ let(:session) { create(:session, :tomorrow) } it "queues a job" do - expect { perform_now }.to have_enqueued_job( - SendSchoolSessionRemindersJob - ).with(session) + expect { perform }.to enqueue_sidekiq_job( + SendSchoolSessionRemindersSidekiqJob + ).with(session.id) end end @@ -37,9 +37,9 @@ let(:session) { create(:session, :scheduled) } it "doesn't queue a job" do - expect { perform_now }.not_to have_enqueued_job( - SendSchoolSessionRemindersJob - ).with(session) + expect { perform }.not_to enqueue_sidekiq_job( + SendSchoolSessionRemindersSidekiqJob + ) end end end diff --git a/spec/jobs/enqueue_update_patients_from_pds_job_spec.rb b/spec/jobs/enqueue_update_patients_from_pds_job_spec.rb index 4f156a663f..3e6286988e 100644 --- a/spec/jobs/enqueue_update_patients_from_pds_job_spec.rb +++ b/spec/jobs/enqueue_update_patients_from_pds_job_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe EnqueueUpdatePatientsFromPDSJob do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } let!(:invalidated_patient) { create(:patient, :invalidated) } let!(:deceased_patient) { create(:patient, :deceased) } @@ -20,44 +20,44 @@ end it "only queues jobs for the appropriate patients" do - expect { perform_now }.to have_enqueued_job( - PatientUpdateFromPDSJob + expect { perform }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob ).exactly(4).times end it "queues a job for the invalidated patient" do - expect { perform_now }.to have_enqueued_job(PatientUpdateFromPDSJob).with( - invalidated_patient - ) + expect { perform }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).with(invalidated_patient.id, nil) end it "doesn't queue a job for the deceased patient" do - expect { perform_now }.not_to have_enqueued_job( - PatientUpdateFromPDSJob - ).with(deceased_patient) + expect { perform }.not_to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).with(deceased_patient.id, nil) end it "doesn't queue a job for the recently updated patient" do - expect { perform_now }.not_to have_enqueued_job( - PatientUpdateFromPDSJob - ).with(recently_updated_patient) + expect { perform }.not_to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).with(recently_updated_patient.id, nil) end it "queues a job for the restricted patient" do - expect { perform_now }.to have_enqueued_job(PatientUpdateFromPDSJob).with( - restricted_patient - ) + expect { perform }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).with(restricted_patient.id, nil) end it "queues a job for the not recently updated patient" do - expect { perform_now }.to have_enqueued_job(PatientUpdateFromPDSJob).with( - not_recently_updated_patient - ) + expect { perform }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).with(not_recently_updated_patient.id, nil) end it "queues a job for the never updated patient" do - expect { perform_now }.to have_enqueued_job(PatientUpdateFromPDSJob).with( - never_updated_patient - ) + expect { perform }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).with(never_updated_patient.id, nil) end end diff --git a/spec/jobs/enqueue_vaccinations_search_in_nhs_job_spec.rb b/spec/jobs/enqueue_vaccinations_search_in_nhs_job_spec.rb index 202abebbd3..7b3c3e6eed 100644 --- a/spec/jobs/enqueue_vaccinations_search_in_nhs_job_spec.rb +++ b/spec/jobs/enqueue_vaccinations_search_in_nhs_job_spec.rb @@ -1,513 +1,501 @@ # frozen_string_literal: true -describe EnqueueVaccinationsSearchInNHSJob do - include ActiveJob::TestHelper - - describe "#perform", :within_academic_year do - let(:flu) { Programme.flu } - let(:programmes) { [flu] } - let(:team) { create(:team) } - let(:gias_year_groups) { (0..11).to_a } - let(:location) { school } - let(:school) { create(:gias_school, team:, programmes:) } - let(:clinic) { create(:generic_clinic, team:, programmes:) } - let(:days_before_consent_reminders) { 7 } - let(:session) do - send_consent_requests_at = - if location.generic_clinic? - nil - else - send_consent_requests_or_invitations_at - end +describe EnqueueVaccinationsSearchInNHSJob, :within_academic_year do + subject(:perform) { described_class.new.perform } + + let(:flu) { Programme.flu } + let(:programmes) { [flu] } + let(:team) { create(:team) } + let(:gias_year_groups) { (0..11).to_a } + let(:location) { school } + let(:school) { create(:gias_school, team:, programmes:) } + let(:clinic) { create(:generic_clinic, team:, programmes:) } + let(:days_before_consent_reminders) { 7 } + let(:session) do + send_consent_requests_at = + (location.generic_clinic? ? nil : send_consent_requests_or_invitations_at) + + create( + :session, + programmes:, + academic_year: AcademicYear.pending, + dates:, + send_consent_requests_at:, + days_before_consent_reminders:, + team:, + location: + ) + end + let(:dates) { [14.days.from_now] } + let(:send_consent_requests_or_invitations_at) { 7.days.ago } - create( - :session, - programmes:, - academic_year: AcademicYear.pending, - dates:, - send_consent_requests_at:, - days_before_consent_reminders:, - team:, - location: - ) + describe "session searches" do + before do + setup_feature_flag + allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) + + # Ensure expectation patients and sessions are created before the job runs + searchable_patients end - let(:dates) { [14.days.from_now] } - let(:send_consent_requests_or_invitations_at) { 7.days.ago } - describe "session searches" do - before do - setup_feature_flag - allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) + def setup_feature_flag + Flipper.enable(:imms_api_enqueue_session_searches) + end - # Ensure expectation patients and sessions are created before the job runs - searchable_patients - end + let(:searchable_patients) { [create(:patient, team:, school:, session:)] } + context "with feature flag disabled" do def setup_feature_flag - Flipper.enable(:imms_api_enqueue_session_searches) + Flipper.disable(:imms_api_enqueue_session_searches) end - let(:searchable_patients) { [create(:patient, team:, school:, session:)] } + it "does not perform searches on the session patients" do + expect(SearchVaccinationRecordsInNHSJob).not_to have_received( + :perform_bulk + ) + end + end - context "with feature flag disabled" do - def setup_feature_flag - Flipper.disable(:imms_api_enqueue_session_searches) - end + shared_examples "behaviour before, during or after consent/invitation period" do + context "before the consent or invitation period for the session", + within_academic_year: { + from_start: 30.days + } do + let(:dates) { [30.days.from_now] } + let(:send_consent_requests_or_invitations_at) { 7.days.from_now } + + it "does not perform any searches" do + perform - it "does not perform searches on the session patients" do expect(SearchVaccinationRecordsInNHSJob).not_to have_received( :perform_bulk ) end end - shared_examples "behaviour before, during or after consent/invitation period" do - context "before the consent or invitation period for the session", - within_academic_year: { - from_start: 30.days - } do - let(:dates) { [30.days.from_now] } - let(:send_consent_requests_or_invitations_at) { 7.days.from_now } + context "within the consent or invitation period for the session", + within_academic_year: { + from_start: 7.days + } do + let(:dates) { [7.days.from_now] } + let(:send_consent_requests_or_invitations_at) { 14.days.ago } - it "does not perform any searches" do - described_class.perform_now + it "performs searches on the session patients" do + perform - expect(SearchVaccinationRecordsInNHSJob).not_to have_received( - :perform_bulk - ) - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with(searchable_patients.map(&:id).zip) end + end - context "within the consent or invitation period for the session", - within_academic_year: { - from_start: 7.days - } do - let(:dates) { [7.days.from_now] } - let(:send_consent_requests_or_invitations_at) { 14.days.ago } + context "after the session dates", + within_academic_year: { + from_start: 7.days + } do + let(:dates) { [7.days.ago] } + let(:send_consent_requests_or_invitations_at) { 30.days.ago } - it "performs searches on the session patients" do - described_class.perform_now + it "does not perform any searches" do + perform - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once.with(searchable_patients.map(&:id).zip) - end + expect(SearchVaccinationRecordsInNHSJob).not_to have_received( + :perform_bulk + ) end + end - context "after the session dates", - within_academic_year: { - from_start: 7.days - } do - let(:dates) { [7.days.ago] } - let(:send_consent_requests_or_invitations_at) { 30.days.ago } + context "in between multiple session dates", + within_academic_year: { + from_start: 7.days + } do + let(:dates) { [7.days.ago, 7.days.from_now] } + let(:send_consent_requests_or_invitations_at) { 14.days.ago } - it "does not perform any searches" do - described_class.perform_now + it "performs searches on the session patients" do + perform - expect(SearchVaccinationRecordsInNHSJob).not_to have_received( - :perform_bulk - ) - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with(searchable_patients.map(&:id).zip) end + end - context "in between multiple session dates", - within_academic_year: { - from_start: 7.days - } do - let(:dates) { [7.days.ago, 7.days.from_now] } - let(:send_consent_requests_or_invitations_at) { 14.days.ago } + context "on the threshold of the consent or invitation period", + within_academic_year: { + from_start: 7.days + } do + let(:dates) { [30.days.from_now] } + let(:send_consent_requests_or_invitations_at) { 2.days.from_now } - it "performs searches on the session patients" do - described_class.perform_now + it "performs searches on the session patients" do + perform - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once.with(searchable_patients.map(&:id).zip) - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with(searchable_patients.map(&:id).zip) end - context "on the threshold of the consent or invitation period", - within_academic_year: { - from_start: 7.days - } do - let(:dates) { [30.days.from_now] } - let(:send_consent_requests_or_invitations_at) { 2.days.from_now } + context "when run just after midnight BST" do + around do |example| + travel_to(Time.zone.parse("2026-06-15 00:15")) { example.run } + end it "performs searches on the session patients" do - described_class.perform_now + perform expect(SearchVaccinationRecordsInNHSJob).to have_received( :perform_bulk ).once.with(searchable_patients.map(&:id).zip) end + end + end + end - context "when run just after midnight BST" do - around do |example| - travel_to(Time.zone.parse("2026-06-15 00:15")) { example.run } - end + context "with a normal school session" do + let(:location) { school } - it "performs searches on the session patients" do - described_class.perform_now + include_examples "behaviour before, during or after consent/invitation period" + end - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once.with(searchable_patients.map(&:id).zip) - end - end - end + context "with sessions from previous academic years" do + before do + # The session searches only trigger for patients in sessions from the + # current academic year, so these should not show up. + old_session = + create( + :session, + programmes: [flu], + academic_year: AcademicYear.previous, + dates: [Date.new(AcademicYear.previous, 10, 1)], + send_consent_requests_at: 1.day.ago, + team: + ) + create(:patient, team:, session: old_session, year_group: 8) end - context "with a normal school session" do - let(:location) { school } + let(:location) { school } + + include_examples "behaviour before, during or after consent/invitation period" + end + + (Programme.all - [Programme.flu]).each do |other_programme| + context "in a #{other_programme.type} programmes" do + let(:programmes) { [other_programme] } include_examples "behaviour before, during or after consent/invitation period" end + end - context "with sessions from previous academic years" do - before do - # The session searches only trigger for patients in sessions from the - # current academic year, so these should not show up. - old_session = - create( - :session, - programmes: [flu], - academic_year: AcademicYear.previous, - dates: [Date.new(AcademicYear.previous, 10, 1)], - send_consent_requests_at: 1.day.ago, - team: - ) - create(:patient, team:, session: old_session, year_group: 8) - end + context "with patients with no NHS number" do + before { create(:patient, team:, school:, session:, nhs_number: nil) } - let(:location) { school } + it "does not perform searches for those patients" do + perform - include_examples "behaviour before, during or after consent/invitation period" + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).with(searchable_patients.map(&:id).zip) end + end + end - (Programme.all - [Programme.flu]).each do |other_programme| - context "in a #{other_programme.type} programmes" do - let(:programmes) { [other_programme] } + describe "doing the rolling searches" do + before do + Flipper.enable(:imms_api_enqueue_rolling_searches) + allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) + end - include_examples "behaviour before, during or after consent/invitation period" - end + let(:school) { create(:gias_school, team:, programmes:, gias_year_groups:) } + let(:location) { school } + let(:session) { create(:session, programmes:, team:, location:) } + + context "on a single patient" do + before do + # Ensure expectation patients are created before the job runs + patient_programme_vaccinations_search if last_searched_at end - context "with patients with no NHS number" do - before { create(:patient, team:, school:, session:, nhs_number: nil) } + let(:patient_programme_vaccinations_search) do + create( + :patient_programme_vaccinations_search, + programme: flu, + last_searched_at:, + patient: + ) + end + let(:last_searched_at) { 30.days.ago } # default for specs that don't care + let!(:patient) { create(:patient, team:, school:, session:) } - it "does not perform searches for those patients" do - described_class.perform_now + context "with the feature flag disabled" do + before { Flipper.disable(:imms_api_enqueue_rolling_searches) } - expect(SearchVaccinationRecordsInNHSJob).to have_received( + it "does not perform a search on the patient" do + perform + + expect(SearchVaccinationRecordsInNHSJob).not_to have_received( :perform_bulk - ).with(searchable_patients.map(&:id).zip) + ) end end - end - describe "doing the rolling searches" do - before do - Flipper.enable(:imms_api_enqueue_rolling_searches) - allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) - end + context "that has no previous searches" do + let(:last_searched_at) { nil } - let(:school) do - create(:gias_school, team:, programmes:, gias_year_groups:) - end - let(:location) { school } - let(:session) { create(:session, programmes:, team:, location:) } + it "performs a search on the patients" do + perform - context "on a single patient" do - before do - # Ensure expectation patients are created before the job runs - patient_programme_vaccinations_search if last_searched_at - end - - let(:patient_programme_vaccinations_search) do - create( - :patient_programme_vaccinations_search, - programme: flu, - last_searched_at:, - patient: - ) + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with([[patient.id]]) end - let(:last_searched_at) { 30.days.ago } # default for specs that don't care - let!(:patient) { create(:patient, team:, school:, session:) } + end - context "with the feature flag disabled" do - before { Flipper.disable(:imms_api_enqueue_rolling_searches) } + context "that has searches 28 days ago or older" do + let(:last_searched_at) { 28.days.ago } - it "does not perform a search on the patient" do - described_class.perform_now + it "performs a search on the patient" do + perform - expect(SearchVaccinationRecordsInNHSJob).not_to have_received( - :perform_bulk - ) - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with([[patient.id]]) end + end - context "that has no previous searches" do - let(:last_searched_at) { nil } + context "that has searches less than 28 days" do + let(:last_searched_at) { 27.days.ago } - it "performs a search on the patients" do - described_class.perform_now + it "does not perform a search on the patient" do + perform - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once.with([[patient.id]]) - end + expect(SearchVaccinationRecordsInNHSJob).not_to have_received( + :perform_bulk + ) end + end - context "that has searches 28 days ago or older" do - let(:last_searched_at) { 28.days.ago } + (Programme.all - [Programme.flu]).each do |other_programme| + context "in a #{other_programme.type} programmes" do + let(:programmes) { [other_programme] } it "performs a search on the patient" do - described_class.perform_now + perform expect(SearchVaccinationRecordsInNHSJob).to have_received( :perform_bulk ).once.with([[patient.id]]) end end + end - context "that has searches less than 28 days" do - let(:last_searched_at) { 27.days.ago } - - it "does not perform a search on the patient" do - described_class.perform_now - - expect(SearchVaccinationRecordsInNHSJob).not_to have_received( - :perform_bulk - ) - end + context "that does not have an NHS number" do + let(:patient) do + create(:patient, team:, school:, session:, nhs_number: nil) end - (Programme.all - [Programme.flu]).each do |other_programme| - context "in a #{other_programme.type} programmes" do - let(:programmes) { [other_programme] } - - it "performs a search on the patient" do - described_class.perform_now + it "does not perform a search on the patient" do + perform - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once.with([[patient.id]]) - end - end + expect(SearchVaccinationRecordsInNHSJob).not_to have_received( + :perform_bulk + ) end + end - context "that does not have an NHS number" do - let(:patient) do - create(:patient, team:, school:, session:, nhs_number: nil) - end + context "that is part of a national reporting only team" do + let(:team) { create(:team, :national_reporting) } - it "does not perform a search on the patient" do - described_class.perform_now + it "does not perform a search on the patient" do + perform - expect(SearchVaccinationRecordsInNHSJob).not_to have_received( - :perform_bulk - ) - end + expect(SearchVaccinationRecordsInNHSJob).not_to have_received( + :perform_bulk + ) end + end - context "that is part of a national reporting only team" do - let(:team) { create(:team, :national_reporting) } + context "that is linked to both a poc and a national reporting team" do + before do + national_reporting_team = create(:team, :national_reporting) + create( + :vaccination_record, + :sourced_from_national_reporting, + patient:, + team: national_reporting_team + ) + end - it "does not perform a search on the patient" do - described_class.perform_now + it "does perform a search on the patient" do + perform - expect(SearchVaccinationRecordsInNHSJob).not_to have_received( - :perform_bulk - ) - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ) end + end + end - context "that is linked to both a poc and a national reporting team" do - before do - national_reporting_team = create(:team, :national_reporting) - create( - :vaccination_record, - :sourced_from_national_reporting, - patient:, - team: national_reporting_team - ) + context "with many patients" do + # Ignore patient from previous tests + let(:patient) {} + # let(:last_searched_at) { 28.days.ago } + let!(:patients) do + # Importing patients this way is significantly faster than creating + # them with create / create_list. Not using a factory could end up + # with broken tests, but since the expectations further down expect to + # find at least 2 patients, I think it'll reliably stop working if the + # underlying structure changes in the future so we won't end up with a + # false-positive test. + + Patient.import(build_list(:patient, 50, team:, school:, session:)) + PatientLocation.import( + Patient.all.map do + { + patient_id: it.id, + location_id: location.id, + academic_year: AcademicYear.pending + } end + ) + PatientTeamUpdater.call + Patient.all + end - it "does perform a search on the patient" do - described_class.perform_now + it "searches for 1/28th of the elligible patients" do + perform - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ) - end - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with(match([anything, anything])) end - context "with many patients" do - # Ignore patient from previous tests - let(:patient) {} - # let(:last_searched_at) { 28.days.ago } - let!(:patients) do - # Importing patients this way is significantly faster than creating - # them with create / create_list. Not using a factory could end up - # with broken tests, but since the expectations further down expect to - # find at least 2 patients, I think it'll reliably stop working if the - # underlying structure changes in the future so we won't end up with a - # false-positive test. - - Patient.import(build_list(:patient, 50, team:, school:, session:)) - PatientLocation.import( - Patient.all.map do + context "when most of the patients have had a search done recently" do + before do + PatientProgrammeVaccinationsSearch.import( + (Patient.all - patients_with_older_searches).map do |patient| + { + patient_id: patient.id, + programme_type: flu.type, + last_searched_at: 10.days.ago + } + end + ) + + PatientProgrammeVaccinationsSearch.import( + patients_with_older_searches.map do |patient| { - patient_id: it.id, - location_id: location.id, - academic_year: AcademicYear.pending + patient_id: patient.id, + programme_type: flu.type, + last_searched_at: 30.days.ago } end ) - PatientTeamUpdater.call - Patient.all end + let(:patients_with_older_searches) { patients.sample(5) } + it "searches for 1/28th of the elligible patients" do - described_class.perform_now + # We're protecting against a regression here where we search for + # 1/28th of the _remaining_ searchable patients, rather than 1/28th + # of all the patients elligible for searching. If we do the former, + # then we'll search for less and less patients over time. + perform expect(SearchVaccinationRecordsInNHSJob).to have_received( :perform_bulk - ).once.with(match([anything, anything])) - end - - context "when most of the patients have had a search done recently" do - before do - PatientProgrammeVaccinationsSearch.import( - (Patient.all - patients_with_older_searches).map do |patient| - { - patient_id: patient.id, - programme_type: flu.type, - last_searched_at: 10.days.ago - } - end - ) - - PatientProgrammeVaccinationsSearch.import( - patients_with_older_searches.map do |patient| - { - patient_id: patient.id, - programme_type: flu.type, - last_searched_at: 30.days.ago - } - end - ) + ).once do |args| + expect(args.length).to eq(2) + expect(patients_with_older_searches.map(&:id).zip).to include(*args) end - let(:patients_with_older_searches) { patients.sample(5) } - - it "searches for 1/28th of the elligible patients" do - # We're protecting against a regression here where we search for - # 1/28th of the _remaining_ searchable patients, rather than 1/28th - # of all the patients elligible for searching. If we do the former, - # then we'll search for less and less patients over time. - described_class.perform_now - - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once do |args| - expect(args.length).to eq(2) - expect(patients_with_older_searches.map(&:id).zip).to include( - *args - ) - end - - # .once.with(patients_with_older_searches.first(2).map(&:id).zip) - end + # .once.with(patients_with_older_searches.first(2).map(&:id).zip) end + end - context "with patients with no searches or older searches" do - before do - patients = - Patient.where.not( - id: [patient_with_no_searches.id, patient_with_old_searches.id] - ) - PatientProgrammeVaccinationsSearch.import( - patients.map do |patient| - { - patient_id: patient.id, - programme_type: flu.type, - last_searched_at: 30.days.ago - } - end - ) - create( - :patient_programme_vaccinations_search, - patient: patient_with_old_searches, - programme: flu, - last_searched_at: 40.days.ago + context "with patients with no searches or older searches" do + before do + patients = + Patient.where.not( + id: [patient_with_no_searches.id, patient_with_old_searches.id] ) - end + PatientProgrammeVaccinationsSearch.import( + patients.map do |patient| + { + patient_id: patient.id, + programme_type: flu.type, + last_searched_at: 30.days.ago + } + end + ) + create( + :patient_programme_vaccinations_search, + patient: patient_with_old_searches, + programme: flu, + last_searched_at: 40.days.ago + ) + end - let(:patient_with_no_searches) { patients.first } - let(:patient_with_old_searches) { patients.second } + let(:patient_with_no_searches) { patients.first } + let(:patient_with_old_searches) { patients.second } - it "prioritises patients with no searches, then older searches" do - described_class.perform_now + it "prioritises patients with no searches, then older searches" do + perform - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once.with( - [[patient_with_no_searches.id], [patient_with_old_searches.id]] - ) - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with( + [[patient_with_no_searches.id], [patient_with_old_searches.id]] + ) end end end + end - describe "when a patient shows up on both session and rolling searches" do - before do - Flipper.enable(:imms_api_enqueue_session_searches) - Flipper.enable(:imms_api_enqueue_rolling_searches) + describe "when a patient shows up on both session and rolling searches" do + before do + Flipper.enable(:imms_api_enqueue_session_searches) + Flipper.enable(:imms_api_enqueue_rolling_searches) - allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) + allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) - patient - end + patient + end - let(:school) do - create(:gias_school, team:, programmes:, gias_year_groups:) - end - let(:session) do - create( - :session, - programmes:, - academic_year: AcademicYear.pending, - dates: [7.days.from_now], - send_consent_requests_at: 7.days.ago, - days_before_consent_reminders: 7, - team:, - location: school - ) - end - let(:patient) do - create( - :patient, - team:, - school:, - session:, - patient_programme_vaccinations_searches: [ - PatientProgrammeVaccinationsSearch.new( - programme: flu, - last_searched_at: 28.days.ago - ) - ] - ) - end + let(:school) { create(:gias_school, team:, programmes:, gias_year_groups:) } + let(:session) do + create( + :session, + programmes:, + academic_year: AcademicYear.pending, + dates: [7.days.from_now], + send_consent_requests_at: 7.days.ago, + days_before_consent_reminders: 7, + team:, + location: school + ) + end + let(:patient) do + create( + :patient, + team:, + school:, + session:, + patient_programme_vaccinations_searches: [ + PatientProgrammeVaccinationsSearch.new( + programme: flu, + last_searched_at: 28.days.ago + ) + ] + ) + end - it "only performs one search for the patient" do - described_class.perform_now + it "only performs one search for the patient" do + perform - expect(SearchVaccinationRecordsInNHSJob).to have_received( - :perform_bulk - ).once.with([[patient.id]]) - end + expect(SearchVaccinationRecordsInNHSJob).to have_received( + :perform_bulk + ).once.with([[patient.id]]) end end end diff --git a/spec/jobs/gias_import_job_spec.rb b/spec/jobs/gias_import_job_spec.rb index 6307e60414..3e9eef8eba 100644 --- a/spec/jobs/gias_import_job_spec.rb +++ b/spec/jobs/gias_import_job_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe GIASImportJob do - subject(:perform_now) { described_class.perform_now(dry_run:) } + subject(:perform) { described_class.new.perform } before do allow(GIAS).to receive(:download) @@ -10,29 +10,12 @@ allow(GIAS).to receive(:import) end - context "when a dry run" do - let(:dry_run) { true } + it "runs the import" do + expect(GIAS).to receive(:download) + expect(GIAS).to receive(:check_import) + expect(GIAS).to receive(:log_import_check_results) + expect(GIAS).to receive(:import) - it "doesn't import" do - expect(GIAS).to receive(:download) - expect(GIAS).to receive(:check_import) - expect(GIAS).to receive(:log_import_check_results) - expect(GIAS).not_to receive(:import) - - perform_now - end - end - - context "when not a dry run" do - let(:dry_run) { false } - - it "does import" do - expect(GIAS).to receive(:download) - expect(GIAS).to receive(:check_import) - expect(GIAS).to receive(:log_import_check_results) - expect(GIAS).to receive(:import) - - perform_now - end + perform end end diff --git a/spec/jobs/important_notice_generator_job_spec.rb b/spec/jobs/important_notice_generator_job_spec.rb index e44a73b971..f0f33288ed 100644 --- a/spec/jobs/important_notice_generator_job_spec.rb +++ b/spec/jobs/important_notice_generator_job_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe ImportantNoticeGeneratorJob do - include ActiveJob::TestHelper + subject(:perform) { described_class.new.perform([patient.id]) } let(:team_a) { create(:team) } let(:team_b) { create(:team) } @@ -15,259 +15,251 @@ create(:patient_location, patient:, session: session_b) end - describe "#perform" do - subject(:perform) { described_class.new.perform([patient.id]) } + context "when patient exists in multiple teams" do + context "deceased" do + before do + patient.update_columns( + # if we use update! the jobs gets called automatically + date_of_death: Time.current, + date_of_death_recorded_at: Time.current + ) + end - context "when patient exists in multiple teams" do - context "deceased" do - before do - patient.update_columns( - # if we use update! the jobs gets called automatically - date_of_death: Time.current, - date_of_death_recorded_at: Time.current - ) - end + it "creates deceased notices for all associated teams" do + expect { perform }.to change { team_a.important_notices.count }.by( + 1 + ).and change { team_b.important_notices.count }.by(1) - it "creates deceased notices for all associated teams" do - expect { perform }.to change { team_a.important_notices.count }.by( - 1 - ).and change { team_b.important_notices.count }.by(1) + expect(team_a.important_notices.first.type).to eq("deceased") + expect(team_a.important_notices.first.message).to eq( + "Record updated with child’s date of death" + ) - expect(team_a.important_notices.first.type).to eq("deceased") - expect(team_a.important_notices.first.message).to eq( - "Record updated with child’s date of death" - ) - - expect { described_class.new.perform([patient.id]) }.to not_change( - team_a.important_notices, - :count - ).and not_change(team_b.important_notices, :count) - end + expect { described_class.new.perform([patient.id]) }.to not_change( + team_a.important_notices, + :count + ).and not_change(team_b.important_notices, :count) end + end - context "restricted" do - before { patient.update_column(:restricted_at, Time.current) } + context "restricted" do + before { patient.update_column(:restricted_at, Time.current) } - it "creates restricted notices for all associated teams" do - expect { perform }.to change(ImportantNotice, :count).by(2) + it "creates restricted notices for all associated teams" do + expect { perform }.to change(ImportantNotice, :count).by(2) - notices = patient.important_notices.where(type: :restricted) - expect(notices.pluck(:team_id)).to contain_exactly( - team_a.id, - team_b.id - ) - expect(notices.first.can_dismiss?).to be true - expect(notices.first.message).to eq("Record flagged as sensitive") - end + notices = patient.important_notices.where(type: :restricted) + expect(notices.pluck(:team_id)).to contain_exactly(team_a.id, team_b.id) + expect(notices.first.can_dismiss?).to be true + expect(notices.first.message).to eq("Record flagged as sensitive") + end - context "patient is no longer restricted" do - before do - patient.update!(restricted_at: Time.current) - perform_enqueued_jobs - patient.update_column(:restricted_at, nil) - end - - it "dismisses existing restricted notices" do - expect { perform }.to change { - ImportantNotice - .active(team: team_a) - .where(patient:, type: :restricted) - .count - }.from(1).to(0) - end + context "patient is no longer restricted" do + before do + patient.update!(restricted_at: Time.current) + ImportantNoticeGeneratorSidekiqJob.drain + patient.update_column(:restricted_at, nil) end - context "when a restricted notice was dismissed and the patient becomes restricted again" do - before do - # Create initial notices - patient.update_column(:restricted_at, Time.current) - described_class.perform_now([patient.id]) - - # Clear restriction and dismiss notices - patient.update_column(:restricted_at, nil) - described_class.perform_now([patient.id]) - - # Re-apply restriction - patient.update_column(:restricted_at, Time.current) - end - - it "creates a new active restricted notice (dismissed notices do not block re-creation)" do - expect { described_class.perform_now([patient.id]) }.to change { - ImportantNotice - .active(team: team_a) - .where(patient:, type: :restricted) - .count - }.from(0).to(1) - - expect( - ImportantNotice - .dismissed(team: team_a) - .where(patient:, type: :restricted) - .count - ).to be >= 1 - end + it "dismisses existing restricted notices" do + expect { perform }.to change { + ImportantNotice + .active(team: team_a) + .where(patient:, type: :restricted) + .count + }.from(1).to(0) end end - context "invalidated" do - before { patient.update_column(:invalidated_at, Time.current) } + context "when a restricted notice was dismissed and the patient becomes restricted again" do + before do + # Create initial notices + patient.update_column(:restricted_at, Time.current) + described_class.perform_now([patient.id]) - it "creates invalidated notices for all associated teams" do - expect { perform }.to change(ImportantNotice, :count).by(2) + # Clear restriction and dismiss notices + patient.update_column(:restricted_at, nil) + described_class.perform_now([patient.id]) - notices = patient.important_notices.where(type: :invalidated) - expect(notices.pluck(:team_id)).to contain_exactly( - team_a.id, - team_b.id - ) - expect(notices.first.can_dismiss?).to be false - expect(notices.first.message).to eq("Record flagged as invalid") + # Re-apply restriction + patient.update_column(:restricted_at, Time.current) end - context "patient is no longer invalidated" do - before do - patient.update!(invalidated_at: Time.current) - perform_enqueued_jobs - patient.update_column(:invalidated_at, nil) - end - - it "dismisses existing invalidated notices" do - expect { perform }.to change { - ImportantNotice - .active(team: team_a) - .where(patient:, type: :invalidated) - .count - }.from(1).to(0) - end - end - end + it "creates a new active restricted notice (dismissed notices do not block re-creation)" do + expect { described_class.perform_now([patient.id]) }.to change { + ImportantNotice + .active(team: team_a) + .where(patient:, type: :restricted) + .count + }.from(0).to(1) - context "gillick_no_notify" do - it "creates notice only for vaccination team" do - expect { - create( - :vaccination_record, - patient: patient, - team: team_a, - notify_parents: false, - programme: programmes.first, - session: session_a - ) - }.to have_enqueued_job(described_class).with([patient.id]) - - perform_enqueued_jobs - - expect(team_a.important_notices.count).to eq(1) - expect(team_b.important_notices.count).to eq(0) - - expect(team_a.important_notices.first.type).to eq("gillick_no_notify") - expect(team_a.important_notices.first.can_dismiss?).to be true + expect( + ImportantNotice + .dismissed(team: team_a) + .where(patient:, type: :restricted) + .count + ).to be >= 1 end end + end - context "team_changed" do - let(:new_school) { create(:gias_school, team: team_b) } - let(:team_changed_patient) { create(:patient, school: new_school) } - let(:school_move_log_entry) do - create( - :school_move_log_entry, - patient: team_changed_patient, - school: new_school - ) - end + context "invalidated" do + before { patient.update_column(:invalidated_at, Time.current) } + + it "creates invalidated notices for all associated teams" do + expect { perform }.to change(ImportantNotice, :count).by(2) + notices = patient.important_notices.where(type: :invalidated) + expect(notices.pluck(:team_id)).to contain_exactly(team_a.id, team_b.id) + expect(notices.first.can_dismiss?).to be false + expect(notices.first.message).to eq("Record flagged as invalid") + end + + context "patient is no longer invalidated" do before do - create( - :patient_location, - patient: team_changed_patient, - session: session_b - ) + patient.update!(invalidated_at: Time.current) + ImportantNoticeGeneratorSidekiqJob.drain + patient.update_column(:invalidated_at, nil) end - it "dismisses team_changed notice when patient's school is now in the team with the notice" do - create( - :important_notice, - :team_changed, - patient: team_changed_patient, - team: team_b, - school_move_log_entry: - ) - - expect { - described_class.perform_now([team_changed_patient.id]) - }.to change { + it "dismisses existing invalidated notices" do + expect { perform }.to change { ImportantNotice - .active(team: team_b) - .team_changed - .where(patient: team_changed_patient) + .active(team: team_a) + .where(patient:, type: :invalidated) .count }.from(1).to(0) end + end + end - it "dismisses team_changed notice when patient has no school but has patient_location in the team" do - patient_no_school = create(:patient, school: nil, team: team_a) + context "gillick_no_notify" do + it "creates notice only for vaccination team" do + expect { create( - :patient_location, - patient: patient_no_school, + :vaccination_record, + patient: patient, + team: team_a, + notify_parents: false, + programme: programmes.first, session: session_a ) + }.to enqueue_sidekiq_job(ImportantNoticeGeneratorSidekiqJob).with( + [patient.id] + ) + + ImportantNoticeGeneratorSidekiqJob.drain + + expect(team_a.important_notices.count).to eq(1) + expect(team_b.important_notices.count).to eq(0) + + expect(team_a.important_notices.first.type).to eq("gillick_no_notify") + expect(team_a.important_notices.first.can_dismiss?).to be true + end + end + + context "team_changed" do + let(:new_school) { create(:gias_school, team: team_b) } + let(:team_changed_patient) { create(:patient, school: new_school) } + let(:school_move_log_entry) do + create( + :school_move_log_entry, + patient: team_changed_patient, + school: new_school + ) + end + + before do + create( + :patient_location, + patient: team_changed_patient, + session: session_b + ) + end - school_move_log_entry_a = - create( - :school_move_log_entry, - :unknown_school, - patient: patient_no_school, - team: team_b - ) + it "dismisses team_changed notice when patient's school is now in the team with the notice" do + create( + :important_notice, + :team_changed, + patient: team_changed_patient, + team: team_b, + school_move_log_entry: + ) + + expect { + described_class.perform_now([team_changed_patient.id]) + }.to change { + ImportantNotice + .active(team: team_b) + .team_changed + .where(patient: team_changed_patient) + .count + }.from(1).to(0) + end + + it "dismisses team_changed notice when patient has no school but has patient_location in the team" do + patient_no_school = create(:patient, school: nil, team: team_a) + create( + :patient_location, + patient: patient_no_school, + session: session_a + ) + school_move_log_entry_a = create( - :important_notice, - :team_changed, + :school_move_log_entry, + :unknown_school, patient: patient_no_school, - team: team_a, - school_move_log_entry: school_move_log_entry_a + team: team_b ) - expect { - described_class.perform_now([patient_no_school.id]) - }.to change { - ImportantNotice - .active(team: team_a) - .team_changed - .where(patient: patient_no_school) - .count - }.from(1).to(0) - end - - it "does not dismiss team_changed notice when patient has no school and no patient_location in the team" do - patient_no_association = create(:patient, school: nil) + create( + :important_notice, + :team_changed, + patient: patient_no_school, + team: team_a, + school_move_log_entry: school_move_log_entry_a + ) + + expect { + described_class.perform_now([patient_no_school.id]) + }.to change { + ImportantNotice + .active(team: team_a) + .team_changed + .where(patient: patient_no_school) + .count + }.from(1).to(0) + end - school_move_log_entry_a = - create( - :school_move_log_entry, - :home_educated, - patient: patient_no_association, - team: team_b - ) + it "does not dismiss team_changed notice when patient has no school and no patient_location in the team" do + patient_no_association = create(:patient, school: nil) + school_move_log_entry_a = create( - :important_notice, - :team_changed, + :school_move_log_entry, + :home_educated, patient: patient_no_association, - team: team_a, - school_move_log_entry: school_move_log_entry_a + team: team_b ) - important_notices = - ImportantNotice - .active(team: team_a) - .team_changed - .where(patient: patient_no_association) - - expect { - described_class.perform_now([patient_no_association.id]) - }.not_to change(important_notices, :count) - end + create( + :important_notice, + :team_changed, + patient: patient_no_association, + team: team_a, + school_move_log_entry: school_move_log_entry_a + ) + + important_notices = + ImportantNotice + .active(team: team_a) + .team_changed + .where(patient: patient_no_association) + + expect { + described_class.perform_now([patient_no_association.id]) + }.not_to change(important_notices, :count) end end end diff --git a/spec/jobs/invalidate_self_consents_job_spec.rb b/spec/jobs/invalidate_self_consents_job_spec.rb index 2e960cbf5c..558ab5233e 100644 --- a/spec/jobs/invalidate_self_consents_job_spec.rb +++ b/spec/jobs/invalidate_self_consents_job_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe InvalidateSelfConsentsJob do - subject(:perform_now) { described_class.perform_now } + subject(:perform) { described_class.new.perform } let(:academic_year) { AcademicYear.current } let(:programme) { Programme.hpv } @@ -16,7 +16,7 @@ before { create(:patient_programme_status, :due, patient:, programme:) } it "does not invalidate the consent" do - expect { perform_now }.not_to(change { consent.reload.invalidated? }) + expect { perform }.not_to(change { consent.reload.invalidated? }) end context "with triage" do @@ -33,7 +33,7 @@ end it "does not invalidate the triage" do - expect { perform_now }.not_to(change { triage.reload.invalidated? }) + expect { perform }.not_to(change { triage.reload.invalidated? }) end end end @@ -44,7 +44,7 @@ before { create(:patient_programme_status, :due, patient:, programme:) } it "does not invalidate the consent" do - expect { perform_now }.not_to(change { consent.reload.invalidated? }) + expect { perform }.not_to(change { consent.reload.invalidated? }) end context "with triage" do @@ -60,7 +60,7 @@ end it "does not invalidate the triage" do - expect { perform_now }.not_to(change { triage.reload.invalidated? }) + expect { perform }.not_to(change { triage.reload.invalidated? }) end end end @@ -79,7 +79,7 @@ before { create(:patient_programme_status, :due, patient:, programme:) } it "invalidates the consent" do - expect { perform_now }.to change { consent.reload.invalidated? }.from( + expect { perform }.to change { consent.reload.invalidated? }.from( false ).to(true) end @@ -98,7 +98,7 @@ end it "invalidates the triage" do - expect { perform_now }.to change { triage.reload.invalidated? }.from( + expect { perform }.to change { triage.reload.invalidated? }.from( false ).to(true) end @@ -119,7 +119,7 @@ end it "doesn't invalidate the triage" do - expect { perform_now }.not_to(change { triage.reload.invalidated? }) + expect { perform }.not_to(change { triage.reload.invalidated? }) end end @@ -138,7 +138,7 @@ end it "doesn't invalidate the triage" do - expect { perform_now }.not_to(change { triage.reload.invalidated? }) + expect { perform }.not_to(change { triage.reload.invalidated? }) end end @@ -156,7 +156,7 @@ end it "does not invalidate the consent" do - expect { perform_now }.not_to(change { consent.reload.invalidated? }) + expect { perform }.not_to(change { consent.reload.invalidated? }) end context "with triage" do @@ -173,7 +173,7 @@ end it "does not invalidate the triage" do - expect { perform_now }.not_to(change { triage.reload.invalidated? }) + expect { perform }.not_to(change { triage.reload.invalidated? }) end end end @@ -185,7 +185,7 @@ before { create(:patient_programme_status, patient:, programme:) } it "does not invalidate the consent" do - expect { perform_now }.not_to(change { consent.reload.invalidated? }) + expect { perform }.not_to(change { consent.reload.invalidated? }) end context "with triage" do @@ -201,7 +201,7 @@ end it "does not invalidate the triage" do - expect { perform_now }.not_to(change { triage.reload.invalidated? }) + expect { perform }.not_to(change { triage.reload.invalidated? }) end end end @@ -252,15 +252,13 @@ end it "does not invalidate the parent consent" do - expect { perform_now }.not_to( - change { parent_consent.reload.invalidated? } - ) + expect { perform }.not_to(change { parent_consent.reload.invalidated? }) end it "invalidates the self-consent" do - expect { perform_now }.to change { - self_consent.reload.invalidated? - }.from(false).to(true) + expect { perform }.to change { self_consent.reload.invalidated? }.from( + false + ).to(true) end end end diff --git a/spec/jobs/patient_update_from_pds_job_spec.rb b/spec/jobs/patient_update_from_pds_job_spec.rb index 1f3f523d03..4bc5984f93 100644 --- a/spec/jobs/patient_update_from_pds_job_spec.rb +++ b/spec/jobs/patient_update_from_pds_job_spec.rb @@ -62,7 +62,9 @@ end it "doesn't queue a job to look up NHS number" do - expect { perform_now }.not_to have_enqueued_job(PDSCascadingSearchJob) + expect { perform_now }.not_to enqueue_sidekiq_job( + PDSCascadingSearchSidekiqJob + ) end context "when the patient is invalidated" do @@ -120,9 +122,9 @@ end it "queues a job to look up NHS number using PDS cascading search" do - expect { perform_now }.to have_enqueued_job( - PDSCascadingSearchJob - ).with(patient) + expect { perform_now }.to enqueue_sidekiq_job( + PDSCascadingSearchSidekiqJob + ).with(patient.to_global_id.to_s, nil, nil, nil) end end @@ -152,9 +154,9 @@ end it "queues a job to look up NHS number using PDS cascading search" do - expect { perform_now }.to have_enqueued_job( - PDSCascadingSearchJob - ).with(patient) + expect { perform_now }.to enqueue_sidekiq_job( + PDSCascadingSearchSidekiqJob + ).with(patient.to_global_id.to_s, nil, nil, nil) end end @@ -184,9 +186,9 @@ end it "queues a job to look up NHS number using PDS cascading search" do - expect { perform_now }.to have_enqueued_job( - PDSCascadingSearchJob - ).with(patient) + expect { perform_now }.to enqueue_sidekiq_job( + PDSCascadingSearchSidekiqJob + ).with(patient.to_global_id.to_s, nil, nil, nil) end end end diff --git a/spec/jobs/patients_refused_consent_already_vaccinated_job_spec.rb b/spec/jobs/patients_refused_consent_already_vaccinated_job_spec.rb index c31deed4bc..b14d43ff08 100644 --- a/spec/jobs/patients_refused_consent_already_vaccinated_job_spec.rb +++ b/spec/jobs/patients_refused_consent_already_vaccinated_job_spec.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true describe PatientsRefusedConsentAlreadyVaccinatedJob do - subject(:perform_now) do + subject(:perform) do PatientStatusUpdater.call - described_class.perform_now + described_class.new.perform end around { |example| travel_to(today) { example.run } } @@ -18,7 +18,7 @@ before { create(:patient, :vaccinated, session:, year_group: 7) } it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -26,7 +26,7 @@ before { create(:patient, session:, year_group: 7) } it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -41,7 +41,7 @@ end it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -56,7 +56,7 @@ before { consent.update!(reason_for_refusal: "personal_choice") } it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -64,7 +64,7 @@ before { consent.update!(reason_for_refusal: "already_vaccinated") } it "creates a vaccination record" do - expect { perform_now }.to change(VaccinationRecord, :count) + expect { perform }.to change(VaccinationRecord, :count) vaccination_record = VaccinationRecord.last expect(vaccination_record).to be_already_had @@ -87,7 +87,7 @@ before { create(:patient, :vaccinated, session:, year_group: 7) } it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -95,7 +95,7 @@ before { create(:patient, session:, year_group: 7) } it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -110,7 +110,7 @@ end it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -125,7 +125,7 @@ before { consent.update!(reason_for_refusal: "personal_choice") } it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end @@ -133,7 +133,7 @@ before { consent.update!(reason_for_refusal: "already_vaccinated") } it "does not create a vaccination record" do - expect { perform_now }.not_to change(VaccinationRecord, :count) + expect { perform }.not_to change(VaccinationRecord, :count) end end end diff --git a/spec/jobs/pds_cascading_search_job_spec.rb b/spec/jobs/pds_cascading_search_job_spec.rb index 1c2ce7d52e..2a2a06f067 100644 --- a/spec/jobs/pds_cascading_search_job_spec.rb +++ b/spec/jobs/pds_cascading_search_job_spec.rb @@ -40,7 +40,7 @@ it "saves the search result and enqueues ProcessPatientChangesetJob" do expect { described_class.perform_now(patient_changeset) - }.to have_enqueued_job(ProcessPatientChangesetJob).with( + }.to enqueue_sidekiq_job(ProcessPatientChangesetSidekiqJob).with( patient_changeset.id ) @@ -64,10 +64,10 @@ patient_changeset, step_name: "no_fuzzy_with_history" ) - }.to have_enqueued_job(described_class).with( - patient_changeset, - step_name: "no_fuzzy_with_wildcard_postcode", - search_results: [ + }.to enqueue_sidekiq_job(PDSCascadingSearchSidekiqJob).with( + patient_changeset.to_global_id.to_s, + "no_fuzzy_with_wildcard_postcode", + [ { "step" => "no_fuzzy_with_history", "result" => "no_matches", @@ -75,7 +75,7 @@ "created_at" => Time.current.iso8601 } ], - queue: "pds" + "pds" ) end end @@ -102,7 +102,7 @@ it "records no_postcode result and enqueues ProcessPatientChangesetJob" do expect { described_class.perform_now(patient_changeset) - }.to have_enqueued_job(ProcessPatientChangesetJob).with( + }.to enqueue_sidekiq_job(ProcessPatientChangesetSidekiqJob).with( patient_changeset.id ) @@ -139,8 +139,8 @@ it "skips wildcard name steps and completes search" do described_class.perform_now(patient_changeset) - perform_enqueued_jobs_while_exists(only: described_class) - perform_enqueued_jobs(only: ProcessPatientChangesetJob) + perform_enqueued_jobs_while_exists(PDSCascadingSearchSidekiqJob) + ProcessPatientChangesetSidekiqJob.drain patient_changeset.reload @@ -178,7 +178,7 @@ step_name: "no_fuzzy_with_wildcard_given_name", search_results: patient_changeset.search_results ) - }.to have_enqueued_job(ProcessPatientChangesetJob).with( + }.to enqueue_sidekiq_job(ProcessPatientChangesetSidekiqJob).with( patient_changeset.id ) end @@ -196,7 +196,7 @@ expect { described_class.perform_now(patient_changeset) - }.to have_enqueued_job(ProcessPatientChangesetJob).with( + }.to enqueue_sidekiq_job(ProcessPatientChangesetSidekiqJob).with( patient_changeset.id ) @@ -221,7 +221,7 @@ expect { described_class.perform_now(patient_changeset) - }.to have_enqueued_job(ProcessPatientChangesetJob).with( + }.to enqueue_sidekiq_job(ProcessPatientChangesetSidekiqJob).with( patient_changeset.id ) @@ -244,10 +244,10 @@ it "enqueues next step (no_fuzzy_without_history)" do expect { described_class.perform_now(patient_changeset) - }.to have_enqueued_job(described_class).with( - patient_changeset, - step_name: "no_fuzzy_without_history", - search_results: [ + }.to enqueue_sidekiq_job(PDSCascadingSearchSidekiqJob).with( + patient_changeset.to_global_id.to_s, + "no_fuzzy_without_history", + [ { "step" => "no_fuzzy_with_history", "result" => "too_many_matches", @@ -255,7 +255,7 @@ "created_at" => Time.current.iso8601 }.with_indifferent_access ], - queue: "pds" + "pds" ) end end @@ -278,7 +278,7 @@ patient_changeset, step_name: "no_fuzzy_without_history" ) - }.to have_enqueued_job(ProcessPatientChangesetJob).with( + }.to enqueue_sidekiq_job(ProcessPatientChangesetSidekiqJob).with( patient_changeset.id ) end @@ -293,17 +293,16 @@ it "saves the search result into the provided array and enqueues PatientUpdateFromPDSJob" do expect { - described_class.perform_now(patient, search_results:) - }.to have_enqueued_job(PatientUpdateFromPDSJob).with( - patient, - search_results - ) - - expect(search_results.count).to eq(1) - expect(search_results.first).to include( - "step" => "no_fuzzy_with_history", - "result" => "one_match", - "nhs_number" => "9449306168" + described_class.new.perform(patient, search_results:) + }.to enqueue_sidekiq_job(PatientUpdateFromPDSSidekiqJob).with( + patient.id, + [ + a_hash_including( + "step" => "no_fuzzy_with_history", + "result" => "one_match", + "nhs_number" => "9449306168" + ) + ] ) end @@ -311,16 +310,21 @@ allow(PDS::Patient).to receive(:search).and_return(nil) expect { - described_class.perform_now( + described_class.new.perform( patient, step_name: "no_fuzzy_with_history", - search_results: search_results + search_results: ) - }.to have_enqueued_job(described_class).with( - patient, - step_name: "no_fuzzy_with_wildcard_postcode", - search_results: search_results, - queue: "pds" + }.to enqueue_sidekiq_job(PDSCascadingSearchSidekiqJob).with( + patient.to_global_id.to_s, + "no_fuzzy_with_wildcard_postcode", + [ + a_hash_including( + "step" => "no_fuzzy_with_history", + "result" => "no_matches" + ) + ], + "pds" ) end @@ -332,21 +336,27 @@ "result" => "one_match", "nhs_number" => "9435780156", "created_at" => Time.current.iso8601 - }.with_indifferent_access, + }, { "step" => "no_fuzzy_with_history", "result" => "one_match", "nhs_number" => "9435792103", "created_at" => Time.current.iso8601 - }.with_indifferent_access + } ] ) expect { - described_class.perform_now(patient, search_results:) - }.to have_enqueued_job(PatientUpdateFromPDSJob).with( - patient, - search_results + described_class.new.perform(patient, search_results:) + }.to enqueue_sidekiq_job(PatientUpdateFromPDSSidekiqJob).with( + patient.id, + search_results + + [ + a_hash_including( + "step" => "no_fuzzy_with_history", + "result" => "one_match" + ) + ] ) end @@ -358,10 +368,15 @@ expect(Sentry).to receive(:capture_exception) expect { - described_class.perform_now(patient, search_results:) - }.to have_enqueued_job(PatientUpdateFromPDSJob).with( - patient, - search_results + described_class.new.perform(patient, search_results:) + }.to enqueue_sidekiq_job(PatientUpdateFromPDSSidekiqJob).with( + patient.id, + [ + a_hash_including( + "step" => "no_fuzzy_with_history", + "result" => "error" + ) + ] ) expect(search_results.first).to include("result" => "error") diff --git a/spec/jobs/process_patient_changeset_job_spec.rb b/spec/jobs/process_patient_changeset_job_spec.rb index 252c5cf58b..c5a6a222b4 100644 --- a/spec/jobs/process_patient_changeset_job_spec.rb +++ b/spec/jobs/process_patient_changeset_job_spec.rb @@ -41,7 +41,7 @@ it "does not enqueue ReviewPatientChangesetJob" do expect { described_class.perform_now(patient_changeset.id) - }.not_to have_enqueued_job(ReviewPatientChangesetJob) + }.not_to enqueue_sidekiq_job(ReviewPatientChangesetSidekiqJob) end end @@ -201,7 +201,7 @@ it "doesn't enqueue ReviewPatientChangesetJob" do expect { described_class.perform_now(patient_changeset.id) - }.not_to have_enqueued_job(ReviewPatientChangesetJob) + }.not_to enqueue_sidekiq_job(ReviewPatientChangesetSidekiqJob) end end end @@ -240,8 +240,8 @@ context "when changesets have unique NHS numbers and unique patients" do it "enqueues ReviewPatientChangesetJob" do expect { - described_class.perform_now(patient_changeset.id) - }.to have_enqueued_job(ReviewPatientChangesetJob).with( + described_class.new.perform(patient_changeset.id) + }.to enqueue_sidekiq_job(ReviewPatientChangesetSidekiqJob).with( patient_changeset.id ) end @@ -404,7 +404,7 @@ it "enqueues ReviewPatientChangesetJob" do expect { described_class.perform_now(patient_changeset.id) - }.to have_enqueued_job(ReviewPatientChangesetJob).with( + }.to enqueue_sidekiq_job(ReviewPatientChangesetSidekiqJob).with( patient_changeset.id ) end diff --git a/spec/jobs/send_manual_school_consent_reminders_job_spec.rb b/spec/jobs/send_manual_school_consent_reminders_job_spec.rb index 088c9c1528..b80004b289 100644 --- a/spec/jobs/send_manual_school_consent_reminders_job_spec.rb +++ b/spec/jobs/send_manual_school_consent_reminders_job_spec.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true describe SendManualSchoolConsentRemindersJob do - subject(:perform_now) do + subject(:perform) do PatientStatusUpdater.call - described_class.perform_now(session, current_user: user) + described_class.new.perform(session.id, user.id) end let(:programmes) { [Programme.flu] } @@ -51,7 +51,7 @@ context "when the patient has not consented or been vaccinated" do it "creates a notification" do - expect { perform_now }.to change(ConsentNotification, :count).by(1) + expect { perform }.to change(ConsentNotification, :count).by(1) last_notification = ConsentNotification.last expect(last_notification.patient).to eq(patient) @@ -72,7 +72,7 @@ end it "does not create a notification" do - expect { perform_now }.not_to change(ConsentNotification, :count) + expect { perform }.not_to change(ConsentNotification, :count) end end @@ -87,7 +87,7 @@ end it "does not create a notification" do - expect { perform_now }.not_to change(ConsentNotification, :count) + expect { perform }.not_to change(ConsentNotification, :count) end end @@ -96,7 +96,7 @@ it "doesn't send any notifications" do expect(SessionNotification).not_to receive(:create_and_send!) - perform_now + perform end end @@ -105,7 +105,7 @@ it "doesn't send any notifications" do expect(SessionNotification).not_to receive(:create_and_send!) - perform_now + perform end end @@ -114,7 +114,7 @@ it "doesn't send any notifications" do expect(SessionNotification).not_to receive(:create_and_send!) - perform_now + perform end end @@ -123,7 +123,7 @@ it "doesn't send any notifications" do expect(SessionNotification).not_to receive(:create_and_send!) - perform_now + perform end end end diff --git a/spec/lib/already_had_notification_sender_spec.rb b/spec/lib/already_had_notification_sender_spec.rb index c1ab1225ad..9040067418 100644 --- a/spec/lib/already_had_notification_sender_spec.rb +++ b/spec/lib/already_had_notification_sender_spec.rb @@ -29,31 +29,31 @@ shared_examples "sends one email to all parents with valid consents" do it "sends email notifications to all parents with valid consents" do expect { call }.to deliver_email(:vaccination_already_had).with( - parent: first_parent, - vaccination_record:, - consent: first_consent - ).once.and deliver_email(:vaccination_already_had).with( - parent: second_parent, - vaccination_record:, - consent: second_consent - ).once + parent_id: first_parent.id, + vaccination_record_id: vaccination_record.id, + consent_id: first_consent.id + ).and deliver_email(:vaccination_already_had).with( + parent_id: second_parent.id, + vaccination_record_id: vaccination_record.id, + consent_id: second_consent.id + ) end end shared_examples "sends one SMS only to opted-in parents" do it "sends SMS notifications to parents who opted in for updates" do expect { call }.to deliver_sms(:vaccination_already_had).with( - parent: first_parent, - vaccination_record:, - consent: first_consent + parent_id: first_parent.id, + vaccination_record_id: vaccination_record.id, + consent_id: first_consent.id ) end it "doesn't send SMS notifications to parents who haven't opted in" do expect { call }.not_to deliver_sms(:vaccination_already_had).with( - parent: second_parent, - vaccination_record:, - consent: second_consent + parent_id: second_parent.id, + vaccination_record_id: vaccination_record.id, + consent_id: second_consent.id ) end end diff --git a/spec/lib/notifier/consent_form_spec.rb b/spec/lib/notifier/consent_form_spec.rb index dc5e2098fd..fbab1c50f5 100644 --- a/spec/lib/notifier/consent_form_spec.rb +++ b/spec/lib/notifier/consent_form_spec.rb @@ -15,7 +15,7 @@ expect { send_confirmation }.to deliver_email( :consent_confirmation_given - ).with(consent_form:, programme_types:, disease_types:) + ).with(consent_form_id: consent_form.id, programme_types:, disease_types:) end it "sends a consent given text" do @@ -25,7 +25,7 @@ expect { send_confirmation }.to deliver_sms( :consent_confirmation_given - ).with(consent_form:, programme_types:, disease_types:) + ).with(consent_form_id: consent_form.id, programme_types:, disease_types:) end context "when user refuses consent" do @@ -34,13 +34,13 @@ it "sends an confirmation refused email" do expect { send_confirmation }.to deliver_email( :consent_confirmation_refused - ).with(consent_form:) + ).with(consent_form_id: consent_form.id) end it "sends a consent refused text" do expect { send_confirmation }.to deliver_sms( :consent_confirmation_refused - ).with(consent_form:) + ).with(consent_form_id: consent_form.id) end end @@ -62,11 +62,11 @@ expect { send_confirmation }.to deliver_email( :consent_confirmation_given ).with( - consent_form:, + consent_form_id: consent_form.id, programme_types: [menacwy_programme.type], disease_types: menacwy_programme.disease_types ).and deliver_email(:consent_confirmation_refused).with( - consent_form:, + consent_form_id: consent_form.id, programme_types: [td_ipv_programme.type], disease_types: td_ipv_programme.disease_types ) @@ -76,11 +76,11 @@ expect { send_confirmation }.to deliver_sms( :consent_confirmation_given ).with( - consent_form:, + consent_form_id: consent_form.id, programme_types: [menacwy_programme.type], disease_types: menacwy_programme.disease_types ).and deliver_sms(:consent_confirmation_refused).with( - consent_form:, + consent_form_id: consent_form.id, programme_types: [td_ipv_programme.type], disease_types: td_ipv_programme.disease_types ) @@ -97,7 +97,11 @@ expect { send_confirmation }.to deliver_email( :consent_confirmation_triage - ).with(consent_form:, programme_types:, disease_types:) + ).with( + consent_form_id: consent_form.id, + programme_types:, + disease_types: + ) end it "doesn't send a text" do @@ -127,7 +131,11 @@ expect { send_confirmation }.to deliver_email( :consent_confirmation_clinic - ).with(consent_form:, programme_types:, disease_types:) + ).with( + consent_form_id: consent_form.id, + programme_types:, + disease_types: + ) end it "doesn't send a text" do @@ -166,7 +174,7 @@ expect { send_confirmation }.to deliver_email( :consent_confirmation_given ).with( - consent_form:, + consent_form_id: consent_form.id, programme_types: consent_form.programme_types, disease_types: %w[measles mumps rubella varicella] ) @@ -199,9 +207,15 @@ it "sends warning email and SMS to existing parent" do expect { send_unknown_contact_details_warning }.to deliver_email( :consent_unknown_contact_details_warning - ).with(parent:, patient:, consent_form:).and deliver_sms( - :consent_unknown_contact_details_warning - ).with(parent:, patient:, consent_form:) + ).with( + parent_id: parent.id, + patient_id: patient.id, + consent_form_id: consent_form.id + ).and deliver_sms(:consent_unknown_contact_details_warning).with( + parent_id: parent.id, + patient_id: patient.id, + consent_form_id: consent_form.id + ) end context "when parent has phone_receive_updates disabled" do @@ -217,7 +231,11 @@ it "sends warning email" do expect { send_unknown_contact_details_warning }.to deliver_email( :consent_unknown_contact_details_warning - ).with(parent:, patient:, consent_form:) + ).with( + parent_id: parent.id, + patient_id: patient.id, + consent_form_id: consent_form.id + ) end it "does not send warning SMS" do @@ -239,13 +257,25 @@ it "sends warnings to all existing parents" do expect { send_unknown_contact_details_warning }.to deliver_email( :consent_unknown_contact_details_warning - ).with(parent:, patient:, consent_form:).and deliver_email( - :consent_unknown_contact_details_warning - ).with(parent: parent2, patient:, consent_form:).and deliver_sms( - :consent_unknown_contact_details_warning - ).with(parent:, patient:, consent_form:).and deliver_sms( + ).with( + parent_id: parent.id, + patient_id: patient.id, + consent_form_id: consent_form.id + ).and deliver_email(:consent_unknown_contact_details_warning).with( + parent_id: parent2.id, + patient_id: patient.id, + consent_form_id: consent_form.id + ).and deliver_sms(:consent_unknown_contact_details_warning).with( + parent_id: parent.id, + patient_id: patient.id, + consent_form_id: consent_form.id + ).and deliver_sms( :consent_unknown_contact_details_warning - ).with(parent: parent2, patient:, consent_form:) + ).with( + parent_id: parent2.id, + patient_id: patient.id, + consent_form_id: consent_form.id + ) end end end diff --git a/spec/lib/notifier/consent_spec.rb b/spec/lib/notifier/consent_spec.rb index 5d67b1985f..dbb64d517c 100644 --- a/spec/lib/notifier/consent_spec.rb +++ b/spec/lib/notifier/consent_spec.rb @@ -24,7 +24,11 @@ it "sends an email saying triage was needed and vaccination will happen" do expect { send_confirmation }.to deliver_email( :triage_vaccination_will_happen - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end it "doesn't send a text message" do @@ -44,7 +48,11 @@ it "sends a different email tailored to MMR second dose" do expect { send_confirmation }.to deliver_email( :triage_vaccination_will_happen_mmr_second_dose - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end end @@ -61,7 +69,11 @@ it "sends an email saying triage was needed but vaccination won't happen" do expect { send_confirmation }.to deliver_email( :triage_vaccination_wont_happen - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end it "doesn't send a text message" do @@ -77,7 +89,11 @@ it "sends an email saying triage was needed but vaccination won't happen" do expect { send_confirmation }.to deliver_email( :triage_vaccination_at_clinic - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end it "doesn't send a text message" do @@ -91,7 +107,11 @@ it "enqueues an email using the CWPT-specific template" do expect { send_confirmation }.to deliver_email( :triage_vaccination_at_clinic_ryg - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end end @@ -102,7 +122,11 @@ it "enqueues an email using the LPT-specific template" do expect { send_confirmation }.to deliver_email( :triage_vaccination_at_clinic_rt5 - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end end end @@ -115,13 +139,21 @@ it "sends an email saying vaccination will happen" do expect { send_confirmation }.to deliver_email( :consent_confirmation_given - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end it "sends a text message" do expect { send_confirmation }.to deliver_sms( :consent_confirmation_given - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end end @@ -131,7 +163,11 @@ it "sends an email saying triage is required" do expect { send_confirmation }.to deliver_email( :consent_confirmation_triage - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end it "doesn't send a text message" do @@ -172,13 +208,21 @@ it "sends an email confirming they've refused consent" do expect { send_confirmation }.to deliver_email( :consent_confirmation_refused - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end it "sends a text message" do expect { send_confirmation }.to deliver_sms( :consent_confirmation_refused - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end end @@ -252,13 +296,21 @@ it "sends an email" do expect { send_confirmation }.to deliver_email( :consent_confirmation_given - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end it "sends a text message" do expect { send_confirmation }.to deliver_sms( :consent_confirmation_given - ).with(consent:, session:, sent_by:) + ).with( + consent_id: consent.id, + session_id: session.id, + sent_by_user_id: sent_by.id + ) end end end diff --git a/spec/lib/notifier/patient_spec.rb b/spec/lib/notifier/patient_spec.rb index f3e971b7f4..427cd13344 100644 --- a/spec/lib/notifier/patient_spec.rb +++ b/spec/lib/notifier/patient_spec.rb @@ -20,6 +20,42 @@ end let(:team_location) { session.team_location } + let(:first_parent_params) do + { + disease_types:, + parent_id: parents.first.id, + patient_id: patient.id, + programme_types:, + sent_by_user_id: sent_by.id + } + end + + let(:first_parent_params_with_session) do + first_parent_params.merge(session_id: session.id) + end + + let(:first_parent_params_with_team_location) do + first_parent_params.merge(team_location_id: team_location.id) + end + + let(:second_parent_params) do + { + disease_types:, + parent_id: parents.second.id, + patient_id: patient.id, + programme_types:, + sent_by_user_id: sent_by.id + } + end + + let(:second_parent_params_with_session) do + second_parent_params.merge(session_id: session.id) + end + + let(:second_parent_params_with_team_location) do + second_parent_params.merge(team_location_id: team_location.id) + end + context "with a session" do subject(:send_consent_request) do travel_to(today) do @@ -78,41 +114,17 @@ it "enqueues an email per parent" do expect { send_consent_request }.to deliver_email( :consent_school_request_hpv - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_email(:consent_school_request_hpv).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params_with_session).and deliver_email( + :consent_school_request_hpv + ).with(second_parent_params_with_session) end it "enqueues a text per parent" do expect { send_consent_request }.to deliver_sms( :consent_school_request - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_sms(:consent_school_request).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params_with_session).and deliver_sms( + :consent_school_request + ).with(second_parent_params_with_session) end context "when parent doesn't want to receive updates by text" do @@ -123,14 +135,7 @@ it "still enqueues a text" do expect { send_consent_request }.to deliver_sms( :consent_school_request - ).with( - disease_types:, - parent:, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params_with_session) end end @@ -281,41 +286,17 @@ it "enqueues an email per parent" do expect { send_consent_request }.to deliver_email( :consent_clinic_request - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_email(:consent_clinic_request).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params_with_session).and deliver_email( + :consent_clinic_request + ).with(second_parent_params_with_session) end it "enqueues a text per parent" do expect { send_consent_request }.to deliver_sms( :consent_clinic_request - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_sms(:consent_clinic_request).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params_with_session).and deliver_sms( + :consent_clinic_request + ).with(second_parent_params_with_session) end context "when parent doesn't want to receive updates by text" do @@ -326,14 +307,7 @@ it "still enqueues a text" do expect { send_consent_request }.to deliver_sms( :consent_clinic_request - ).with( - disease_types:, - parent:, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params_with_session) end end end @@ -365,41 +339,17 @@ it "enqueues an email per parent" do expect { send_consent_request }.to deliver_email( :consent_school_request_hpv - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - team_location:, - sent_by: - ).and deliver_email(:consent_school_request_hpv).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - team_location:, - sent_by: - ) + ).with(first_parent_params_with_team_location).and deliver_email( + :consent_school_request_hpv + ).with(second_parent_params_with_team_location) end it "enqueues a text per parent" do expect { send_consent_request }.to deliver_sms( :consent_school_request - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - team_location:, - sent_by: - ).and deliver_sms(:consent_school_request).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - team_location:, - sent_by: - ) + ).with(first_parent_params_with_team_location).and deliver_sms( + :consent_school_request + ).with(second_parent_params_with_team_location) end context "when parent doesn't want to receive updates by text" do @@ -410,14 +360,7 @@ it "still enqueues a text" do expect { send_consent_request }.to deliver_sms( :consent_school_request - ).with( - disease_types:, - parent:, - patient:, - programme_types:, - team_location:, - sent_by: - ) + ).with(first_parent_params_with_team_location) end end @@ -532,41 +475,17 @@ it "enqueues an email per parent" do expect { send_consent_request }.to deliver_email( :consent_clinic_request - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - team_location:, - sent_by: - ).and deliver_email(:consent_clinic_request).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - team_location:, - sent_by: - ) + ).with(first_parent_params_with_team_location).and deliver_email( + :consent_clinic_request + ).with(second_parent_params_with_team_location) end it "enqueues a text per parent" do expect { send_consent_request }.to deliver_sms( :consent_clinic_request - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - team_location:, - sent_by: - ).and deliver_sms(:consent_clinic_request).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - team_location:, - sent_by: - ) + ).with(first_parent_params_with_team_location).and deliver_sms( + :consent_clinic_request + ).with(second_parent_params_with_team_location) end context "when parent doesn't want to receive updates by text" do @@ -577,14 +496,7 @@ it "still enqueues a text" do expect { send_consent_request }.to deliver_sms( :consent_clinic_request - ).with( - disease_types:, - parent:, - patient:, - programme_types:, - team_location:, - sent_by: - ) + ).with(first_parent_params_with_team_location) end end end @@ -611,6 +523,28 @@ let(:location) { create(:gias_school, team:) } let(:session) { create(:session, location:, programmes:, team:) } + let(:first_parent_params) do + { + disease_types:, + parent_id: parents.first.id, + patient_id: patient.id, + programme_types:, + sent_by_user_id: sent_by.id, + session_id: session.id + } + end + + let(:second_parent_params) do + { + disease_types:, + parent_id: parents.second.id, + patient_id: patient.id, + programme_types:, + sent_by_user_id: sent_by.id, + session_id: session.id + } + end + context "without an initial reminder" do it "creates a record" do expect { send_consent_reminder }.to change( @@ -628,41 +562,17 @@ it "enqueues an email per parent with the correct args" do expect { send_consent_reminder }.to deliver_email( :consent_school_reminder_hpv - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_email(:consent_school_reminder_hpv).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params).and deliver_email( + :consent_school_reminder_hpv + ).with(second_parent_params) end it "enqueues a text per parent" do expect { send_consent_reminder }.to deliver_sms( :consent_school_reminder - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_sms(:consent_school_reminder).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params).and deliver_sms( + :consent_school_reminder + ).with(second_parent_params) end context "when parent doesn't want to receive updates by text" do @@ -673,14 +583,7 @@ it "still enqueues a text" do expect { send_consent_reminder }.to deliver_sms( :consent_school_reminder - ).with( - disease_types:, - parent:, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params) end end @@ -858,41 +761,17 @@ it "enqueues an email per parent" do expect { send_consent_reminder }.to deliver_email( :consent_school_reminder_hpv - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_email(:consent_school_reminder_hpv).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params).and deliver_email( + :consent_school_reminder_hpv + ).with(second_parent_params) end it "enqueues a text per parent" do expect { send_consent_reminder }.to deliver_sms( :consent_school_reminder - ).with( - disease_types:, - parent: parents.first, - patient:, - programme_types:, - session:, - sent_by: - ).and deliver_sms(:consent_school_reminder).with( - disease_types:, - parent: parents.second, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params).and deliver_sms( + :consent_school_reminder + ).with(second_parent_params) end context "when parent doesn't want to receive updates by text" do @@ -903,14 +782,7 @@ it "still enqueues a text" do expect { send_consent_reminder }.to deliver_sms( :consent_school_reminder - ).with( - disease_types:, - parent:, - patient:, - programme_types:, - session:, - sent_by: - ) + ).with(first_parent_params) end end @@ -1148,6 +1020,26 @@ around { |example| travel_to(today) { example.run } } let(:today) { Date.new(2024, 1, 1) } + let(:first_parent_params) do + { + academic_year:, + parent_id: parents.first.id, + patient_id: patient.id, + programme_types:, + sent_by_user_id: sent_by.id, + team_id: team.id + } + end + let(:second_parent_params) do + { + academic_year:, + parent_id: parents.second.id, + patient_id: patient.id, + programme_types:, + sent_by_user_id: sent_by.id, + team_id: team.id + } + end let(:parents) { create_list(:parent, 2) } let(:patient) { create(:patient, session:, parents:, year_group: 10) } @@ -1179,41 +1071,17 @@ it "enqueues an email per parent" do expect { send_clinic_invitation }.to deliver_email( :clinic_initial_invitation - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ).and deliver_email(:clinic_initial_invitation).with( - parent: parents.second, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params).and deliver_email( + :clinic_initial_invitation + ).with(second_parent_params) end it "enqueues a text per parent" do expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ).and deliver_sms(:clinic_initial_invitation).with( - parent: parents.second, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params).and deliver_sms( + :clinic_initial_invitation + ).with(second_parent_params) end context "if the child received their first dose under self-consent and doesn't want parents notified" do @@ -1248,27 +1116,13 @@ it "only sends emails for the remaining programme" do expect { send_clinic_invitation }.to deliver_email( :clinic_initial_invitation - ).with( - parent: parents.first, - patient:, - programme_types: [programmes.second.type], - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params.merge(programme_types: %w[hpv])) end it "enqueues a text per parent" do expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation - ).with( - parent: parents.first, - patient:, - programme_types: [programmes.second.type], - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params.merge(programme_types: %w[hpv])) end end @@ -1278,27 +1132,13 @@ it "enqueues an email using the CWPT-specific template" do expect { send_clinic_invitation }.to deliver_email( :clinic_initial_invitation_ryg - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params) end it "enqueues an SMS using the CWPT-specific template" do expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation_ryg - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params) end end @@ -1308,27 +1148,13 @@ it "enqueues an email using the LPT-specific template" do expect { send_clinic_invitation }.to deliver_email( :clinic_initial_invitation_rt5 - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params) end it "enqueues an SMS using the LPT-specific template" do expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation_rt5 - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params) end end @@ -1340,14 +1166,7 @@ it "still enqueues a text" do expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation - ).with( - parent:, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params) end end end @@ -1380,41 +1199,17 @@ it "enqueues an email per parent" do expect { send_clinic_invitation }.to deliver_email( :clinic_subsequent_invitation - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ).and deliver_email(:clinic_subsequent_invitation).with( - parent: parents.second, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params).and deliver_email( + :clinic_subsequent_invitation + ).with(second_parent_params) end it "enqueues a text per parent" do expect { send_clinic_invitation }.to deliver_sms( :clinic_subsequent_invitation - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ).and deliver_sms(:clinic_subsequent_invitation).with( - parent: parents.second, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params).and deliver_sms( + :clinic_subsequent_invitation + ).with(second_parent_params) end context "when parent doesn't want to receive updates by text" do @@ -1425,14 +1220,7 @@ it "still enqueues a text" do expect { send_clinic_invitation }.to deliver_sms( :clinic_subsequent_invitation - ).with( - parent:, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params) end end end @@ -1467,41 +1255,17 @@ it "enqueues an email per parent" do expect { send_clinic_invitation }.to deliver_email( :clinic_initial_invitation - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ).and deliver_email(:clinic_initial_invitation).with( - parent: parents.second, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params).and deliver_email( + :clinic_initial_invitation + ).with(second_parent_params) end it "enqueues a text per parent" do expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation - ).with( - parent: parents.first, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ).and deliver_sms(:clinic_initial_invitation).with( - parent: parents.second, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params).and deliver_sms( + :clinic_initial_invitation + ).with(second_parent_params) end context "when parent doesn't want to receive updates by text" do @@ -1512,14 +1276,7 @@ it "still enqueues a text" do expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation - ).with( - parent:, - patient:, - programme_types:, - team:, - academic_year:, - sent_by: - ) + ).with(first_parent_params) end end @@ -1546,19 +1303,9 @@ expect { send_clinic_invitation }.to deliver_email( :clinic_initial_invitation ).with( - parent: parents.first, - patient:, - programme_types: %w[hpv], - team:, - academic_year:, - sent_by: + first_parent_params.merge("programme_types" => %w[hpv]) ).and deliver_email(:clinic_initial_invitation).with( - parent: parents.second, - patient:, - programme_types: %w[hpv], - team:, - academic_year:, - sent_by: + second_parent_params.merge("programme_types" => %w[hpv]) ) end @@ -1566,19 +1313,9 @@ expect { send_clinic_invitation }.to deliver_sms( :clinic_initial_invitation ).with( - parent: parents.first, - patient:, - programme_types: %w[hpv], - team:, - academic_year:, - sent_by: + first_parent_params.merge("programme_types" => %w[hpv]) ).and deliver_sms(:clinic_initial_invitation).with( - parent: parents.second, - patient:, - programme_types: %w[hpv], - team:, - academic_year:, - sent_by: + second_parent_params.merge("programme_types" => %w[hpv]) ) end end diff --git a/spec/lib/notifier/vaccination_record_spec.rb b/spec/lib/notifier/vaccination_record_spec.rb index 84eab18dea..974e716f98 100644 --- a/spec/lib/notifier/vaccination_record_spec.rb +++ b/spec/lib/notifier/vaccination_record_spec.rb @@ -27,13 +27,21 @@ it "sends an email" do expect { send_confirmation }.to deliver_email( :vaccination_administered - ).with(parent:, vaccination_record:, sent_by:) + ).with( + parent_id: parent.id, + vaccination_record_id: vaccination_record.id, + sent_by_user_id: sent_by.id + ) end it "sends a text message" do expect { send_confirmation }.to deliver_sms( :vaccination_administered - ).with(parent:, vaccination_record:, sent_by:) + ).with( + parent_id: parent.id, + vaccination_record_id: vaccination_record.id, + sent_by_user_id: sent_by.id + ) end end @@ -53,13 +61,21 @@ it "sends an email" do expect { send_confirmation }.to deliver_email( :vaccination_not_administered - ).with(parent:, vaccination_record:, sent_by:) + ).with( + parent_id: parent.id, + vaccination_record_id: vaccination_record.id, + sent_by_user_id: sent_by.id + ) end it "sends a text message" do expect { send_confirmation }.to deliver_sms( :vaccination_not_administered - ).with(parent:, vaccination_record:, sent_by:) + ).with( + parent_id: parent.id, + vaccination_record_id: vaccination_record.id, + sent_by_user_id: sent_by.id + ) end end @@ -91,13 +107,21 @@ it "sends an email" do expect { send_confirmation }.to deliver_email( :vaccination_administered - ).with(parent:, vaccination_record:, sent_by:) + ).with( + parent_id: parent.id, + vaccination_record_id: vaccination_record.id, + sent_by_user_id: sent_by.id + ) end it "sends a text message" do expect { send_confirmation }.to deliver_sms( :vaccination_administered - ).with(parent:, vaccination_record:, sent_by:) + ).with( + parent_id: parent.id, + vaccination_record_id: vaccination_record.id, + sent_by_user_id: sent_by.id + ) end end diff --git a/spec/lib/update_patients_from_pds_spec.rb b/spec/lib/update_patients_from_pds_spec.rb index 827a5e39c9..0945c7d5a9 100644 --- a/spec/lib/update_patients_from_pds_spec.rb +++ b/spec/lib/update_patients_from_pds_spec.rb @@ -12,14 +12,14 @@ end it "queues no jobs" do - expect { call }.not_to have_enqueued_job + expect { call }.not_to enqueue_sidekiq_job end context "when feature is enabled but not main switch" do before { Flipper.enable(:pds_enqueue_bulk_updates) } it "queues no jobs" do - expect { call }.not_to have_enqueued_job + expect { call }.not_to enqueue_sidekiq_job end end @@ -27,7 +27,7 @@ before { Flipper.enable(:pds) } it "queues no jobs" do - expect { call }.not_to have_enqueued_job + expect { call }.not_to enqueue_sidekiq_job end end @@ -38,15 +38,15 @@ end it "queues PDSCascadingSearchJob for patients without an NHS number" do - expect { call }.to have_enqueued_job(PDSCascadingSearchJob) - .on_queue(:pds) + expect { call }.to enqueue_sidekiq_job(PDSCascadingSearchSidekiqJob) + .on("pds") .exactly(2) .times end it "queues a job for each patient with an NHS number" do - expect { call }.to have_enqueued_job(PatientUpdateFromPDSJob) - .on_queue(:pds) + expect { call }.to enqueue_sidekiq_job(PatientUpdateFromPDSSidekiqJob) + .on("pds") .exactly(2) .times end diff --git a/spec/models/class_import_spec.rb b/spec/models/class_import_spec.rb index b5675d6b96..97226f6a41 100644 --- a/spec/models/class_import_spec.rb +++ b/spec/models/class_import_spec.rb @@ -171,13 +171,13 @@ describe "#process!" do let(:file) { "valid.csv" } - let(:configured_job) { instance_double(ActiveJob::ConfiguredJob) } + let(:process_job) { double } before do - allow(PDSCascadingSearchJob).to receive(:set).with( + allow(PDSCascadingSearchSidekiqJob).to receive(:set).with( queue: :imports - ).and_return(configured_job) - allow(configured_job).to receive(:perform_later) + ).and_return(process_job) + allow(process_job).to receive(:perform_async) class_import.parse_rows! end @@ -191,7 +191,7 @@ it "enqueues PDSCascadingSearchJob for each changeset with a postcode" do class_import.process! - expect(configured_job).to have_received(:perform_later).exactly(3).times + expect(process_job).to have_received(:perform_async).exactly(3).times without_postcode = PatientChangeset.select { it.given_name == "Gae" }.sole @@ -206,11 +206,11 @@ before { Flipper.disable(:pds_search_during_import) } it "enqueues ReviewPatientChangesetJob for each changeset" do - expect { class_import.process! }.to have_enqueued_job( - ReviewPatientChangesetJob + expect { class_import.process! }.to enqueue_sidekiq_job( + ReviewPatientChangesetSidekiqJob ).exactly(4).times - expect(configured_job).not_to have_received(:perform_later) + expect(process_job).not_to have_received(:perform_async) end end end diff --git a/spec/models/cohort_import_spec.rb b/spec/models/cohort_import_spec.rb index 2efef1ab70..eed613c749 100644 --- a/spec/models/cohort_import_spec.rb +++ b/spec/models/cohort_import_spec.rb @@ -171,14 +171,14 @@ end describe "#process!" do - let(:configured_job) { instance_double(ActiveJob::ConfiguredJob) } let(:file) { "valid.csv" } + let(:process_job) { double } before do - allow(PDSCascadingSearchJob).to receive(:set).with( + allow(PDSCascadingSearchSidekiqJob).to receive(:set).with( queue: :imports - ).and_return(configured_job) - allow(configured_job).to receive(:perform_later) + ).and_return(process_job) + allow(process_job).to receive(:perform_async) cohort_import.parse_rows! end @@ -192,7 +192,7 @@ it "enqueues PDSCascadingSearchJob for each changeset" do cohort_import.process! - expect(configured_job).to have_received(:perform_later).exactly(3).times + expect(process_job).to have_received(:perform_async).exactly(3).times end end @@ -200,8 +200,8 @@ before { Flipper.disable(:pds_search_during_import) } it "enqueues ReviewPatientChangesetJob for each changeset" do - expect { cohort_import.process! }.to have_enqueued_job( - ReviewPatientChangesetJob + expect { cohort_import.process! }.to enqueue_sidekiq_job( + ReviewPatientChangesetSidekiqJob ).exactly(3).times end end diff --git a/spec/models/immunisation_import_spec.rb b/spec/models/immunisation_import_spec.rb index e9644acba1..433eff5504 100644 --- a/spec/models/immunisation_import_spec.rb +++ b/spec/models/immunisation_import_spec.rb @@ -285,15 +285,15 @@ end it "enqueues jobs to look up missing NHS numbers" do - expect { immunisation_import.process! }.to have_enqueued_job( - PDSCascadingSearchJob - ).once.on_queue(:imports) + expect { immunisation_import.process! }.to enqueue_sidekiq_job( + PDSCascadingSearchSidekiqJob + ).once.on("imports") end it "enqueues jobs to update from PDS" do - expect { immunisation_import.process! }.to have_enqueued_job( - PatientUpdateFromPDSJob - ).exactly(10).times.on_queue(:imports) + expect { immunisation_import.process! }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).exactly(10).times.on("imports") end end @@ -336,15 +336,15 @@ end it "enqueues jobs to look up missing NHS numbers" do - expect { immunisation_import.process! }.to have_enqueued_job( - PDSCascadingSearchJob - ).once.on_queue(:imports) + expect { immunisation_import.process! }.to enqueue_sidekiq_job( + PDSCascadingSearchSidekiqJob + ).once.on("imports") end it "enqueues jobs to update from PDS" do - expect { immunisation_import.process! }.to have_enqueued_job( - PatientUpdateFromPDSJob - ).exactly(9).times.on_queue(:imports) + expect { immunisation_import.process! }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).exactly(9).times.on("imports") end end @@ -387,15 +387,15 @@ end it "enqueues jobs to look up missing NHS numbers" do - expect { immunisation_import.process! }.to have_enqueued_job( - PDSCascadingSearchJob - ).once.on_queue(:imports) + expect { immunisation_import.process! }.to enqueue_sidekiq_job( + PDSCascadingSearchSidekiqJob + ).once.on("imports") end it "enqueues jobs to update from PDS" do - expect { immunisation_import.process! }.to have_enqueued_job( - PatientUpdateFromPDSJob - ).exactly(9).times.on_queue(:imports) + expect { immunisation_import.process! }.to enqueue_sidekiq_job( + PatientUpdateFromPDSSidekiqJob + ).exactly(9).times.on("imports") end end diff --git a/spec/models/session_notification_spec.rb b/spec/models/session_notification_spec.rb index a176752f57..cede90af55 100644 --- a/spec/models/session_notification_spec.rb +++ b/spec/models/session_notification_spec.rb @@ -51,6 +51,15 @@ let(:session_date) { session.dates.min } let(:current_user) { create(:user) } + let(:delivery_params) do + { + parent_id: parent.id, + patient_id: patient.id, + session_id: session.id, + sent_by_user_id: current_user.id + } + end + context "with a school reminder" do let(:type) { :school_reminder } @@ -74,25 +83,13 @@ it "enqueues an email per parent who gave consent" do expect { create_and_send! }.to deliver_email( :session_school_reminder - ).with( - parent:, - patient:, - programme_types:, - session:, - sent_by: current_user - ) + ).with(delivery_params.merge(programme_types:)) end it "enqueues a text per parent" do expect { create_and_send! }.to deliver_sms( :session_school_reminder - ).with( - parent:, - patient:, - programme_types:, - session:, - sent_by: current_user - ) + ).with(delivery_params.merge(programme_types:)) end context "when parent doesn't want to receive updates by text" do @@ -113,11 +110,9 @@ expect { create_and_send! }.to deliver_email( :session_school_reminder ).with( - parent:, - patient:, - programme_types: consented_programmes.map(&:type), - session:, - sent_by: current_user + delivery_params.merge( + programme_types: consented_programmes.map(&:type) + ) ) end @@ -125,11 +120,9 @@ expect { create_and_send! }.to deliver_sms( :session_school_reminder ).with( - parent:, - patient:, - programme_types: consented_programmes.map(&:type), - session:, - sent_by: current_user + delivery_params.merge( + programme_types: consented_programmes.map(&:type) + ) ) end end diff --git a/spec/requests/api/testing/refresh_reporting_spec.rb b/spec/requests/api/testing/refresh_reporting_spec.rb index 8c6b146d2a..961840bcf2 100644 --- a/spec/requests/api/testing/refresh_reporting_spec.rb +++ b/spec/requests/api/testing/refresh_reporting_spec.rb @@ -6,7 +6,7 @@ describe "GET" do context "without wait param" do it "enqueues the job and responds with accepted" do - expect { get "/api/testing/refresh-reporting" }.to have_enqueued_job( + expect { get "/api/testing/refresh-reporting" }.to enqueue_sidekiq_job( ReportingAPI::RefreshJob ) expect(response).to have_http_status(:accepted) @@ -14,12 +14,19 @@ end context "with wait=true" do - before { allow(ReportingAPI::RefreshJob).to receive(:perform_now) } + let(:job_double) { instance_double(ReportingAPI::RefreshJob) } - it "runs the job synchronously and responds with ok" do - get "/api/testing/refresh-reporting", params: { wait: "true" } - expect(ReportingAPI::RefreshJob).to have_received(:perform_now) + before do + allow(ReportingAPI::RefreshJob).to receive(:new).and_return(job_double) + allow(job_double).to receive(:perform) + end + + it "runs the refresh synchronously and responds with ok status" do + expect { + get "/api/testing/refresh-reporting", params: { wait: "true" } + }.not_to enqueue_sidekiq_job expect(response).to have_http_status(:ok) + expect(job_double).to have_received(:perform) end end end diff --git a/spec/requests/api/testing/vaccinations_search_in_nhs_spec.rb b/spec/requests/api/testing/vaccinations_search_in_nhs_spec.rb index 90ba3956ed..22a95e8513 100644 --- a/spec/requests/api/testing/vaccinations_search_in_nhs_spec.rb +++ b/spec/requests/api/testing/vaccinations_search_in_nhs_spec.rb @@ -8,22 +8,33 @@ it "enqueues the job and responds with accepted" do expect { post "/api/testing/vaccinations-search-in-nhs" - }.to have_enqueued_job(EnqueueVaccinationsSearchInNHSJob) + }.to enqueue_sidekiq_job(EnqueueVaccinationsSearchInNHSJob) expect(response).to have_http_status(:accepted) end end context "with wait=true" do + let(:job_double) { instance_double(EnqueueVaccinationsSearchInNHSJob) } + before do - allow(EnqueueVaccinationsSearchInNHSJob).to receive(:perform_now) + allow(EnqueueVaccinationsSearchInNHSJob).to receive(:new).and_return( + job_double + ) + allow(job_double).to receive(:perform) allow(Sidekiq::Queue).to receive(:new).with( "immunisations_api_search" ).and_return(instance_double(Sidekiq::Queue, size: 0)) end it "runs the job synchronously and responds with ok" do - post "/api/testing/vaccinations-search-in-nhs", params: { wait: "true" } - expect(EnqueueVaccinationsSearchInNHSJob).to have_received(:perform_now) + expect { + post "/api/testing/vaccinations-search-in-nhs", + params: { + wait: "true" + } + }.not_to enqueue_sidekiq_job + + expect(job_double).to have_received(:perform) expect(response).to have_http_status(:ok) end end diff --git a/spec/support/email_expectations.rb b/spec/support/email_expectations.rb index 795505d6b3..c813fbffe2 100644 --- a/spec/support/email_expectations.rb +++ b/spec/support/email_expectations.rb @@ -15,7 +15,7 @@ def expect_email_to(to, template, nth = :first) end def email_deliveries - perform_enqueued_jobs(only: EmailDeliveryJob) + EmailDeliverySidekiqJob.drain EmailDeliveryJob.deliveries end end diff --git a/spec/support/imports_helper.rb b/spec/support/imports_helper.rb index f7a1e31745..347bde5e15 100644 --- a/spec/support/imports_helper.rb +++ b/spec/support/imports_helper.rb @@ -17,13 +17,13 @@ def wait_for_import_to_commit(import_class) end def wait_for_import_to_complete_until_review(import_class) - perform_enqueued_jobs(only: ProcessImportJob) + ProcessImportSidekiqJob.drain if import_class != ImmunisationImport - perform_enqueued_jobs_while_exists(only: PDSCascadingSearchJob) - perform_enqueued_jobs_while_exists(only: ProcessPatientChangesetJob) - perform_enqueued_jobs_while_exists(only: ReviewPatientChangesetJob) - perform_enqueued_jobs(only: ReviewClassImportSchoolMoveJob) + perform_enqueued_jobs_while_exists(PDSCascadingSearchSidekiqJob) + perform_enqueued_jobs_while_exists(ProcessPatientChangesetSidekiqJob) + perform_enqueued_jobs_while_exists(ReviewPatientChangesetSidekiqJob) + ReviewClassImportSchoolMoveSidekiqJob.drain end click_on_most_recent_import(import_class) @@ -34,14 +34,8 @@ def click_on_most_recent_import(import_class) click_on link_text, match: :first if page.has_link?(link_text) end - def perform_enqueued_jobs_while_exists(only:) - job_class = only.name - - # rubocop:disable Style/WhileUntilModifier - while enqueued_jobs.any? { it["job_class"] == job_class } - perform_enqueued_jobs(only:) - end - # rubocop:enable Style/WhileUntilModifier + def perform_enqueued_jobs_while_exists(job_class) + job_class.drain while job_class.jobs.present? end # Process and approve an import programmatically (for job/unit specs) @@ -51,13 +45,14 @@ def process_and_approve_import(import) import.process! unless import.is_a?(ImmunisationImport) - perform_enqueued_jobs_while_exists(only: PDSCascadingSearchJob) - - perform_enqueued_jobs_while_exists(only: ProcessPatientChangesetJob) - perform_enqueued_jobs_while_exists(only: ReviewPatientChangesetJob) + perform_enqueued_jobs_while_exists(PDSCascadingSearchSidekiqJob) + perform_enqueued_jobs_while_exists(ProcessPatientChangesetSidekiqJob) + perform_enqueued_jobs_while_exists(ReviewPatientChangesetSidekiqJob) if import.is_a?(ClassImport) - perform_enqueued_jobs_while_exists(only: ReviewClassImportSchoolMoveJob) + perform_enqueued_jobs_while_exists( + ReviewClassImportSchoolMoveSidekiqJob + ) end end diff --git a/spec/support/matchers/deliver_email.rb b/spec/support/matchers/deliver_email.rb index c03adb2bb9..e512b0b8ca 100644 --- a/spec/support/matchers/deliver_email.rb +++ b/spec/support/matchers/deliver_email.rb @@ -13,21 +13,14 @@ define_method :matcher do @matcher ||= - if @params.nil? - have_enqueued_job(EmailDeliveryJob).with( - *[template_name].compact, - any_args - ) - else - have_enqueued_job(EmailDeliveryJob).with( - *[template_name].compact, - **@params - ) - end + enqueue_sidekiq_job(EmailDeliverySidekiqJob).with( + template_name&.to_s.presence || anything, + @params || anything + ) end chain :with do |params = {}| - @params = params + @params = params.stringify_keys end match do |actual| @@ -44,8 +37,8 @@ raise end - # TODO: copy the error message from the have_enqueued_job but only list jobs - # enqueued for EmailDeliveryJob + # TODO: copy the error message from the enqueue_sidekiq_job but only list + # jobs enqueued for EmailDeliveryJob failure_message { <<~MESSAGE } expected #{template_name} email to have been delivered #{@error} diff --git a/spec/support/matchers/deliver_sms.rb b/spec/support/matchers/deliver_sms.rb index a138dc3d71..5a78e8bf84 100644 --- a/spec/support/matchers/deliver_sms.rb +++ b/spec/support/matchers/deliver_sms.rb @@ -13,21 +13,14 @@ define_method :matcher do @matcher ||= - if @params.nil? - have_enqueued_job(SMSDeliveryJob).with( - *[template_name].compact, - any_args - ) - else - have_enqueued_job(SMSDeliveryJob).with( - *[template_name].compact, - **@params - ) - end + enqueue_sidekiq_job(SMSDeliverySidekiqJob).with( + template_name&.to_s.presence || anything, + @params || anything + ) end chain :with do |params = {}| - @params = params + @params = params.stringify_keys end match do |actual| @@ -44,8 +37,8 @@ raise end - # TODO: copy the error message from the have_enqueued_job but only list jobs - # enqueued for SMSDeliveryJob + # TODO: copy the error message from the enqueue_sidekiq_job but only list + # jobs enqueued for SMSDeliveryJob failure_message { <<~MESSAGE } expected #{template_name} sms to have been delivered #{@error} diff --git a/spec/support/sms_expectations.rb b/spec/support/sms_expectations.rb index ffb3c52725..55d4538ffd 100644 --- a/spec/support/sms_expectations.rb +++ b/spec/support/sms_expectations.rb @@ -15,8 +15,7 @@ def expect_sms_to(phone_number, template, nth = :first) end def sms_deliveries - perform_enqueued_jobs - + SMSDeliverySidekiqJob.drain SMSDeliveryJob.deliveries end end