screen_options.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://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. )
  10. or only with user
  11. result = Ticket::ScreenOptions.attributes_to_change(
  12. current_user: User.find(123),
  13. )
  14. returns
  15. result = {
  16. :form_meta => {
  17. :filter => {
  18. :state_id => [1, 2, 4, 7, 3],
  19. :priority_id => [2, 1, 3],
  20. :type_id => [10, 5],
  21. :group_id => [12]
  22. },
  23. },
  24. :dependencies => {
  25. :group_id => {
  26. "" => {
  27. : owner_id => []
  28. },
  29. 12 => {
  30. : owner_id => [4, 5, 6, 7]
  31. }
  32. }
  33. }
  34. =end
  35. def self.attributes_to_change(params)
  36. raise 'current_user param needed' if !params[:current_user]
  37. if params[:ticket].blank? && params[:ticket_id].present?
  38. params[:ticket] = Ticket.find(params[:ticket_id])
  39. end
  40. filter = {}
  41. assets = {}
  42. # get ticket states
  43. state_ids = []
  44. if params[:ticket].present?
  45. state_type = params[:ticket].state.state_type
  46. end
  47. state_types = ['open', 'closed', 'pending action', 'pending reminder']
  48. if state_type && !state_types.include?(state_type.name)
  49. state_ids.push params[:ticket].state_id
  50. end
  51. state_types.each do |type|
  52. state_type = Ticket::StateType.find_by(name: type)
  53. next if !state_type
  54. state_type.states.each do |state|
  55. assets = state.assets(assets)
  56. state_ids.push state.id
  57. end
  58. end
  59. filter[:state_id] = state_ids
  60. # get priorities
  61. priority_ids = []
  62. Ticket::Priority.where(active: true).each do |priority|
  63. assets = priority.assets(assets)
  64. priority_ids.push priority.id
  65. end
  66. filter[:priority_id] = priority_ids
  67. type_ids = []
  68. if params[:ticket]
  69. types = %w[note phone]
  70. if params[:ticket].group.email_address_id
  71. types.push 'email'
  72. end
  73. types.each do |type_name|
  74. type = Ticket::Article::Type.lookup(name: type_name)
  75. next if type.blank?
  76. type_ids.push type.id
  77. end
  78. end
  79. filter[:type_id] = type_ids
  80. # get group / user relations
  81. dependencies = { group_id: { '' => { owner_id: [] } } }
  82. filter[:group_id] = []
  83. groups = if params[:current_user].permissions?('ticket.agent')
  84. params[:current_user].groups_access(%w[create change])
  85. else
  86. Group.where(active: true)
  87. end
  88. agents = {}
  89. agent_role_ids = Role.with_permissions('ticket.agent').pluck(:id)
  90. agent_user_ids = User.joins(:roles).where(users: { active: true }).where('roles_users.role_id IN (?)', agent_role_ids).pluck(:id)
  91. groups.each do |group|
  92. filter[:group_id].push group.id
  93. assets = group.assets(assets)
  94. dependencies[:group_id][group.id] = { owner_id: [] }
  95. 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)
  96. 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)
  97. group_agent_role_user_ids = User.joins(:roles).where(roles: { id: group_agent_roles_ids }).pluck(:id)
  98. User.where(id: group_agent_user_ids.concat(group_agent_role_user_ids).uniq, active: true).pluck(:id).each do |user_id|
  99. dependencies[:group_id][group.id][:owner_id].push user_id
  100. next if agents[user_id]
  101. agents[user_id] = true
  102. next if assets[:User] && assets[:User][user_id]
  103. user = User.lookup(id: user_id)
  104. next if !user
  105. assets = user.assets(assets)
  106. end
  107. end
  108. =begin
  109. # for performance reasons we moved from api calls to optimized sql queries
  110. groups.each do |group|
  111. filter[:group_id].push group.id
  112. assets = group.assets(assets)
  113. dependencies[:group_id][group.id] = { owner_id: [] }
  114. User.group_access(group.id, 'full').each do |user|
  115. dependencies[:group_id][ group.id ][:owner_id].push user.id
  116. next if agents[user.id]
  117. agents[user.id] = true
  118. assets = user.assets(assets)
  119. end
  120. end
  121. =end
  122. {
  123. assets: assets,
  124. form_meta: {
  125. filter: filter,
  126. dependencies: dependencies,
  127. }
  128. }
  129. end
  130. =begin
  131. list tickets by customer groupd in state categroie open and closed
  132. result = Ticket::ScreenOptions.list_by_customer(
  133. customer_id: 123,
  134. limit: 15, # optional, default 15
  135. )
  136. returns
  137. result = {
  138. ticket_ids_open: tickets_open,
  139. ticket_ids_closed: tickets_closed,
  140. assets: { ...list of assets... },
  141. }
  142. =end
  143. def self.list_by_customer(data)
  144. # get closed/open states
  145. state_list_open = Ticket::State.by_category(:open)
  146. state_list_closed = Ticket::State.by_category(:closed)
  147. # get tickets
  148. tickets_open = Ticket.where(
  149. customer_id: data[:customer_id],
  150. state_id: state_list_open
  151. ).limit( data[:limit] || 15 ).order('created_at DESC')
  152. assets = {}
  153. ticket_ids_open = []
  154. tickets_open.each do |ticket|
  155. ticket_ids_open.push ticket.id
  156. assets = ticket.assets(assets)
  157. end
  158. tickets_closed = Ticket.where(
  159. customer_id: data[:customer_id],
  160. state_id: state_list_closed
  161. ).limit( data[:limit] || 15 ).order('created_at DESC')
  162. ticket_ids_closed = []
  163. tickets_closed.each do |ticket|
  164. ticket_ids_closed.push ticket.id
  165. assets = ticket.assets(assets)
  166. end
  167. {
  168. ticket_ids_open: ticket_ids_open,
  169. ticket_ids_closed: ticket_ids_closed,
  170. assets: assets,
  171. }
  172. end
  173. end