From 6d14b34bfb7783093fe430c746683fbe79b66a28 Mon Sep 17 00:00:00 2001 From: Alberto Espinoza Date: Fri, 17 Nov 2023 14:33:03 -0600 Subject: [PATCH] R2-2655 - API Disabling send push and email notifications --- app/models/assign.rb | 6 ++++++ app/models/transition.rb | 6 +++++- app/services/bulk_assign_service.rb | 3 ++- spec/jobs/bulk_assign_records_spec.rb | 24 +++++++++++++++++++++++ spec/models/assign_spec.rb | 24 +++++++++++++++++++++++ spec/services/bulk_assign_service_spec.rb | 3 ++- 6 files changed, 63 insertions(+), 3 deletions(-) diff --git a/app/models/assign.rb b/app/models/assign.rb index c5404c0e29..d5d3c8e12d 100644 --- a/app/models/assign.rb +++ b/app/models/assign.rb @@ -7,6 +7,8 @@ class Assign < Transition MAX_BULK_RECORDS = 100 + attr_accessor :from_bulk_export + def perform return if transitioned_to_user.nil? @@ -25,4 +27,8 @@ def consent_given? def notified_statuses [Transition::STATUS_DONE] end + + def should_notify? + !from_bulk_export && super + end end diff --git a/app/models/transition.rb b/app/models/transition.rb index 55c3ce8875..f748484c3e 100644 --- a/app/models/transition.rb +++ b/app/models/transition.rb @@ -92,11 +92,15 @@ def notified_statuses end def notify - return unless notified_statuses.include?(status) + return unless should_notify? TransitionNotifyJob.perform_later(id) end + def should_notify? + notified_statuses.include?(status) + end + def index_record Sunspot.index(record) if record end diff --git a/app/services/bulk_assign_service.rb b/app/services/bulk_assign_service.rb index 65f35f9764..375cadb366 100644 --- a/app/services/bulk_assign_service.rb +++ b/app/services/bulk_assign_service.rb @@ -28,7 +28,8 @@ def create_assignment(record) record:, transitioned_to: @args[:transitioned_to], transitioned_by: @transitioned_by.user_name, - notes: @args[:notes] + notes: @args[:notes], + from_bulk_export: true ) end diff --git a/spec/jobs/bulk_assign_records_spec.rb b/spec/jobs/bulk_assign_records_spec.rb index 90e63c65ad..5639f071a2 100644 --- a/spec/jobs/bulk_assign_records_spec.rb +++ b/spec/jobs/bulk_assign_records_spec.rb @@ -32,6 +32,10 @@ disclosure_other_orgs: true, module_id: PrimeroModule::CP) end + before :each do + BulkAssignService.any_instance.stub(:search_results).and_return([child]) + end + describe 'perform_later' do before do ActiveJob::Base.queue_adapter = :test @@ -53,6 +57,26 @@ end end + describe 'when job is performed' do + let(:bulk_assign_params) do + { + transitioned_to: user2.user_name, + transitioned_by: user.user_name, + notes: 'this is a note', + filters: { short_id: [child.short_id] } + } + end + + it 'should not enqueue a TransitionNotifyJob' do + ActiveJob::Base.queue_adapter = :test + BulkAssignRecordsJob.perform_now(Child, user, **bulk_assign_params) + + expect( + ActiveJob::Base.queue_adapter.enqueued_jobs.select { |j| j[:job] == TransitionNotifyJob }.size + ).to eq(0) + end + end + after :each do clean_data(User, Role, PrimeroModule, PrimeroProgram, Field, FormSection, UserGroup, Agency, Child, Transition) end diff --git a/spec/models/assign_spec.rb b/spec/models/assign_spec.rb index 3573ea704a..a00b3f0219 100644 --- a/spec/models/assign_spec.rb +++ b/spec/models/assign_spec.rb @@ -5,6 +5,8 @@ require 'rails_helper' describe Assign do + include ActiveJob::TestHelper + before do clean_data(User, Role, PrimeroModule, UserGroup, Agency, Transition, Incident, Child) @@ -150,6 +152,28 @@ end end end + + describe '.notify' do + context 'when should_notify? is true' do + it 'should enqueue a TransitionNotifyJob' do + expect( + ActiveJob::Base.queue_adapter.enqueued_jobs.select { |j| j[:job] == TransitionNotifyJob }.size + ).to eq(1) + end + end + context 'when should_notify? is false' do + before do + clear_enqueued_jobs + Assign.create!(transitioned_by: 'user1', transitioned_to: 'user2', record: @case, from_bulk_export: true) + end + + it 'should enqueue a TransitionNotifyJob' do + expect( + ActiveJob::Base.queue_adapter.enqueued_jobs.select { |j| j[:job] == TransitionNotifyJob }.size + ).to eq(0) + end + end + end end context 'and the user has permission within the user group' do diff --git a/spec/services/bulk_assign_service_spec.rb b/spec/services/bulk_assign_service_spec.rb index e60c4d6c36..559f09e7e6 100644 --- a/spec/services/bulk_assign_service_spec.rb +++ b/spec/services/bulk_assign_service_spec.rb @@ -45,7 +45,8 @@ { transitioned_to: user2.user_name, transitioned_by: user.user_name, - notes: 'this is a note' + notes: 'this is a note', + from_bulk_export: true } end