overviews.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module Ticket::Overviews
  3. =begin
  4. all overviews by user
  5. result = Ticket::Overviews.all(current_user: User.find(3))
  6. returns
  7. result = [overview1, overview2]
  8. =end
  9. def self.all(data)
  10. current_user = data[:current_user]
  11. # get customer overviews
  12. role_ids = User.joins(:roles).where(users: { id: current_user.id, active: true }, roles: { active: true }).pluck('roles.id')
  13. if current_user.permissions?('ticket.customer')
  14. overview_filter = { active: true, organization_shared: false }
  15. if current_user.organization_id && current_user.organization.shared
  16. overview_filter.delete(:organization_shared)
  17. end
  18. overviews = Overview.joins(:roles).left_joins(:users).where(overviews_roles: { role_id: role_ids }, overviews_users: { user_id: nil }, overviews: overview_filter).or(Overview.joins(:roles).left_joins(:users).where(overviews_roles: { role_id: role_ids }, overviews_users: { user_id: current_user.id }, overviews: overview_filter)).distinct('overview.id').order(:prio, :name)
  19. return overviews
  20. end
  21. # get agent overviews
  22. return [] if !current_user.permissions?('ticket.agent')
  23. overview_filter = { active: true }
  24. overview_filter_not = { out_of_office: true }
  25. if User.where('out_of_office = ? AND out_of_office_start_at <= ? AND out_of_office_end_at >= ? AND out_of_office_replacement_id = ? AND active = ?', true, Time.zone.today, Time.zone.today, current_user.id, true).count.positive?
  26. overview_filter_not = {}
  27. end
  28. Overview.joins(:roles).left_joins(:users).where(overviews_roles: { role_id: role_ids }, overviews_users: { user_id: nil }, overviews: overview_filter).or(Overview.joins(:roles).left_joins(:users).where(overviews_roles: { role_id: role_ids }, overviews_users: { user_id: current_user.id }, overviews: overview_filter)).where.not(overview_filter_not).distinct('overview.id').order(:prio, :name)
  29. end
  30. =begin
  31. result = Ticket::Overviews.index(User.find(123))
  32. returns
  33. [
  34. {
  35. overview: {
  36. id: 123,
  37. name: 'some name',
  38. view: 'some_view',
  39. updated_at: ...,
  40. },
  41. count: 3,
  42. tickets: [
  43. {
  44. id: 1,
  45. updated_at: ...,
  46. },
  47. {
  48. id: 2,
  49. updated_at: ...,
  50. },
  51. {
  52. id: 3,
  53. updated_at: ...,
  54. }
  55. ],
  56. },
  57. {
  58. ...
  59. }
  60. ]
  61. =end
  62. def self.index(user)
  63. overviews = Ticket::Overviews.all(
  64. current_user: user,
  65. )
  66. return [] if overviews.blank?
  67. # get only tickets with permissions
  68. access_condition = Ticket.access_condition(user, 'overview')
  69. ticket_attributes = Ticket.new.attributes
  70. list = []
  71. overviews.each do |overview|
  72. query_condition, bind_condition, tables = Ticket.selector2sql(overview.condition, user)
  73. direction = overview.order[:direction]
  74. order_by = overview.order[:by]
  75. # validate direction
  76. raise "Invalid order direction '#{direction}'" if direction && direction !~ /^(ASC|DESC)$/i
  77. # check if order by exists
  78. if !ticket_attributes.key?(order_by)
  79. order_by = if ticket_attributes.key?("#{order_by}_id")
  80. "#{order_by}_id"
  81. else
  82. 'created_at'
  83. end
  84. end
  85. order_by = "#{ActiveRecord::Base.connection.quote_table_name('tickets')}.#{ActiveRecord::Base.connection.quote_column_name(order_by)}"
  86. # check if group by exists
  87. if overview.group_by.present?
  88. group_by = overview.group_by
  89. if !ticket_attributes.key?(group_by)
  90. group_by = if ticket_attributes.key?("#{group_by}_id")
  91. "#{group_by}_id"
  92. end
  93. end
  94. if group_by
  95. order_by = "#{ActiveRecord::Base.connection.quote_table_name('tickets')}.#{ActiveRecord::Base.connection.quote_column_name(group_by)}, #{order_by}"
  96. end
  97. end
  98. ticket_result = Ticket.distinct
  99. .where(access_condition)
  100. .where(query_condition, *bind_condition)
  101. .joins(tables)
  102. .order("#{order_by} #{direction}")
  103. .limit(2000)
  104. .pluck(:id, :updated_at, order_by)
  105. tickets = ticket_result.map do |ticket|
  106. {
  107. id: ticket[0],
  108. updated_at: ticket[1],
  109. }
  110. end
  111. count = Ticket.distinct.where(access_condition).where(query_condition, *bind_condition).joins(tables).count()
  112. item = {
  113. overview: {
  114. name: overview.name,
  115. id: overview.id,
  116. view: overview.link,
  117. updated_at: overview.updated_at,
  118. },
  119. tickets: tickets,
  120. count: count,
  121. }
  122. list.push item
  123. end
  124. list
  125. end
  126. end