create.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (C) 2012-2024 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. preprocess_ticket_data! ticket_data
  10. Ticket.new(ticket_data).tap do |ticket|
  11. Pundit.authorize current_user, ticket, :create?
  12. ticket.save!
  13. create_article(ticket, article_data)
  14. assign_tags(ticket, tag_data)
  15. end
  16. end
  17. end
  18. private
  19. def create_article(ticket, article_data)
  20. return if article_data.blank?
  21. preprocess_article_data! ticket, article_data
  22. Service::Ticket::Article::Create
  23. .new(current_user: current_user)
  24. .execute(article_data: article_data, ticket: ticket)
  25. end
  26. def assign_tags(ticket, tag_data)
  27. return if tag_data.blank?
  28. tag_data.each do |tag|
  29. next if !::Tag.tag_allowed?(name: tag.strip, user_id: current_user.id)
  30. ticket.tag_add(tag.strip)
  31. end
  32. end
  33. # Desktop UI supplies this data from frontend
  34. # Mobile UI leaves this processing for GraphQL
  35. def preprocess_ticket_data!(ticket_data)
  36. return if !customer?(ticket_data[:group]&.id)
  37. ticket_data[:customer_id] = current_user.id
  38. end
  39. # Desktop UI supplies this data from frontend
  40. # Mobile UI leaves this processing for GraphQL
  41. def preprocess_article_data!(ticket, article_input)
  42. if customer? ticket.group_id
  43. preprocess_permission_customer! ticket, article_input
  44. return
  45. end
  46. case article_input[:sender]
  47. when 'Customer'
  48. preprocess_article_data_customer! ticket, article_input
  49. when 'Agent'
  50. preprocess_article_data_agent! ticket, article_input
  51. end
  52. end
  53. def customer?(group_id)
  54. return if !current_user.permissions?('ticket.customer')
  55. !current_user.group_access?(group_id, :create)
  56. end
  57. def preprocess_permission_customer!(ticket, article_input)
  58. article_input.merge!(
  59. from: current_user.fullname,
  60. to: group_name(ticket)
  61. )
  62. end
  63. def preprocess_article_data_customer!(ticket, article_input)
  64. article_input.merge!(
  65. from: customer_recipient_line(ticket),
  66. to: group_name(ticket)
  67. )
  68. end
  69. def preprocess_article_data_agent!(ticket, article_input)
  70. article_input.merge!(
  71. from: group_name(ticket),
  72. to: customer_recipient_line(ticket)
  73. )
  74. end
  75. def group_name(ticket)
  76. ticket.group&.name || ''
  77. end
  78. def customer_recipient_line(ticket)
  79. customer = ticket.customer
  80. return if !customer
  81. Channel::EmailBuild.recipient_line "#{customer.firstname} #{customer.lastname}".presence, customer.email
  82. end
  83. end