overviews.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. updated_at: ...,
  38. },
  39. count: 3,
  40. tickets: [
  41. {
  42. id: 1,
  43. updated_at: ...,
  44. },
  45. {
  46. id: 2,
  47. updated_at: ...,
  48. },
  49. {
  50. id: 3,
  51. updated_at: ...,
  52. }
  53. ],
  54. },
  55. {
  56. ...
  57. }
  58. ]
  59. =end
  60. def self.index(user)
  61. overviews = Ticket::Overviews.all(
  62. current_user: user,
  63. )
  64. return [] if overviews.blank?
  65. # get only tickets with permissions
  66. access_condition = Ticket.access_condition(user, 'overview')
  67. ticket_attributes = Ticket.new.attributes
  68. list = []
  69. overviews.each do |overview|
  70. query_condition, bind_condition, tables = Ticket.selector2sql(overview.condition, user)
  71. direction = overview.order[:direction]
  72. order_by = overview.order[:by]
  73. # validate direction
  74. raise "Invalid order direction '#{direction}'" if direction && direction !~ /^(ASC|DESC)$/i
  75. # check if order by exists
  76. if !ticket_attributes.key?(order_by)
  77. order_by = if ticket_attributes.key?("#{order_by}_id")
  78. "#{order_by}_id"
  79. else
  80. 'created_at'
  81. end
  82. end
  83. order_by = "tickets.#{order_by}"
  84. # check if group by exists
  85. if overview.group_by.present?
  86. group_by = overview.group_by
  87. if !ticket_attributes.key?(group_by)
  88. group_by = if ticket_attributes.key?("#{group_by}_id")
  89. "#{group_by}_id"
  90. end
  91. end
  92. if group_by
  93. order_by = "tickets.#{group_by}, #{order_by}"
  94. end
  95. end
  96. ticket_result = Ticket.distinct
  97. .where(access_condition)
  98. .where(query_condition, *bind_condition)
  99. .joins(tables)
  100. .order("#{order_by} #{direction}")
  101. .limit(2000)
  102. .pluck(:id, :updated_at, order_by)
  103. tickets = ticket_result.map do |ticket|
  104. {
  105. id: ticket[0],
  106. updated_at: ticket[1],
  107. }
  108. end
  109. count = Ticket.distinct.where(access_condition).where(query_condition, *bind_condition).joins(tables).count()
  110. item = {
  111. overview: {
  112. name: overview.name,
  113. id: overview.id,
  114. view: overview.link,
  115. updated_at: overview.updated_at,
  116. },
  117. tickets: tickets,
  118. count: count,
  119. }
  120. list.push item
  121. end
  122. list
  123. end
  124. end