Commit 7e90bed5 authored by Sean Arnold's avatar Sean Arnold

Check participant limit in service

parent 8aafe8b4
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module IncidentManagement module IncidentManagement
module OncallRotations module OncallRotations
class CreateService class CreateService
MAXIMUM_PARTICIPANTS = 100
# @param schedule [IncidentManagement::OncallSchedule] # @param schedule [IncidentManagement::OncallSchedule]
# @param project [Project] # @param project [Project]
# @param current_user [User] # @param current_user [User]
...@@ -26,15 +28,19 @@ module IncidentManagement ...@@ -26,15 +28,19 @@ module IncidentManagement
return error_no_license unless available? return error_no_license unless available?
return error_no_permissions unless allowed? return error_no_permissions unless allowed?
participant_params = Array(params[:participants])
return error_too_many_participants if participant_params.size > MAXIMUM_PARTICIPANTS
oncall_rotation = schedule.rotations.create(params.except(:participants)) oncall_rotation = schedule.rotations.create(params.except(:participants))
return error_in_create(oncall_rotation) unless oncall_rotation.persisted? return error_in_create(oncall_rotation) unless oncall_rotation.persisted?
new_participants = Array(params[:participants]).map do |participant| new_participants = Array(participant_params).map do |participant|
OncallParticipant.new( OncallParticipant.new(
rotation: oncall_rotation, rotation: oncall_rotation,
user: participant[:user], user: participant[:user],
color_palette: participant[:color_palette], color_palette: participant[:color_palette],
color_weight: participant[:color_weight] color_weight: participant[:color_weight]
) )
end end
...@@ -64,6 +70,10 @@ module IncidentManagement ...@@ -64,6 +70,10 @@ module IncidentManagement
ServiceResponse.success(payload: { oncall_rotation: oncall_rotation }) ServiceResponse.success(payload: { oncall_rotation: oncall_rotation })
end end
def error_too_many_participants
error("A maximum of #{MAXIMUM_PARTICIPANTS} participants can be added")
end
def error_no_permissions def error_no_permissions
error('You have insufficient permissions to create an on-call rotation for this project') error('You have insufficient permissions to create an on-call rotation for this project')
end end
......
...@@ -74,6 +74,17 @@ RSpec.describe IncidentManagement::OncallRotations::CreateService do ...@@ -74,6 +74,17 @@ RSpec.describe IncidentManagement::OncallRotations::CreateService do
it_behaves_like 'error response', 'Name has already been taken' it_behaves_like 'error response', 'Name has already been taken'
end end
context 'when too many participants' do
before do
stub_const('IncidentManagement::OncallRotations::CreateService::MAXIMUM_PARTICIPANTS', 0)
end
it 'has an informative error message' do
expect(execute).to be_error
expect(execute.message).to eq("A maximum of #{IncidentManagement::OncallRotations::CreateService::MAXIMUM_PARTICIPANTS} participants can be added")
end
end
context 'with valid params' do context 'with valid params' do
it 'successfully creates an on-call rotation' do it 'successfully creates an on-call rotation' do
expect(execute).to be_success expect(execute).to be_success
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment