summaryrefslogtreecommitdiff
path: root/app/services/boards/issues/list_service.rb
blob: 185838764c15de8a6793f516dc3f4c0fb49915d9 (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
61
62
63
64
65
66
67
68
module Boards
  module Issues
    class ListService < BaseService
      def execute
        issues = IssuesFinder.new(current_user, filter_params).execute
        issues = without_board_labels(issues) unless movable_list?
        issues = with_list_label(issues) if movable_list?
        issues.reorder(Gitlab::Database.nulls_last_order('relative_position', 'ASC'))
      end

      private

      def board
        @board ||= project.boards.find(params[:board_id])
      end

      def list
        return @list if defined?(@list)

        @list = board.lists.find(params[:id]) if params.key?(:id)
      end

      def movable_list?
        @movable_list ||= list.present? && list.movable?
      end

      def filter_params
        set_default_scope
        set_project
        set_state

        params
      end

      def set_default_scope
        params[:scope] = 'all'
      end

      def set_project
        params[:project_id] = project.id
      end

      def set_state
        params[:state] = list && list.done? ? 'closed' : 'opened'
      end

      def board_label_ids
        @board_label_ids ||= board.lists.movable.pluck(:label_id)
      end

      def without_board_labels(issues)
        return issues unless board_label_ids.any?

        issues.where.not(
          LabelLink.where("label_links.target_type = 'Issue' AND label_links.target_id = issues.id")
                   .where(label_id: board_label_ids).limit(1).arel.exists
        )
      end

      def with_list_label(issues)
        issues.where(
          LabelLink.where("label_links.target_type = 'Issue' AND label_links.target_id = issues.id")
                   .where("label_links.label_id = ?", list.label_id).limit(1).arel.exists
        )
      end
    end
  end
end