2020-01-31 22:09:04 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Todos
|
|
|
|
class RestoreMany < ::Mutations::Todos::Base
|
|
|
|
graphql_name 'TodoRestoreMany'
|
|
|
|
|
|
|
|
MAX_UPDATE_AMOUNT = 50
|
|
|
|
|
|
|
|
argument :ids,
|
2020-10-07 23:08:39 -04:00
|
|
|
[::Types::GlobalIDType[::Todo]],
|
2020-01-31 22:09:04 -05:00
|
|
|
required: true,
|
2021-02-04 16:09:06 -05:00
|
|
|
description: 'The global IDs of the to-do items to restore (a maximum of 50 is supported at once).'
|
2020-01-31 22:09:04 -05:00
|
|
|
|
2020-11-10 07:08:57 -05:00
|
|
|
field :updated_ids, [::Types::GlobalIDType[Todo]],
|
2020-01-31 22:09:04 -05:00
|
|
|
null: false,
|
2021-01-29 19:09:06 -05:00
|
|
|
description: 'The IDs of the updated to-do items.',
|
2021-02-04 16:09:06 -05:00
|
|
|
deprecated: { reason: 'Use to-do items', milestone: '13.2' }
|
2020-07-07 08:09:16 -04:00
|
|
|
|
|
|
|
field :todos, [::Types::TodoType],
|
|
|
|
null: false,
|
2021-02-04 16:09:06 -05:00
|
|
|
description: 'Updated to-do items.'
|
2020-01-31 22:09:04 -05:00
|
|
|
|
|
|
|
def resolve(ids:)
|
|
|
|
check_update_amount_limit!(ids)
|
|
|
|
|
|
|
|
todos = authorized_find_all_pending_by_current_user(model_ids_of(ids))
|
|
|
|
updated_ids = restore(todos)
|
|
|
|
|
|
|
|
{
|
2020-11-10 07:08:57 -05:00
|
|
|
updated_ids: updated_ids,
|
2020-07-07 08:09:16 -04:00
|
|
|
todos: Todo.id_in(updated_ids),
|
2020-01-31 22:09:04 -05:00
|
|
|
errors: errors_on_objects(todos)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def model_ids_of(ids)
|
|
|
|
ids.map do |gid|
|
2020-10-07 23:08:39 -04:00
|
|
|
# TODO: remove this line when the compatibility layer is removed
|
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
|
|
|
|
gid = ::Types::GlobalIDType[::Todo].coerce_isolated_input(gid)
|
|
|
|
gid.model_id.to_i
|
2020-01-31 22:09:04 -05:00
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_too_many_todos_requested_error
|
2021-02-04 16:09:06 -05:00
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, 'Too many to-do items requested.'
|
2020-01-31 22:09:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_update_amount_limit!(ids)
|
|
|
|
raise_too_many_todos_requested_error if ids.size > MAX_UPDATE_AMOUNT
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors_on_objects(todos)
|
|
|
|
todos.flat_map { |todo| errors_on_object(todo) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorized_find_all_pending_by_current_user(ids)
|
|
|
|
return Todo.none if ids.blank? || current_user.nil?
|
|
|
|
|
2021-02-25 04:10:45 -05:00
|
|
|
Todo.id_in(ids).for_user(current_user).done
|
2020-01-31 22:09:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore(todos)
|
2020-05-28 14:08:37 -04:00
|
|
|
TodoService.new.restore_todos(todos, current_user)
|
2020-01-31 22:09:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|