2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
module Groups
|
|
|
|
class DestroyService < Groups::BaseService
|
2018-08-13 07:54:31 -04:00
|
|
|
DestroyError = Class.new(StandardError)
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
def async_execute
|
|
|
|
job_id = GroupDestroyWorker.perform_async(group.id, current_user.id)
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.info("User #{current_user.id} scheduled a deletion of group ID #{group.id} with job ID #{job_id}") # rubocop:disable Gitlab/RailsLogger
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-13 08:45:31 -04:00
|
|
|
def execute
|
2017-02-16 02:50:05 -05:00
|
|
|
group.prepare_for_destroy
|
|
|
|
|
2018-08-23 18:52:35 -04:00
|
|
|
group.projects.includes(:project_feature).each do |project|
|
2016-08-13 08:45:31 -04:00
|
|
|
# Execute the destruction of the models immediately to ensure atomic cleanup.
|
2018-08-13 07:54:31 -04:00
|
|
|
success = ::Projects::DestroyService.new(project, current_user).execute
|
|
|
|
raise DestroyError, "Project #{project.id} can't be deleted" unless success
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
|
2018-08-23 18:52:35 -04:00
|
|
|
# reload the relation to prevent triggering destroy hooks on the projects again
|
2019-04-08 09:33:36 -04:00
|
|
|
group.projects.reset
|
2018-08-23 18:52:35 -04:00
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
group.children.each do |group|
|
2017-02-15 20:15:54 -05:00
|
|
|
# This needs to be synchronous since the namespace gets destroyed below
|
|
|
|
DestroyService.new(group, current_user).execute
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-05-14 06:57:08 -04:00
|
|
|
group.chat_team&.remove_mattermost_team(current_user)
|
|
|
|
|
2018-01-02 11:21:28 -05:00
|
|
|
group.destroy
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Groups::DestroyService.prepend_if_ee('EE::Groups::DestroyService')
|