summaryrefslogtreecommitdiff
path: root/app/services/members/authorized_destroy_service.rb
blob: de3a252d6c6797d64e5d87e0530c8e4565d8eba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Members
  class AuthorizedDestroyService < BaseService
    attr_accessor :member, :user

    def initialize(member, user = nil)
      @member, @user = member, user
    end

    def execute
      return false if member.is_a?(GroupMember) && member.source.last_owner?(member.user)

      Member.transaction do
        unassign_issues_and_merge_requests(member) unless member.invite?

        member.destroy
      end

      if member.request? && member.user != user
        notification_service.decline_access_request(member)
      end

      member
    end

    private

    def unassign_issues_and_merge_requests(member)
      if member.is_a?(GroupMember)
        issues = Issue.unscoped.select(1)
                 .joins(:project)
                 .where('issues.id = issue_assignees.issue_id AND projects.namespace_id = ?', member.source_id)

        # DELETE FROM issue_assignees WHERE user_id = X AND EXISTS (...)
        IssueAssignee.unscoped
          .where('user_id = :user_id AND EXISTS (:sub)', user_id: member.user_id, sub: issues)
          .delete_all

        MergeRequestsFinder.new(user, group_id: member.source_id, assignee_id: member.user_id)
          .execute
          .update_all(assignee_id: nil)
      else
        project = member.source

        # SELECT 1 FROM issues WHERE issues.id = issue_assignees.issue_id AND issues.project_id = X
        issues = Issue.unscoped.select(1)
                 .where('issues.id = issue_assignees.issue_id')
                 .where(project_id: project.id)

        # DELETE FROM issue_assignees WHERE user_id = X AND EXISTS (...)
        IssueAssignee.unscoped
          .where('user_id = :user_id AND EXISTS (:sub)', user_id: member.user_id, sub: issues)
          .delete_all

        project.merge_requests.opened.assigned_to(member.user).update_all(assignee_id: nil)
      end

      member.user.invalidate_cache_counts
    end
  end
end