2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
module Boards
|
|
|
|
module Issues
|
2018-03-17 18:26:18 +05:30
|
|
|
class MoveService < Boards::BaseService
|
2016-09-13 17:45:13 +05:30
|
|
|
def execute(issue)
|
|
|
|
return false unless can?(current_user, :update_issue, issue)
|
2018-11-08 19:23:39 +05:30
|
|
|
return false if issue_params(issue).empty?
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
update(issue)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def board
|
2018-03-17 18:26:18 +05:30
|
|
|
@board ||= parent.boards.find(params[:board_id])
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def move_between_lists?
|
2016-09-13 17:45:13 +05:30
|
|
|
moving_from_list.present? && moving_to_list.present? &&
|
|
|
|
moving_from_list != moving_to_list
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-13 17:45:13 +05:30
|
|
|
def moving_from_list
|
|
|
|
@moving_from_list ||= board.lists.find_by(id: params[:from_list_id])
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-13 17:45:13 +05:30
|
|
|
def moving_to_list
|
|
|
|
@moving_to_list ||= board.lists.find_by(id: params[:to_list_id])
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def update(issue)
|
2018-11-08 19:23:39 +05:30
|
|
|
::Issues::UpdateService.new(issue.project, current_user, issue_params(issue)).execute(issue)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def issue_params(issue)
|
2017-08-17 22:00:37 +05:30
|
|
|
attrs = {}
|
|
|
|
|
|
|
|
if move_between_lists?
|
|
|
|
attrs.merge!(
|
|
|
|
add_label_ids: add_label_ids,
|
|
|
|
remove_label_ids: remove_label_ids,
|
2017-09-10 17:25:29 +05:30
|
|
|
state_event: issue_state
|
2017-08-17 22:00:37 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-01 15:08:00 +05:30
|
|
|
if move_between_ids
|
|
|
|
attrs[:move_between_ids] = move_between_ids
|
2019-03-02 22:35:43 +05:30
|
|
|
attrs[:board_group_id] = board.group&.id
|
2018-05-01 15:08:00 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
attrs
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def issue_state
|
2017-08-17 22:00:37 +05:30
|
|
|
return 'reopen' if moving_from_list.closed?
|
|
|
|
return 'close' if moving_to_list.closed?
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def add_label_ids
|
|
|
|
[moving_to_list.label_id].compact
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-13 17:45:13 +05:30
|
|
|
def remove_label_ids
|
|
|
|
label_ids =
|
|
|
|
if moving_to_list.movable?
|
|
|
|
moving_from_list.label_id
|
|
|
|
else
|
2018-12-13 13:39:08 +05:30
|
|
|
::Label.on_board(board.id).pluck(:label_id)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
Array(label_ids).compact
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def move_between_ids
|
|
|
|
return unless params[:move_after_id] || params[:move_before_id]
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
[params[:move_after_id], params[:move_before_id]]
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|