create.rb 1016 B

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Ticket::Create < Service::BaseWithCurrentUser
  3. include Service::Concerns::HandlesCoreWorkflow
  4. def execute(ticket_data:)
  5. Transaction.execute do
  6. set_core_workflow_information(ticket_data, ::Ticket, 'create_middle')
  7. article_data = ticket_data.delete(:article)
  8. tag_data = ticket_data.delete(:tags)
  9. Ticket.new(ticket_data).tap do |ticket|
  10. Pundit.authorize current_user, ticket, :create?
  11. ticket.save!
  12. create_article(ticket, article_data)
  13. assign_tags(ticket, tag_data)
  14. end
  15. end
  16. end
  17. private
  18. def create_article(ticket, article_data)
  19. return if article_data.blank?
  20. Service::Ticket::Article::Create.new(current_user: current_user).execute(article_data: article_data.to_h.merge!(ticket_id: ticket.id))
  21. end
  22. def assign_tags(ticket, tag_data)
  23. return if tag_data.blank?
  24. tag_data.each { |tag| ticket.tag_add(tag.strip) }
  25. end
  26. end