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