Commit 72525fce authored by Sean Arnold's avatar Sean Arnold

Use EditService for rotation changes

Add specs for failure case
parent 4d709949
......@@ -19,12 +19,16 @@ module IncidentManagement
return error_no_license unless available?
return error_no_permissions unless allowed?
if oncall_schedule.update(params)
update_rotation_active_periods
success(oncall_schedule)
else
error(oncall_schedule.errors.full_messages.to_sentence)
oncall_schedule.update!(params)
update_rotation_result = update_rotation_active_periods
if update_rotation_result.respond_to?(:error?) && update_rotation_result.error?
return error(update_rotation_result.message)
end
success(oncall_schedule)
rescue ActiveRecord::RecordInvalid => e
error(e.record.errors.full_messages.to_sentence)
end
private
......@@ -33,14 +37,19 @@ module IncidentManagement
def update_rotation_active_periods
oncall_schedule.rotations.select(&:has_shift_active_period?).each do |rotation|
rotation.update!(
active_period_start: new_rotation_active_period(rotation.active_period_start).strftime('%H:%M'),
active_period_end: new_rotation_active_period(rotation.active_period_end).strftime('%H:%M')
service = IncidentManagement::OncallRotations::EditService.new(
rotation,
user,
{
active_period_start: new_rotation_active_period(rotation.active_period_start).strftime('%H:%M'),
active_period_end: new_rotation_active_period(rotation.active_period_end).strftime('%H:%M')
}
)
end
# TODO do we need to handle run update rotation job?
# Should the above be moved to update rotation job?
response = service.execute
break(response) if response.error?
end
end
def new_rotation_active_period(time_string)
......
......@@ -73,7 +73,7 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do
# Setting fixed timezone for rotation active period updates
around do |example|
freeze_time do
travel_to Time.utc(2021, 03, 22)
travel_to Time.utc(2021, 03, 22, 0, 0)
example.run
end
......@@ -85,6 +85,16 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do
expect { execute }.to change { time_from_time_column(oncall_rotation.reload.active_period_start) }.from('08:00').to('03:00')
.and change { time_from_time_column(oncall_rotation.active_period_end) }.from('17:00').to('12:00')
end
context 'error updating' do
before do
allow_next_instance_of(IncidentManagement::OncallRotations::EditService) do |edit_service|
allow(edit_service).to receive(:execute).and_return(double(error?: true, message: 'Test something went wrong'))
end
end
it_behaves_like 'error response', 'Test something went wrong'
end
end
def time_from_time_column(attribute)
......
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