applies_split_ticket_article.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module FormUpdater::Concerns::AppliesSplitTicketArticle
  3. extend ActiveSupport::Concern
  4. def resolve
  5. if agent? && selected_ticket_article.present?
  6. apply_ticket_article
  7. end
  8. super
  9. end
  10. private
  11. def apply_value
  12. @apply_value ||= FormUpdater::ApplyValue.new(context:, data:, result:)
  13. end
  14. def apply_ticket_article
  15. apply_attachments
  16. apply_link_ticket_id
  17. attributes_to_apply.each do |key, value|
  18. apply_value.perform(
  19. field: key,
  20. config: { 'value' => value }
  21. )
  22. end
  23. end
  24. def apply_attachments
  25. new_attachments = UserInfo.with_user_id(context[:current_user].id) do
  26. selected_ticket_article.clone_attachments('UploadCache', meta[:form_id])
  27. end
  28. apply_value.perform(field: 'attachments', config: { 'value' => new_attachments.reject(&:inline?) })
  29. end
  30. def attributes_to_apply
  31. attrs = selected_ticket_article.ticket.attributes
  32. attrs['title'] = selected_ticket_article.subject if selected_ticket_article.subject.present?
  33. attrs['body'] = body_with_form_id_urls
  34. attrs.delete 'owner_id'
  35. attrs
  36. end
  37. def body_with_form_id_urls
  38. cache = UploadCache.new(meta[:form_id])
  39. HasRichText.insert_urls(selected_ticket_article.body_as_html, cache.attachments)
  40. end
  41. def apply_link_ticket_id
  42. apply_value.perform(
  43. field: 'link_ticket_id',
  44. config: { 'value' => selected_ticket_article.ticket.id }
  45. )
  46. end
  47. def selected_ticket_article
  48. @selected_ticket_article ||= begin
  49. gid = meta.dig(:additional_data, 'splitTicketArticleId')
  50. Gql::ZammadSchema.authorized_object_from_id(gid, type: Ticket::Article, user: context[:current_user]) if gid.present?
  51. end
  52. end
  53. def agent?
  54. current_user.permissions?('ticket.agent')
  55. end
  56. end