Skip to content

Commit 507d1b6

Browse files
authored
Merge pull request #12147 from opf/fix/send-notifications-groups
Restore ability to suppress notifications in groups service
2 parents 3873f9a + 447151f commit 507d1b6

File tree

7 files changed

+79
-11
lines changed

7 files changed

+79
-11
lines changed

app/models/journal/notification_configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ def with_first(send_notifications)
6161
end
6262

6363
def log_warning(send_notifications)
64-
return if active == send_notifications
64+
return if active? == send_notifications
6565

6666
message = <<~MSG
67-
Ignoring setting journal notifications to '#{send_notifications}' as a parent block already set it to #{active}"
67+
Ignoring setting journal notifications to '#{send_notifications}' as a parent block already set it to #{active?}"
6868
MSG
6969
Rails.logger.debug message
7070
end

app/services/base_services/write.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def instance_class
7777
end
7878

7979
def set_attributes_params(params)
80-
params
80+
params.except(:send_notifications)
8181
end
8282
end
8383
end

app/services/groups/add_users_service.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ def persist(call)
5252
def after_perform(call)
5353
Groups::CreateInheritedRolesService
5454
.new(model, current_user: user, contract_class:)
55-
.call(user_ids: params[:ids], message: params[:message])
55+
.call(
56+
user_ids: params[:ids],
57+
message: params[:message],
58+
send_notifications: params.fetch(:send_notifications, true)
59+
)
5660

5761
call
5862
end

app/services/groups/update_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def after_perform(call)
4949
if new_user_ids.any?
5050
db_call = ::Groups::AddUsersService
5151
.new(call.result, current_user: user)
52-
.call(ids: new_user_ids)
52+
.call(ids: new_user_ids, send_notifications: params.fetch(:send_notifications, true))
5353

5454
call.add_dependent!(db_call)
5555
end

spec/factories/group_factory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
User.system.run_given do |system_user|
4343
Groups::AddUsersService
4444
.new(group, current_user: system_user)
45-
.call(ids: members.map(&:id))
45+
.call(ids: members.map(&:id), send_notifications: false)
4646
.on_failure { |call| raise call.message }
4747
end
4848
end

spec/services/groups/update_roles_service_integration_spec.rb

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
require 'spec_helper'
3030

3131
describe Groups::UpdateRolesService, 'integration', type: :model do
32-
subject(:service_call) { instance.call(member:, message:) }
32+
subject(:service_call) { instance.call(member:, message:, send_notifications:) }
3333

3434
let(:project) { create :project }
3535
let(:role) { create :role }
@@ -46,12 +46,13 @@
4646

4747
::Groups::CreateInheritedRolesService
4848
.new(group, current_user: User.system, contract_class: EmptyContract)
49-
.call(user_ids: users.map(&:id))
49+
.call(user_ids: users.map(&:id), send_notifications: false)
5050
end
5151
end
5252
let(:users) { create_list :user, 2 }
5353
let(:member) { Member.find_by(principal: group) }
5454
let(:message) { "Some message" }
55+
let(:send_notifications) { true }
5556

5657
let(:instance) do
5758
described_class.new(group, current_user:)
@@ -89,6 +90,15 @@
8990
end
9091
end
9192

93+
shared_examples_for 'sends no notification' do
94+
it 'on the updated membership' do
95+
service_call
96+
97+
expect(Notifications::GroupMemberAlteredJob)
98+
.not_to have_received(:perform_later)
99+
end
100+
end
101+
92102
context 'when adding a role' do
93103
let(:added_role) { create(:role) }
94104

@@ -113,6 +123,12 @@
113123
it_behaves_like 'sends notification' do
114124
let(:user) { users }
115125
end
126+
127+
context 'when notifications are suppressed' do
128+
let(:send_notifications) { false }
129+
130+
it_behaves_like 'sends no notification'
131+
end
116132
end
117133

118134
context 'with global membership' do
@@ -154,6 +170,12 @@
154170
it_behaves_like 'sends notification' do
155171
let(:user) { users }
156172
end
173+
174+
context 'when notifications are suppressed' do
175+
let(:send_notifications) { false }
176+
177+
it_behaves_like 'sends no notification'
178+
end
157179
end
158180

159181
context 'when removing a global role' do
@@ -180,6 +202,12 @@
180202
it_behaves_like 'sends notification' do
181203
let(:user) { users }
182204
end
205+
206+
context 'when notifications are suppressed' do
207+
let(:send_notifications) { false }
208+
209+
it_behaves_like 'sends no notification'
210+
end
183211
end
184212
end
185213

@@ -222,6 +250,12 @@
222250
it_behaves_like 'sends notification' do
223251
let(:user) { users.last }
224252
end
253+
254+
context 'when notifications are suppressed' do
255+
let(:send_notifications) { false }
256+
257+
it_behaves_like 'sends no notification'
258+
end
225259
end
226260

227261
context 'when removing a role' do
@@ -248,6 +282,12 @@
248282
it_behaves_like 'sends notification' do
249283
let(:user) { users }
250284
end
285+
286+
context 'when notifications are suppressed' do
287+
let(:send_notifications) { false }
288+
289+
it_behaves_like 'sends no notification'
290+
end
251291
end
252292

253293
context 'when removing a role but with a user having had the role before (no inherited_from)' do
@@ -290,6 +330,12 @@
290330
it_behaves_like 'sends notification' do
291331
let(:user) { users.last }
292332
end
333+
334+
context 'when notifications are suppressed' do
335+
let(:send_notifications) { false }
336+
337+
it_behaves_like 'sends no notification'
338+
end
293339
end
294340

295341
context 'when replacing roles' do
@@ -316,6 +362,12 @@
316362
it_behaves_like 'sends notification' do
317363
let(:user) { users }
318364
end
365+
366+
context 'when notifications are suppressed' do
367+
let(:send_notifications) { false }
368+
369+
it_behaves_like 'sends no notification'
370+
end
319371
end
320372

321373
context 'when replacing a role but with a user having had the replaced role before (no inherited_from)' do
@@ -358,6 +410,12 @@
358410
it_behaves_like 'sends notification' do
359411
let(:user) { users }
360412
end
413+
414+
context 'when notifications are suppressed' do
415+
let(:send_notifications) { false }
416+
417+
it_behaves_like 'sends no notification'
418+
end
361419
end
362420

363421
context 'when adding a role and the user has a role already granted by a different group' do
@@ -373,7 +431,7 @@
373431

374432
::Groups::CreateInheritedRolesService
375433
.new(group, current_user: User.system, contract_class: EmptyContract)
376-
.call(user_ids: users.map(&:id))
434+
.call(user_ids: users.map(&:id), send_notifications: false)
377435
end
378436
end
379437

@@ -399,6 +457,12 @@
399457
it_behaves_like 'sends notification' do
400458
let(:user) { users }
401459
end
460+
461+
context 'when notifications are suppressed' do
462+
let(:send_notifications) { false }
463+
464+
it_behaves_like 'sends no notification'
465+
end
402466
end
403467

404468
context 'when not allowed' do

spec/services/groups/update_service_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777
expect(add_users_service)
7878
.to have_received(:call)
79-
.with(ids: [new_group_user.user_id])
79+
.with(ids: [new_group_user.user_id], send_notifications: true)
8080
end
8181
end
8282

@@ -94,7 +94,7 @@
9494

9595
expect(add_users_service)
9696
.to have_received(:call)
97-
.with(ids: [new_group_user.user_id])
97+
.with(ids: [new_group_user.user_id], send_notifications: true)
9898
end
9999
end
100100

0 commit comments

Comments
 (0)