update.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Ticket::Update < Service::BaseWithCurrentUser
  3. include Service::Concerns::HandlesCoreWorkflow
  4. def execute(ticket:, ticket_data:, skip_validators: nil, macro: nil)
  5. Pundit.authorize current_user, ticket, :update?
  6. set_core_workflow_information(ticket_data, ::Ticket, 'edit')
  7. article_data = ticket_data.delete(:article)
  8. validate!(current_user, ticket, ticket_data, article_data, skip_validators)
  9. save_ticket!(ticket, ticket_data, article_data, macro)
  10. ticket
  11. end
  12. private
  13. def save_ticket!(ticket, ticket_data, article_data, macro)
  14. ticket.with_lock do
  15. if macro
  16. save_ticket_attributes_and_apply_macro!(ticket, ticket_data, article_data, macro)
  17. else
  18. save_ticket_attributes!(ticket, ticket_data, article_data)
  19. end
  20. end
  21. end
  22. def save_ticket_attributes!(ticket, ticket_data, article_data)
  23. ticket.update!(ticket_data)
  24. create_article(ticket, article_data)
  25. end
  26. def save_ticket_attributes_and_apply_macro!(ticket, ticket_data, article_data, macro)
  27. ticket.assign_attributes(ticket_data)
  28. ticket.perform_changes(macro, 'macro', ticket, current_user.id) do |object, _save_needed|
  29. object.save!
  30. create_article(ticket, article_data)
  31. end
  32. end
  33. def create_article(ticket, article_data)
  34. return if article_data.blank?
  35. preprocess_article_data! article_data
  36. Service::Ticket::Article::Create
  37. .new(current_user: current_user)
  38. .execute(article_data: article_data, ticket: ticket)
  39. end
  40. # Desktop UI supplies this data from frontend
  41. # Mobile UI leaves this processing for GraphQL
  42. def preprocess_article_data!(article_input)
  43. article_input[:from] = current_user.fullname
  44. end
  45. def validate!(user, ticket, ticket_data, article_data, skip_validators)
  46. Service::Ticket::Update::Validator.new(user:, ticket:, ticket_data:, article_data:, skip_validators:).validate!
  47. end
  48. end