2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-28 02:33:41 -04:00
|
|
|
module Boards
|
|
|
|
module Issues
|
2017-08-28 17:56:49 -04:00
|
|
|
class MoveService < Boards::BaseService
|
2016-08-16 00:07:25 -04:00
|
|
|
def execute(issue)
|
2019-07-11 09:29:16 -04:00
|
|
|
issue_modification_params = issue_params(issue)
|
|
|
|
return false if issue_modification_params.empty?
|
2016-07-28 02:33:41 -04:00
|
|
|
|
2019-07-11 09:29:16 -04:00
|
|
|
move_single_issue(issue, issue_modification_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_multiple(issues)
|
2019-07-05 06:50:20 -04:00
|
|
|
return execute_multiple_empty_result if issues.empty?
|
2019-07-11 09:29:16 -04:00
|
|
|
|
2019-07-05 06:50:20 -04:00
|
|
|
handled_issues = []
|
2019-07-11 09:29:16 -04:00
|
|
|
last_inserted_issue_id = nil
|
2019-07-05 06:50:20 -04:00
|
|
|
count = issues.each.inject(0) do |moved_count, issue|
|
2019-07-11 09:29:16 -04:00
|
|
|
issue_modification_params = issue_params(issue)
|
2019-07-05 06:50:20 -04:00
|
|
|
next moved_count if issue_modification_params.empty?
|
2019-07-11 09:29:16 -04:00
|
|
|
|
|
|
|
if last_inserted_issue_id
|
2019-07-05 06:50:20 -04:00
|
|
|
issue_modification_params[:move_between_ids] = move_below(last_inserted_issue_id)
|
2019-07-11 09:29:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
last_inserted_issue_id = issue.id
|
2019-07-05 06:50:20 -04:00
|
|
|
handled_issue = move_single_issue(issue, issue_modification_params)
|
|
|
|
handled_issues << present_issue_entity(handled_issue) if handled_issue
|
|
|
|
handled_issue && handled_issue.valid? ? moved_count + 1 : moved_count
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
count: count,
|
|
|
|
success: count == issues.size,
|
|
|
|
issues: handled_issues
|
|
|
|
}
|
2016-07-28 02:33:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-07-05 06:50:20 -04:00
|
|
|
def present_issue_entity(issue)
|
|
|
|
::API::Entities::Issue.represent(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_multiple_empty_result
|
|
|
|
@execute_multiple_empty_result ||= {
|
|
|
|
count: 0,
|
|
|
|
success: false,
|
|
|
|
issues: []
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_below(id)
|
|
|
|
move_between_ids({ move_after_id: nil, move_before_id: id })
|
|
|
|
end
|
|
|
|
|
2019-07-11 09:29:16 -04:00
|
|
|
def move_single_issue(issue, issue_modification_params)
|
2019-07-05 06:50:20 -04:00
|
|
|
return unless can?(current_user, :update_issue, issue)
|
2019-07-11 09:29:16 -04:00
|
|
|
|
|
|
|
update(issue, issue_modification_params)
|
|
|
|
end
|
|
|
|
|
2016-10-05 13:32:53 -04:00
|
|
|
def board
|
2017-08-28 17:56:49 -04:00
|
|
|
@board ||= parent.boards.find(params[:board_id])
|
2016-10-05 13:32:53 -04:00
|
|
|
end
|
|
|
|
|
2017-02-01 13:41:01 -05:00
|
|
|
def move_between_lists?
|
2016-08-17 10:50:22 -04:00
|
|
|
moving_from_list.present? && moving_to_list.present? &&
|
|
|
|
moving_from_list != moving_to_list
|
2016-08-02 21:53:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-15 23:15:30 -04:00
|
|
|
def moving_from_list
|
|
|
|
@moving_from_list ||= board.lists.find_by(id: params[:from_list_id])
|
2016-07-28 02:33:41 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-07-28 02:33:41 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-15 23:15:30 -04:00
|
|
|
def moving_to_list
|
|
|
|
@moving_to_list ||= board.lists.find_by(id: params[:to_list_id])
|
2016-07-28 02:33:41 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-07-28 02:33:41 -04:00
|
|
|
|
2019-07-11 09:29:16 -04:00
|
|
|
def update(issue, issue_modification_params)
|
|
|
|
::Issues::UpdateService.new(issue.project, current_user, issue_modification_params).execute(issue)
|
2016-07-28 02:33:41 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
def issue_params(issue)
|
2017-02-01 13:41:01 -05:00
|
|
|
attrs = {}
|
|
|
|
|
|
|
|
if move_between_lists?
|
|
|
|
attrs.merge!(
|
|
|
|
add_label_ids: add_label_ids,
|
|
|
|
remove_label_ids: remove_label_ids,
|
2017-05-03 07:27:17 -04:00
|
|
|
state_event: issue_state
|
2017-02-01 13:41:01 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-07-11 09:29:16 -04:00
|
|
|
move_between_ids = move_between_ids(params)
|
2018-03-28 15:12:56 -04:00
|
|
|
if move_between_ids
|
|
|
|
attrs[:move_between_ids] = move_between_ids
|
2019-01-16 07:09:29 -05:00
|
|
|
attrs[:board_group_id] = board.group&.id
|
2018-03-28 15:12:56 -04:00
|
|
|
end
|
2017-02-01 13:41:01 -05:00
|
|
|
|
|
|
|
attrs
|
2016-07-28 02:33:41 -04:00
|
|
|
end
|
2016-08-03 11:45:44 -04:00
|
|
|
|
2016-08-15 23:15:30 -04:00
|
|
|
def issue_state
|
2017-03-24 08:40:35 -04:00
|
|
|
return 'reopen' if moving_from_list.closed?
|
|
|
|
return 'close' if moving_to_list.closed?
|
2016-08-15 23:15:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-03 11:45:44 -04:00
|
|
|
def add_label_ids
|
2016-08-15 23:15:30 -04:00
|
|
|
[moving_to_list.label_id].compact
|
2016-08-03 11:45:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-03 11:45:44 -04:00
|
|
|
def remove_label_ids
|
|
|
|
label_ids =
|
2016-08-16 13:38:43 -04:00
|
|
|
if moving_to_list.movable?
|
2016-08-15 23:15:30 -04:00
|
|
|
moving_from_list.label_id
|
2016-08-03 11:45:44 -04:00
|
|
|
else
|
2018-11-01 20:10:53 -04:00
|
|
|
::Label.on_board(board.id).pluck(:label_id)
|
2016-08-03 11:45:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Array(label_ids).compact
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-01 13:41:01 -05:00
|
|
|
|
2019-07-11 09:29:16 -04:00
|
|
|
def move_between_ids(move_params)
|
|
|
|
ids = [move_params[:move_after_id], move_params[:move_before_id]]
|
2019-05-31 21:19:08 -04:00
|
|
|
.map(&:to_i)
|
|
|
|
.map { |m| m.positive? ? m : nil }
|
2017-03-02 11:09:48 -05:00
|
|
|
|
2019-05-31 21:19:08 -04:00
|
|
|
ids.any? ? ids : nil
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
2016-07-28 02:33:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|