Commit 9729ea36 authored by Sarah Yasonik's avatar Sarah Yasonik Committed by Luke Duncalfe

Swap OncallSchedule services to inherit from BaseProjectService

parent 0201fca5
...@@ -28,9 +28,9 @@ module Mutations ...@@ -28,9 +28,9 @@ module Mutations
project = authorized_find!(args[:project_path]) project = authorized_find!(args[:project_path])
response ::IncidentManagement::OncallSchedules::CreateService.new( response ::IncidentManagement::OncallSchedules::CreateService.new(
project, project: project,
current_user, current_user: current_user,
args.slice(:name, :description, :timezone) params: args.slice(:name, :description, :timezone)
).execute ).execute
end end
end end
......
...@@ -18,9 +18,9 @@ module Mutations ...@@ -18,9 +18,9 @@ module Mutations
oncall_schedule = authorized_find!(project_path: project_path, iid: iid) oncall_schedule = authorized_find!(project_path: project_path, iid: iid)
response ::IncidentManagement::OncallSchedules::DestroyService.new( response ::IncidentManagement::OncallSchedules::DestroyService.new(
oncall_schedule, project: oncall_schedule.project,
current_user current_user: current_user
).execute ).execute(oncall_schedule)
end end
end end
end end
......
...@@ -30,10 +30,10 @@ module Mutations ...@@ -30,10 +30,10 @@ module Mutations
oncall_schedule = authorized_find!(project_path: args[:project_path], iid: args[:iid]) oncall_schedule = authorized_find!(project_path: args[:project_path], iid: args[:iid])
response ::IncidentManagement::OncallSchedules::UpdateService.new( response ::IncidentManagement::OncallSchedules::UpdateService.new(
oncall_schedule, project: oncall_schedule.project,
current_user, current_user: current_user,
args.slice(:name, :description, :timezone) params: args.slice(:name, :description, :timezone)
).execute ).execute(oncall_schedule)
end end
end end
end end
......
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
module IncidentManagement module IncidentManagement
module OncallSchedules module OncallSchedules
class BaseService class BaseService < ::BaseProjectService
def allowed? def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project) current_user&.can?(:admin_incident_management_oncall_schedule, project)
end end
def available? def available?
......
...@@ -3,15 +3,6 @@ ...@@ -3,15 +3,6 @@
module IncidentManagement module IncidentManagement
module OncallSchedules module OncallSchedules
class CreateService < OncallSchedules::BaseService class CreateService < OncallSchedules::BaseService
# @param project [Project]
# @param user [User]
# @param params [Hash]
def initialize(project, user, params)
@project = project
@user = user
@params = params
end
def execute def execute
return error_no_license unless available? return error_no_license unless available?
return error_no_permissions unless allowed? return error_no_permissions unless allowed?
...@@ -24,8 +15,6 @@ module IncidentManagement ...@@ -24,8 +15,6 @@ module IncidentManagement
private private
attr_reader :project, :user, :params
def error_no_permissions def error_no_permissions
error(_('You have insufficient permissions to create an on-call schedule for this project')) error(_('You have insufficient permissions to create an on-call schedule for this project'))
end end
......
...@@ -3,15 +3,7 @@ ...@@ -3,15 +3,7 @@
module IncidentManagement module IncidentManagement
module OncallSchedules module OncallSchedules
class DestroyService < OncallSchedules::BaseService class DestroyService < OncallSchedules::BaseService
# @param oncall_schedule [IncidentManagement::OncallSchedule] def execute(oncall_schedule)
# @param user [User]
def initialize(oncall_schedule, user)
@oncall_schedule = oncall_schedule
@user = user
@project = oncall_schedule.project
end
def execute
return error_no_license unless available? return error_no_license unless available?
return error_no_permissions unless allowed? return error_no_permissions unless allowed?
...@@ -24,8 +16,6 @@ module IncidentManagement ...@@ -24,8 +16,6 @@ module IncidentManagement
private private
attr_reader :oncall_schedule, :user, :project
def error_no_permissions def error_no_permissions
error(_('You have insufficient permissions to remove an on-call schedule from this project')) error(_('You have insufficient permissions to remove an on-call schedule from this project'))
end end
......
...@@ -3,24 +3,13 @@ ...@@ -3,24 +3,13 @@
module IncidentManagement module IncidentManagement
module OncallSchedules module OncallSchedules
class UpdateService < OncallSchedules::BaseService class UpdateService < OncallSchedules::BaseService
# @param oncall_schedule [IncidentManagement::OncallSchedule] def execute(oncall_schedule)
# @param user [User]
# @param params [Hash]
def initialize(oncall_schedule, user, params)
@oncall_schedule = oncall_schedule
@original_schedule_timezone = oncall_schedule.timezone
@user = user
@params = params
@project = oncall_schedule.project
end
def execute
return error_no_license unless available? return error_no_license unless available?
return error_no_permissions unless allowed? return error_no_permissions unless allowed?
IncidentManagement::OncallSchedule.transaction do IncidentManagement::OncallSchedule.transaction do
oncall_schedule.update!(params) oncall_schedule.update!(params)
update_rotations! update_rotations!(oncall_schedule)
end end
success(oncall_schedule) success(oncall_schedule)
...@@ -32,22 +21,16 @@ module IncidentManagement ...@@ -32,22 +21,16 @@ module IncidentManagement
private private
attr_reader :oncall_schedule, :original_schedule_timezone, :user, :params, :project def update_rotations!(oncall_schedule)
return unless oncall_schedule.timezone_previously_changed?
def update_rotations!
return if same_schedule_timezone?
update_rotation_active_periods!
end
def same_schedule_timezone? update_rotation_active_periods!(oncall_schedule)
original_schedule_timezone == oncall_schedule.timezone
end end
# Converts & updates the active period to the new timezone # Converts & updates the active period to the new timezone
# Ex: 8:00 - 17:00 Europe/Berlin becomes 6:00 - 15:00 UTC # Ex: 8:00 - 17:00 Europe/Berlin becomes 6:00 - 15:00 UTC
def update_rotation_active_periods! def update_rotation_active_periods!(oncall_schedule)
original_schedule_current_time = Time.current.in_time_zone(original_schedule_timezone) original_schedule_current_time = Time.current.in_time_zone(oncall_schedule.timezone_previously_was)
oncall_schedule.rotations.with_active_period.each do |rotation| oncall_schedule.rotations.with_active_period.each do |rotation|
active_period = rotation.active_period.for_date(original_schedule_current_time) active_period = rotation.active_period.for_date(original_schedule_current_time)
...@@ -55,7 +38,7 @@ module IncidentManagement ...@@ -55,7 +38,7 @@ module IncidentManagement
service = IncidentManagement::OncallRotations::EditService.new( service = IncidentManagement::OncallRotations::EditService.new(
rotation, rotation,
user, current_user,
{ {
active_period_start: new_start_time, active_period_start: new_start_time,
active_period_end: new_end_time active_period_end: new_end_time
......
...@@ -9,7 +9,7 @@ RSpec.describe IncidentManagement::OncallSchedules::CreateService do ...@@ -9,7 +9,7 @@ RSpec.describe IncidentManagement::OncallSchedules::CreateService do
let(:current_user) { user_with_permissions } let(:current_user) { user_with_permissions }
let(:params) { { name: 'On-call schedule', description: 'On-call schedule description', timezone: 'Europe/Berlin' } } let(:params) { { name: 'On-call schedule', description: 'On-call schedule description', timezone: 'Europe/Berlin' } }
let(:service) { described_class.new(project, current_user, params) } let(:service) { described_class.new(project: project, current_user: current_user, params: params) }
before do before do
stub_licensed_features(oncall_schedules: true) stub_licensed_features(oncall_schedules: true)
......
...@@ -10,7 +10,7 @@ RSpec.describe IncidentManagement::OncallSchedules::DestroyService do ...@@ -10,7 +10,7 @@ RSpec.describe IncidentManagement::OncallSchedules::DestroyService do
let!(:oncall_schedule) { create(:incident_management_oncall_schedule, project: project) } let!(:oncall_schedule) { create(:incident_management_oncall_schedule, project: project) }
let(:current_user) { user_with_permissions } let(:current_user) { user_with_permissions }
let(:params) { {} } let(:params) { {} }
let(:service) { described_class.new(oncall_schedule, current_user) } let(:service) { described_class.new(project: project, current_user: current_user) }
before do before do
stub_licensed_features(oncall_schedules: true) stub_licensed_features(oncall_schedules: true)
...@@ -25,7 +25,7 @@ RSpec.describe IncidentManagement::OncallSchedules::DestroyService do ...@@ -25,7 +25,7 @@ RSpec.describe IncidentManagement::OncallSchedules::DestroyService do
end end
end end
subject(:execute) { service.execute } subject(:execute) { service.execute(oncall_schedule) }
context 'when the current_user is anonymous' do context 'when the current_user is anonymous' do
let(:current_user) { nil } let(:current_user) { nil }
......
...@@ -11,7 +11,7 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do ...@@ -11,7 +11,7 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do
let(:current_user) { user_with_permissions } let(:current_user) { user_with_permissions }
let(:new_timezone) { 'America/New_York' } let(:new_timezone) { 'America/New_York' }
let(:params) { { name: 'Updated name', description: 'Updated description', timezone: new_timezone } } let(:params) { { name: 'Updated name', description: 'Updated description', timezone: new_timezone } }
let(:service) { described_class.new(oncall_schedule, current_user, params) } let(:service) { described_class.new(project: project, current_user: current_user, params: params) }
before do before do
stub_licensed_features(oncall_schedules: true) stub_licensed_features(oncall_schedules: true)
...@@ -26,7 +26,7 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do ...@@ -26,7 +26,7 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do
end end
end end
subject(:execute) { service.execute } subject(:execute) { service.execute(oncall_schedule) }
context 'when the current_user is anonymous' do context 'when the current_user is anonymous' do
let(:current_user) { nil } let(:current_user) { nil }
......
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