Commit 4d709949 authored by Sean Arnold's avatar Sean Arnold

Update rotation active period after schedule changes

parent 8ecbfd78
......@@ -8,6 +8,8 @@ module IncidentManagement
# @param params [Hash]
def initialize(oncall_schedule, user, params)
@oncall_schedule = oncall_schedule
@original_schedule_timezone = oncall_schedule&.timezone
@oncall_rotations = oncall_schedule&.rotations
@user = user
@params = params
@project = oncall_schedule.project
......@@ -18,6 +20,7 @@ module IncidentManagement
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)
......@@ -26,7 +29,23 @@ module IncidentManagement
private
attr_reader :oncall_schedule, :user, :params, :project
attr_reader :oncall_schedule, :original_schedule_timezone, :oncall_rotations, :user, :params, :project
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')
)
end
# TODO do we need to handle run update rotation job?
# Should the above be moved to update rotation job?
end
def new_rotation_active_period(time_string)
time_string.in_time_zone(original_schedule_timezone).in_time_zone(oncall_schedule.timezone)
end
def error_no_permissions
error(_('You have insufficient permissions to update an on-call schedule for this project'))
......
......@@ -68,6 +68,28 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do
expect(oncall_schedule.description).to eq(params[:description])
expect(oncall_schedule.timezone).to eq(params[:timezone])
end
context 'schedule has a rotation' do
# Setting fixed timezone for rotation active period updates
around do |example|
freeze_time do
travel_to Time.utc(2021, 03, 22)
example.run
end
end
let_it_be_with_reload(:oncall_rotation) { create(:incident_management_oncall_rotation, :with_active_period, schedule: oncall_schedule) }
it 'updates the rotation active periods with new timezone' 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
end
def time_from_time_column(attribute)
attribute.strftime('%H:%M')
end
end
end
end
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