2021-04-17 20:07:23 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Boards
|
|
|
|
class BaseItemMoveService < Boards::BaseService
|
|
|
|
def execute(issuable)
|
|
|
|
issuable_modification_params = issuable_params(issuable)
|
2021-10-27 15:23:28 +05:30
|
|
|
return if issuable_modification_params.empty?
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
return unless move_single_issuable(issuable, issuable_modification_params)
|
|
|
|
|
|
|
|
success(issuable)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def success(issuable)
|
|
|
|
ServiceResponse.success(payload: { issuable: issuable })
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(issuable, message)
|
|
|
|
ServiceResponse.error(payload: { issuable: issuable }, message: message)
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def issuable_params(issuable)
|
|
|
|
attrs = {}
|
|
|
|
|
|
|
|
if move_between_lists?
|
|
|
|
attrs.merge!(
|
|
|
|
add_label_ids: add_label_ids,
|
|
|
|
remove_label_ids: remove_label_ids,
|
|
|
|
state_event: issuable_state
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
reposition_ids = move_between_ids(params)
|
2021-09-04 01:27:46 +05:30
|
|
|
attrs.merge!(reposition_params(reposition_ids)) if reposition_ids
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
attrs
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def reposition_params(reposition_ids)
|
2022-04-04 11:22:00 +05:30
|
|
|
{ move_between_ids: reposition_ids }
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def move_single_issuable(issuable, issuable_modification_params)
|
|
|
|
ability_name = :"admin_#{issuable.to_ability_name}"
|
|
|
|
return unless can?(current_user, ability_name, issuable)
|
|
|
|
|
|
|
|
update(issuable, issuable_modification_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_between_lists?
|
|
|
|
moving_from_list.present? && moving_to_list.present? &&
|
|
|
|
moving_from_list != moving_to_list
|
|
|
|
end
|
|
|
|
|
|
|
|
def moving_from_list
|
|
|
|
return unless params[:from_list_id].present?
|
|
|
|
|
|
|
|
@moving_from_list ||= board.lists.id_in(params[:from_list_id]).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def moving_to_list
|
|
|
|
return unless params[:to_list_id].present?
|
|
|
|
|
|
|
|
@moving_to_list ||= board.lists.id_in(params[:to_list_id]).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def issuable_state
|
|
|
|
return 'reopen' if moving_from_list.closed?
|
|
|
|
return 'close' if moving_to_list.closed?
|
|
|
|
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
|
2021-04-29 21:17:54 +05:30
|
|
|
board_label_ids
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
Array(label_ids).compact
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
def board_label_ids
|
|
|
|
::Label.ids_on_board(board.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_between_ids(move_params)
|
2022-04-04 11:22:00 +05:30
|
|
|
ids = [move_params[:move_before_id], move_params[:move_after_id]]
|
2021-04-29 21:17:54 +05:30
|
|
|
.map(&:to_i)
|
|
|
|
.map { |m| m > 0 ? m : nil }
|
|
|
|
|
|
|
|
ids.any? ? ids : nil
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|