summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/todos/restore_many.rb
blob: ea5f5414134b4b7d5b60c1664df117e867769b5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

module Mutations
  module Todos
    class RestoreMany < ::Mutations::Todos::Base
      graphql_name 'TodoRestoreMany'

      MAX_UPDATE_AMOUNT = 50

      argument :ids,
               [::Types::GlobalIDType[::Todo]],
               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',
            deprecated: { reason: 'Use todos', milestone: '13.2' }

      field :todos, [::Types::TodoType],
            null: false,
            description: 'Updated todos'

      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),
            todos: Todo.id_in(updated_ids),
            errors: errors_on_objects(todos)
        }
      end

      private

      def gids_of(ids)
        ids.map { |id| Gitlab::GlobalId.as_global_id(id, model_name: Todo.name).to_s }
      end

      def model_ids_of(ids)
        ids.map do |gid|
          # 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
        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)
        TodoService.new.restore_todos(todos, current_user)
      end
    end
  end
end