summaryrefslogtreecommitdiff
path: root/app/services/todos/destroy/private_features_service.rb
blob: a8c3fe0ef5a43267342a41f32d3cba5b1040f21d (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
# frozen_string_literal: true

module Todos
  module Destroy
    class PrivateFeaturesService < ::Todos::Destroy::BaseService
      attr_reader :project_ids, :user_id

      def initialize(project_ids, user_id = nil)
        @project_ids = project_ids
        @user_id = user_id
      end

      # rubocop: disable CodeReuse/ActiveRecord
      def execute
        ProjectFeature.where(project_id: project_ids).each do |project_features|
          target_types = []
          target_types << Issue if private?(project_features.issues_access_level)
          target_types << MergeRequest if private?(project_features.merge_requests_access_level)
          target_types << Commit if private?(project_features.repository_access_level)

          next if target_types.empty?

          remove_todos(project_features.project_id, target_types)
        end
      end
      # rubocop: enable CodeReuse/ActiveRecord

      private

      def private?(feature_level)
        feature_level == ProjectFeature::PRIVATE
      end

      # rubocop: disable CodeReuse/ActiveRecord
      def remove_todos(project_id, target_types)
        items = Todo.where(project_id: project_id)
        items = items.where(user_id: user_id) if user_id

        items.where('user_id NOT IN (?)', authorized_users)
          .where(target_type: target_types)
          .delete_all
      end
      # rubocop: enable CodeReuse/ActiveRecord
    end
  end
end