idoit_object_add.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Ticket::ExternalReferences::IdoitObjectAdd < BaseMutation
  4. description 'Add idoit objects to a ticket or just resolve them for ticket creation.'
  5. argument :ticket_id, GraphQL::Types::ID, required: false, loads: Gql::Types::TicketType, description: 'The related ticket for the idoit objects'
  6. argument :idoit_object_ids, [Integer], description: 'The idoit objects to add'
  7. field :idoit_objects, [Gql::Types::Ticket::ExternalReferences::IdoitObjectType], description: 'The added / resolved idoit objects'
  8. def self.authorize(_obj, _ctx)
  9. Setting.get('idoit_integration')
  10. end
  11. def authorized?(idoit_object_ids:, ticket: nil)
  12. if ticket.present?
  13. Pundit.authorize(context.current_user, ticket, :agent_update_access?)
  14. else
  15. context.current_user.permissions?('ticket.agent')
  16. end
  17. end
  18. def resolve(idoit_object_ids:, ticket: nil)
  19. results = []
  20. existing_ids = ticket&.preferences&.dig(:idoit, :object_ids) || []
  21. idoit_object_ids.each do |idoit_object_id|
  22. if existing_ids.include?(idoit_object_id)
  23. return error_response({ field: :idoit_object_ids, message: __('The idoit object is already present on the ticket.') })
  24. end
  25. api_object = Idoit.query('cmdb.objects', { ids: [idoit_object_id] })['result'].first
  26. if !api_object
  27. return error_response({ field: :idoit_object_ids, message: __('The idoit object could not be found.') })
  28. end
  29. results.push api_object
  30. end
  31. if ticket.present?
  32. ticket.preferences[:idoit] ||= {}
  33. ticket.preferences[:idoit][:object_ids] ||= []
  34. ticket.preferences[:idoit][:object_ids].push(*idoit_object_ids)
  35. ticket.save!
  36. end
  37. { idoit_objects: results }
  38. end
  39. end
  40. end