tickets.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class CalendarSubscriptions::Tickets
  3. def initialize(user, preferences, time_zone)
  4. @user = user
  5. @preferences = preferences
  6. @time_zone = time_zone
  7. end
  8. def all
  9. events_data = []
  10. return events_data if @preferences.blank?
  11. events_data += new_open
  12. events_data += pending
  13. events_data += escalation
  14. events_data
  15. end
  16. def alarm?
  17. alarm = false
  18. return alarm if @preferences.blank?
  19. return alarm if !@preferences[:alarm]
  20. @preferences[:alarm]
  21. end
  22. def owner_ids(method)
  23. owner_ids = []
  24. return owner_ids if @preferences.blank?
  25. return owner_ids if !@preferences[method]
  26. return owner_ids if @preferences[method].blank?
  27. preferences = @preferences[method]
  28. if preferences[:own]
  29. owner_ids = [ @user.id ]
  30. end
  31. if preferences[:not_assigned]
  32. owner_ids.push(1)
  33. end
  34. owner_ids
  35. end
  36. def new_open
  37. events_data = []
  38. owner_ids = owner_ids(:new_open)
  39. return events_data if owner_ids.blank?
  40. condition = {
  41. 'ticket.owner_id' => {
  42. operator: 'is',
  43. value: owner_ids,
  44. },
  45. 'ticket.state_id' => {
  46. operator: 'is',
  47. value: Ticket::State.where(
  48. state_type_id: Ticket::StateType.where(
  49. name: %w[new open],
  50. ),
  51. ).map(&:id),
  52. },
  53. }
  54. tickets = Ticket.search(
  55. current_user: @user,
  56. condition: condition,
  57. )
  58. user_locale = @user.locale
  59. translated_ticket = Translation.translate(user_locale, 'ticket')
  60. events_data = []
  61. tickets.each do |ticket|
  62. event_data = {}
  63. translated_state = Translation.translate(user_locale, ticket.state.name)
  64. event_data[:dtstart] = Icalendar::Values::Date.new(Time.zone.today, 'tzid' => @time_zone)
  65. event_data[:dtend] = Icalendar::Values::Date.new(Time.zone.today, 'tzid' => @time_zone)
  66. event_data[:summary] = "#{translated_state} #{translated_ticket}: '#{ticket.title}'"
  67. event_data[:description] = "T##{ticket.number}"
  68. event_data[:type] = 'new_open'
  69. events_data.push event_data
  70. end
  71. events_data
  72. end
  73. def pending
  74. events_data = []
  75. owner_ids = owner_ids(:pending)
  76. return events_data if owner_ids.blank?
  77. condition = {
  78. 'ticket.owner_id' => {
  79. operator: 'is',
  80. value: owner_ids,
  81. },
  82. 'ticket.state_id' => {
  83. operator: 'is',
  84. value: Ticket::State.where(
  85. state_type_id: Ticket::StateType.where(
  86. name: [
  87. 'pending reminder',
  88. 'pending action',
  89. ],
  90. ),
  91. ).map(&:id),
  92. },
  93. }
  94. tickets = Ticket.search(
  95. current_user: @user,
  96. condition: condition,
  97. )
  98. user_locale = @user.locale
  99. translated_ticket = Translation.translate(user_locale, 'ticket')
  100. customer = Translation.translate(user_locale, 'customer')
  101. events_data = []
  102. tickets.each do |ticket|
  103. next if !ticket.pending_time
  104. event_data = {}
  105. pending_time = ticket.pending_time
  106. if pending_time < Time.zone.today
  107. pending_time = Time.zone.today
  108. end
  109. translated_state = Translation.translate(user_locale, ticket.state.name)
  110. event_data[:dtstart] = Icalendar::Values::DateTime.new(pending_time, 'tzid' => @time_zone)
  111. event_data[:dtend] = Icalendar::Values::DateTime.new(pending_time, 'tzid' => @time_zone)
  112. event_data[:summary] = "#{translated_state} #{translated_ticket}: '#{ticket.title}' #{customer}: #{ticket.customer.longname}"
  113. event_data[:description] = "T##{ticket.number}"
  114. if alarm?
  115. event_data[:alarm] = {
  116. summary: event_data[:summary],
  117. trigger: '-PT1M',
  118. }
  119. end
  120. event_data[:type] = 'pending'
  121. events_data.push event_data
  122. end
  123. events_data
  124. end
  125. def escalation
  126. events_data = []
  127. owner_ids = owner_ids(:escalation)
  128. return events_data if owner_ids.blank?
  129. condition = {
  130. 'ticket.owner_id' => {
  131. operator: 'is',
  132. value: owner_ids,
  133. },
  134. 'ticket.escalation_at' => {
  135. operator: 'is not',
  136. value: nil,
  137. }
  138. }
  139. tickets = Ticket.search(
  140. current_user: @user,
  141. condition: condition,
  142. )
  143. user_locale = @user.locale
  144. translated_ticket_escalation = Translation.translate(user_locale, 'ticket escalation')
  145. customer = Translation.translate(user_locale, 'customer')
  146. tickets.each do |ticket|
  147. next if !ticket.escalation_at
  148. event_data = {}
  149. escalation_at = ticket.escalation_at
  150. if escalation_at < Time.zone.today
  151. escalation_at = Time.zone.today
  152. end
  153. event_data[:dtstart] = Icalendar::Values::DateTime.new(escalation_at, 'tzid' => @time_zone)
  154. event_data[:dtend] = Icalendar::Values::DateTime.new(escalation_at, 'tzid' => @time_zone)
  155. event_data[:summary] = "#{translated_ticket_escalation}: '#{ticket.title}' #{customer}: #{ticket.customer.longname}"
  156. event_data[:description] = "T##{ticket.number}"
  157. if alarm?
  158. event_data[:alarm] = {
  159. summary: event_data[:summary],
  160. trigger: '-PT1M',
  161. }
  162. end
  163. event_data[:type] = 'escalated'
  164. events_data.push event_data
  165. end
  166. events_data
  167. end
  168. end