1# frozen_string_literal: true
2
3module Mutations
4  module DesignManagement
5    class Move < ::Mutations::BaseMutation
6      graphql_name "DesignManagementMove"
7
8      DesignID = ::Types::GlobalIDType[::DesignManagement::Design]
9
10      argument :id, DesignID, required: true, as: :current_design,
11               description: "ID of the design to move."
12
13      argument :previous, DesignID, required: false, as: :previous_design,
14               description: "ID of the immediately preceding design."
15
16      argument :next, DesignID, required: false, as: :next_design,
17               description: "ID of the immediately following design."
18
19      field :design_collection, Types::DesignManagement::DesignCollectionType,
20            null: true,
21            description: "Current state of the collection."
22
23      def resolve(**args)
24        service = ::DesignManagement::MoveDesignsService.new(current_user, parameters(**args))
25
26        { design_collection: service.collection, errors: service.execute.errors }
27      end
28
29      private
30
31      def parameters(**args)
32        args.transform_values { |id| find_design(id) }.transform_values(&:sync).tap do |hash|
33          hash.each { |k, design| not_found(args[k]) unless current_user.can?(:read_design, design) }
34        end
35      end
36
37      def find_design(id)
38        # TODO: remove this line when the compatibility layer is removed
39        # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
40        id = DesignID.coerce_isolated_input(id)
41        GitlabSchema.find_by_gid(id)
42      end
43
44      def not_found(gid)
45        raise Gitlab::Graphql::Errors::ResourceNotAvailable, "Resource not available: #{gid}"
46      end
47    end
48  end
49end
50