summaryrefslogtreecommitdiff
path: root/app/services/todos/destroy/entity_leave_service.rb
blob: 129e5505a21a15d3ed7132c373291577a3ee419b (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
module Todos
  module Destroy
    class EntityLeaveService < ::Todos::Destroy::BaseService
      extend ::Gitlab::Utils::Override

      attr_reader :user_id, :entity

      def initialize(user_id, entity_id, entity_type)
        unless %w(Group Project).include?(entity_type)
          raise ArgumentError.new("#{entity_type} is not an entity user can leave")
        end

        @user_id = user_id
        @entity = entity_type.constantize.find_by(id: entity_id)
      end

      private

      override :todos
      def todos
        if entity.private?
          Todo.where(project_id: project_ids, user_id: user_id)
        else
          Todo.where(target_id: confidential_issues.select(:id), target_type: Issue)
        end
      end

      override :project_ids
      def project_ids
        if entity.is_a?(Project)
          entity.id
        else
          Project.select(:id).where(namespace_id: entity.self_and_descendants.select(:id))
        end
      end

      override :todos_to_remove?
      def todos_to_remove?
        return unless entity

        entity.private? || confidential_issues.count > 0
      end

      def confidential_issues
        assigned_ids = IssueAssignee.select(:issue_id).where(user_id: user_id)

        Issue.where(project_id: project_ids, confidential: true)
          .where('author_id != ?', user_id)
          .where('id NOT IN (?)', assigned_ids)
      end
    end
  end
end