1# frozen_string_literal: true
2
3module Mutations
4  module CustomerRelations
5    module Contacts
6      class Update < Mutations::BaseMutation
7        include ResolvesIds
8
9        graphql_name 'CustomerRelationsContactUpdate'
10
11        authorize :admin_crm_contact
12
13        field :contact,
14              Types::CustomerRelations::ContactType,
15              null: true,
16              description: 'Contact after the mutation.'
17
18        argument :id, ::Types::GlobalIDType[::CustomerRelations::Contact],
19                 required: true,
20                 description: 'Global ID of the contact.'
21
22        argument :organization_id, ::Types::GlobalIDType[::CustomerRelations::Organization],
23                 required: false,
24                 description: 'Organization of the contact.'
25
26        argument :first_name, GraphQL::Types::String,
27                  required: false,
28                  description: 'First name of the contact.'
29
30        argument :last_name, GraphQL::Types::String,
31                  required: false,
32                  description: 'Last name of the contact.'
33
34        argument :phone, GraphQL::Types::String,
35                  required: false,
36                  description: 'Phone number of the contact.'
37
38        argument :email, GraphQL::Types::String,
39                  required: false,
40                  description: 'Email address of the contact.'
41
42        argument :description, GraphQL::Types::String,
43                  required: false,
44                  description: 'Description of or notes for the contact.'
45
46        def resolve(args)
47          contact = ::Gitlab::Graphql::Lazy.force(GitlabSchema.object_from_id(args.delete(:id), expected_type: ::CustomerRelations::Contact))
48          raise_resource_not_available_error! unless contact
49
50          group = contact.group
51          authorize!(group)
52
53          result = ::CustomerRelations::Contacts::UpdateService.new(group: group, current_user: current_user, params: args).execute(contact)
54          { contact: result.payload, errors: result.errors }
55        end
56      end
57    end
58  end
59end
60