debian-mirror-gitlab/app/graphql/mutations/todos/restore_many.rb

72 lines
2 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Mutations
module Todos
class RestoreMany < ::Mutations::Todos::Base
graphql_name 'TodoRestoreMany'
MAX_UPDATE_AMOUNT = 50
argument :ids,
2021-01-03 14:25:43 +05:30
[::Types::GlobalIDType[::Todo]],
2020-03-13 15:44:24 +05:30
required: true,
2021-02-22 17:27:13 +05:30
description: 'The global IDs of the todos to restore (a maximum of 50 is supported at once)'
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
field :updated_ids, [::Types::GlobalIDType[Todo]],
2020-03-13 15:44:24 +05:30
null: false,
2021-02-22 17:27:13 +05:30
description: 'The IDs of the updated todo items',
2020-07-28 23:09:34 +05:30
deprecated: { reason: 'Use todos', milestone: '13.2' }
field :todos, [::Types::TodoType],
null: false,
description: 'Updated todos'
2020-03-13 15:44:24 +05:30
def resolve(ids:)
check_update_amount_limit!(ids)
todos = authorized_find_all_pending_by_current_user(model_ids_of(ids))
updated_ids = restore(todos)
{
2021-01-29 00:20:46 +05:30
updated_ids: updated_ids,
2020-07-28 23:09:34 +05:30
todos: Todo.id_in(updated_ids),
2020-03-13 15:44:24 +05:30
errors: errors_on_objects(todos)
}
end
private
def model_ids_of(ids)
ids.map do |gid|
2021-01-03 14:25:43 +05:30
# 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-03-13 15:44:24 +05:30
end.compact
end
def raise_too_many_todos_requested_error
raise Gitlab::Graphql::Errors::ArgumentError, 'Too many todos requested.'
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?
Todo.for_ids(ids).for_user(current_user).done
end
def restore(todos)
2020-06-23 00:09:42 +05:30
TodoService.new.restore_todos(todos, current_user)
2020-03-13 15:44:24 +05:30
end
end
end
end