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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.6 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Mutations
module Todos
2023-06-20 00:43:36 +05:30
class RestoreMany < ::Mutations::BaseMutation
2020-03-13 15:44:24 +05:30
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-10-27 15:23:28 +05:30
description: 'Global IDs of the to-do items to restore (a maximum of 50 is supported at once).'
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
field :todos, [::Types::TodoType],
null: false,
2021-03-11 19:13:27 +05:30
description: 'Updated to-do items.'
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)
{
2023-03-04 22:38:38 +05:30
updated_ids: updated_ids,
todos: Todo.id_in(updated_ids),
errors: errors_on_objects(todos)
2020-03-13 15:44:24 +05:30
}
end
private
def model_ids_of(ids)
2022-10-11 01:57:18 +05:30
ids.filter_map { |gid| gid.model_id.to_i }
2020-03-13 15:44:24 +05:30
end
def raise_too_many_todos_requested_error
2021-03-11 19:13:27 +05:30
raise Gitlab::Graphql::Errors::ArgumentError, 'Too many to-do items requested.'
2020-03-13 15:44:24 +05:30
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-04-17 20:07:23 +05:30
Todo.id_in(ids).for_user(current_user).done
2020-03-13 15:44:24 +05:30
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