screen_options.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Ticket::ScreenOptions
  3. =begin
  4. list attributes
  5. result = Ticket::ScreenOptions.attributes_to_change(
  6. ticket_id: 123,
  7. ticket: ticket_model,
  8. current_user: User.find(123),
  9. screen: 'create_middle',
  10. )
  11. or only with user
  12. result = Ticket::ScreenOptions.attributes_to_change(
  13. current_user: User.find(123),
  14. )
  15. returns
  16. result = {
  17. :form_meta => {
  18. :filter => {
  19. :state_id => [1, 2, 4, 7, 3],
  20. :priority_id => [2, 1, 3],
  21. :type_id => [10, 5],
  22. :group_id => [12]
  23. },
  24. },
  25. }
  26. =end
  27. def self.attributes_to_change(params)
  28. raise 'current_user param needed' if !params[:current_user]
  29. if params[:ticket].blank? && params[:ticket_id].present?
  30. params[:ticket] = Ticket.find(params[:ticket_id])
  31. end
  32. assets = {}
  33. filter = {}
  34. type_ids = []
  35. if params[:ticket]
  36. types = %w[note phone]
  37. if params[:ticket].group.email_address_id
  38. types.push 'email'
  39. end
  40. types.each do |type_name|
  41. type = Ticket::Article::Type.lookup(name: type_name)
  42. next if type.blank?
  43. type_ids.push type.id
  44. end
  45. end
  46. filter[:type_id] = type_ids
  47. # get group / user relations (for bulk actions)
  48. dependencies = nil
  49. if params[:view] == 'ticket_overview'
  50. dependencies = { group_id: { '' => { owner_id: [] } } }
  51. groups = params[:current_user].groups_access(%w[create])
  52. agents = {}
  53. agent_role_ids = Role.with_permissions('ticket.agent').pluck(:id)
  54. agent_user_ids = User.joins(:roles).where(users: { active: true }).where('roles_users.role_id' => agent_role_ids).pluck(:id)
  55. groups.each do |group|
  56. assets = group.assets(assets)
  57. dependencies[:group_id][group.id] = { owner_id: [] }
  58. group_agent_user_ids = User.joins(', groups_users').where("users.id = groups_users.user_id AND groups_users.access = 'full' AND groups_users.group_id = ? AND users.id IN (?)", group.id, agent_user_ids).pluck(:id)
  59. group_agent_roles_ids = Role.joins(', roles_groups').where("roles.id = roles_groups.role_id AND roles_groups.access = 'full' AND roles_groups.group_id = ? AND roles.id IN (?)", group.id, agent_role_ids).pluck(:id)
  60. group_agent_role_user_ids = User.joins(:roles).where(roles: { id: group_agent_roles_ids }).pluck(:id)
  61. User.where(id: group_agent_user_ids.concat(group_agent_role_user_ids).uniq, active: true).pluck(:id).each do |user_id|
  62. dependencies[:group_id][group.id][:owner_id].push user_id
  63. next if agents[user_id]
  64. agents[user_id] = true
  65. next if assets[:User] && assets[:User][user_id]
  66. user = User.lookup(id: user_id)
  67. next if !user
  68. assets = user.assets(assets)
  69. end
  70. end
  71. end
  72. configure_attributes = nil
  73. if params[:ticket].present?
  74. configure_attributes = ObjectManager::Object.new('Ticket').attributes(params[:current_user], params[:ticket])
  75. end
  76. core_workflow = CoreWorkflow.perform(payload: {
  77. 'event' => 'core_workflow',
  78. 'request_id' => 'default',
  79. 'class_name' => 'Ticket',
  80. 'screen' => params[:screen],
  81. 'params' => Hash(params[:ticket]&.attributes)
  82. }, user: params[:current_user], assets: assets, assets_in_result: false)
  83. {
  84. assets: assets,
  85. form_meta: {
  86. filter: filter,
  87. dependencies: dependencies,
  88. configure_attributes: configure_attributes,
  89. core_workflow: core_workflow
  90. }
  91. }
  92. end
  93. =begin
  94. list tickets by customer group in state categories open and closed
  95. result = Ticket::ScreenOptions.list_by_customer(
  96. customer_id: 123,
  97. limit: 15, # optional, default 15
  98. )
  99. returns
  100. result = {
  101. ticket_ids_open: tickets_open,
  102. ticket_ids_closed: tickets_closed,
  103. assets: { ...list of assets... },
  104. }
  105. =end
  106. def self.list_by_customer(data)
  107. base_query = TicketPolicy::ReadScope.new(data[:current_user]).resolve
  108. .joins(state: :state_type)
  109. .where(customer_id: data[:customer_id])
  110. .limit(data[:limit] || 15)
  111. .order(created_at: :desc)
  112. open_tickets = base_query.where(ticket_state_types: { name: Ticket::State::TYPES[:open] })
  113. closed_tickets = base_query.where(ticket_state_types: { name: Ticket::State::TYPES[:closed] })
  114. {
  115. ticket_ids_open: open_tickets.map(&:id),
  116. ticket_ids_closed: closed_tickets.map(&:id),
  117. assets: (open_tickets | closed_tickets).reduce({}) { |hash, ticket| ticket.assets(hash) },
  118. }
  119. end
  120. end