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

Check participant limit in service

parent 8aafe8b4
......@@ -3,6 +3,8 @@
module IncidentManagement
module OncallRotations
class CreateService
MAXIMUM_PARTICIPANTS = 100
# @param schedule [IncidentManagement::OncallSchedule]
# @param project [Project]
# @param current_user [User]
......@@ -26,15 +28,19 @@ module IncidentManagement
return error_no_license unless available?
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))
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(
rotation: oncall_rotation,
user: participant[:user],
color_palette: participant[:color_palette],
color_palette: participant[:color_palette],
color_weight: participant[:color_weight]
)
end
......@@ -64,6 +70,10 @@ module IncidentManagement
ServiceResponse.success(payload: { oncall_rotation: oncall_rotation })
end
def error_too_many_participants
error("A maximum of #{MAXIMUM_PARTICIPANTS} participants can be added")
end
def error_no_permissions
error('You have insufficient permissions to create an on-call rotation for this project')
end
......
......@@ -74,6 +74,17 @@ RSpec.describe IncidentManagement::OncallRotations::CreateService do
it_behaves_like 'error response', 'Name has already been taken'
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
it 'successfully creates an on-call rotation' do
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