applies_ticket_shared_draft.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module FormUpdater::Concerns::AppliesTicketSharedDraft
  3. extend ActiveSupport::Concern
  4. class_methods do
  5. def apply_shared_draft_group_keys(group_keys)
  6. @apply_shared_draft_group_keys ||= group_keys
  7. end
  8. end
  9. def resolve
  10. if agent? && selected_draft.present?
  11. apply_draft
  12. end
  13. super
  14. end
  15. private
  16. def apply_draft
  17. apply_value = FormUpdater::ApplyValue.new(context:, data:, result:)
  18. new_attachments = UserInfo.with_user_id(context[:current_user].id) do
  19. selected_draft.clone_attachments('UploadCache', meta[:form_id])
  20. end
  21. apply_value.perform(field: 'attachments', config: { 'value' => new_attachments.reject(&:inline?) })
  22. apply_shared_draft_group_keys = self.class.instance_variable_get(:@apply_shared_draft_group_keys)
  23. selected_draft
  24. .content_with_form_id_body_urls(meta[:form_id])
  25. .each_pair do |field, value|
  26. if apply_shared_draft_group_keys.present? && apply_shared_draft_group_keys.include?(field) && value.is_a?(Hash)
  27. value.each_pair do |sub_field, sub_value|
  28. apply_value.perform(field: sub_field, config: { 'value' => sub_value }, parent_field: field)
  29. end
  30. else
  31. apply_value.perform(field: field, config: { 'value' => value })
  32. end
  33. end
  34. # Include shared draft internal ID for a subsequent reference.
  35. apply_value.perform(field: 'shared_draft_id', config: { 'value' => selected_draft.id })
  36. end
  37. def selected_draft
  38. @selected_draft ||= begin
  39. id = meta.dig(:additional_data, 'sharedDraftId')
  40. draft_type = meta.dig(:additional_data, 'draftType') == 'start' ? ::Ticket::SharedDraftStart : ::Ticket::SharedDraftZoom
  41. Gql::ZammadSchema.authorized_object_from_id(id, type: draft_type, user: context[:current_user]) if id.present?
  42. end
  43. end
  44. def agent?
  45. current_user.permissions?('ticket.agent')
  46. end
  47. end