summaryrefslogtreecommitdiff
path: root/app/services/boards/issues/move_service.rb
blob: 84dc3f70e7661021c1fc94e4e61482748cf9aa54 (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
module Boards
  module Issues
    class MoveService < Boards::BaseService
      def execute(issue)
        return false unless can?(current_user, :update_issue, issue)
        return false unless valid_move?

        update_service.execute(issue)
      end

      private

      def valid_move?
        moving_from_list.present? && moving_to_list.present? &&
          moving_from_list != moving_to_list
      end

      def moving_from_list
        @moving_from_list ||= board.lists.find_by(id: params[:from_list_id])
      end

      def moving_to_list
        @moving_to_list ||= board.lists.find_by(id: params[:to_list_id])
      end

      def update_service
        ::Issues::UpdateService.new(project, current_user, issue_params)
      end

      def issue_params
        {
          add_label_ids: add_label_ids,
          remove_label_ids: remove_label_ids,
          state_event: issue_state
        }
      end

      def issue_state
        return 'reopen' if moving_from_list.done?
        return 'close'  if moving_to_list.done?
      end

      def add_label_ids
        [moving_to_list.label_id].compact
      end

      def remove_label_ids
        label_ids =
          if moving_to_list.movable?
            moving_from_list.label_id
          else
            board.lists.movable.pluck(:label_id)
          end

        Array(label_ids).compact
      end
    end
  end
end