diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-01 03:09:04 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-01 03:09:04 +0000 |
commit | d0356412dfc91d02585f0a177c65677dbe2944a3 (patch) | |
tree | 5a2ac806b6ea6113475bb2a759f6b15c186fb482 /app/graphql | |
parent | 72817fd7c07d1b812623f8d5e27fc7bcecb4eed5 (diff) | |
download | gitlab-ce-d0356412dfc91d02585f0a177c65677dbe2944a3.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/graphql')
-rw-r--r-- | app/graphql/mutations/todos/restore_many.rb | 75 | ||||
-rw-r--r-- | app/graphql/types/mutation_type.rb | 1 |
2 files changed, 76 insertions, 0 deletions
diff --git a/app/graphql/mutations/todos/restore_many.rb b/app/graphql/mutations/todos/restore_many.rb new file mode 100644 index 00000000000..8a6265207cd --- /dev/null +++ b/app/graphql/mutations/todos/restore_many.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +module Mutations + module Todos + class RestoreMany < ::Mutations::Todos::Base + graphql_name 'TodoRestoreMany' + + MAX_UPDATE_AMOUNT = 50 + + argument :ids, + [GraphQL::ID_TYPE], + required: true, + description: 'The global ids of the todos to restore (a maximum of 50 is supported at once)' + + field :updated_ids, [GraphQL::ID_TYPE], + null: false, + description: 'The ids of the updated todo items' + + def resolve(ids:) + check_update_amount_limit!(ids) + + todos = authorized_find_all_pending_by_current_user(model_ids_of(ids)) + updated_ids = restore(todos) + + { + updated_ids: gids_of(updated_ids), + errors: errors_on_objects(todos) + } + end + + private + + def gids_of(ids) + ids.map { |id| ::URI::GID.build(app: GlobalID.app, model_name: Todo.name, model_id: id, params: nil).to_s } + end + + def model_ids_of(ids) + ids.map do |gid| + parsed_gid = ::URI::GID.parse(gid) + parsed_gid.model_id.to_i if accessible_todo?(parsed_gid) + end.compact + end + + def accessible_todo?(gid) + gid.app == GlobalID.app && todo?(gid) + end + + def todo?(gid) + GlobalID.parse(gid)&.model_class&.ancestors&.include?(Todo) + 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) + TodoService.new.mark_todos_as_pending(todos, current_user) + end + end + end +end diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 0a9c0143945..fc0a2a099df 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -25,6 +25,7 @@ module Types mount_mutation Mutations::Todos::MarkDone mount_mutation Mutations::Todos::Restore mount_mutation Mutations::Todos::MarkAllDone + mount_mutation Mutations::Todos::RestoreMany mount_mutation Mutations::Snippets::Destroy mount_mutation Mutations::Snippets::Update mount_mutation Mutations::Snippets::Create |