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)
|
|
|
|
Rails.logger.info("User #{current_user.id} scheduled a deletion of group ID #{group.id} with job ID #{job_id}")
|
|
|
|
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
|
|
|
|
group.projects.reload
|
|
|
|
|
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
|