destroy_service.rb 1.11 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
module Groups
  class DestroyService < Groups::BaseService
5
6
    DestroyError = Class.new(StandardError)

7
8
9
10
11
12
    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

    def execute
13
14
      group.prepare_for_destroy

15
      group.projects.includes(:project_feature).each do |project|
16
        # Execute the destruction of the models immediately to ensure atomic cleanup.
17
18
        success = ::Projects::DestroyService.new(project, current_user).execute
        raise DestroyError, "Project #{project.id} can't be deleted" unless success
19
20
      end

21
22
23
      # reload the relation to prevent triggering destroy hooks on the projects again
      group.projects.reload

24
      group.children.each do |group|
25
26
        # This needs to be synchronous since the namespace gets destroyed below
        DestroyService.new(group, current_user).execute
27
28
      end

29
30
      group.chat_team&.remove_mattermost_team(current_user)

31
      group.destroy
32
33
34
    end
  end
end