has_escalation_calculation_impact_examples.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'HasEscalationCalculationImpact', :performs_jobs do
  3. before do
  4. queue_adapter.perform_enqueued_jobs = true
  5. queue_adapter.perform_enqueued_at_jobs = true
  6. travel_to(DateTime.parse('2013-03-21 09:30:00 UTC'))
  7. end
  8. context 'when affected Ticket existed' do
  9. subject(:sla) { create(:sla, calendar: calendar, first_response_time: 60, response_time: 180, solution_time: 240) }
  10. let(:calendar) { create(:calendar, :business_hours_9_17) }
  11. let!(:ticket) { create(:ticket) }
  12. it 'calculates escalation_at' do
  13. expect { sla }.to change { ticket.reload.escalation_at }.to eq(ticket.created_at + 1.hour)
  14. end
  15. it 'calculates first_response_escalation_at' do
  16. expect { sla }.to change { ticket.reload.first_response_escalation_at }.to eq(ticket.created_at + 1.hour)
  17. end
  18. it 'calculates update_escalation_at' do
  19. expect { sla }.not_to change { ticket.reload.update_escalation_at }.from nil
  20. end
  21. it 'calculates close_escalation_at' do
  22. expect { sla }.to change { ticket.reload.close_escalation_at }.to eq(ticket.created_at + 4.hours)
  23. end
  24. context 'when SLA is destroyed' do
  25. before do
  26. sla
  27. end
  28. it 'calculates escalation_at' do
  29. expect { sla.reload.destroy }.to change { ticket.reload.escalation_at }.to be_nil
  30. end
  31. end
  32. context 'when SLA gets updated' do
  33. before do
  34. sla
  35. end
  36. def first_response_time_change
  37. sla.update!(first_response_time: 120)
  38. end
  39. it 'calculates escalation_at' do
  40. expect { first_response_time_change }.to change { ticket.reload.escalation_at }.to eq(ticket.created_at + 2.hours)
  41. end
  42. it 'calculates first_response_escalation_at' do
  43. expect { first_response_time_change }.to change { ticket.reload.first_response_escalation_at }.to eq(ticket.created_at + 2.hours)
  44. end
  45. it 'calculates update_escalation_at' do
  46. expect { first_response_time_change }.not_to change { ticket.reload.update_escalation_at }.from nil
  47. end
  48. it 'calculates close_escalation_at' do
  49. expect { first_response_time_change }.not_to change { ticket.reload.close_escalation_at }.from eq(ticket.created_at + 4.hours)
  50. end
  51. end
  52. end
  53. context 'when matching conditions' do
  54. context "when matching indirect via 'is not'" do
  55. subject(:ticket) { create(:ticket, created_at: '2013-03-21 09:30:00 UTC', updated_at: '2013-03-21 09:30:00 UTC') }
  56. let(:calendar) { create(:calendar) }
  57. let(:sla_not_matching) do
  58. create(:sla,
  59. calendar: calendar,
  60. condition: {
  61. 'ticket.priority_id' => {
  62. operator: 'is not',
  63. value: %w[1 2 3],
  64. },
  65. },
  66. first_response_time: 10,
  67. response_time: 20,
  68. solution_time: 300)
  69. end
  70. let(:sla_matching_indirect) do
  71. create(:sla,
  72. calendar: calendar,
  73. condition: {
  74. 'ticket.priority_id' => {
  75. operator: 'is not',
  76. value: '1',
  77. },
  78. },
  79. first_response_time: 120,
  80. response_time: 180,
  81. solution_time: 240)
  82. end
  83. before do
  84. sla_not_matching
  85. sla_matching_indirect
  86. ticket
  87. ticket.reload
  88. end
  89. it 'calculates escalation_at attributes' do
  90. expect(ticket.escalation_at.gmtime.to_s).to eq('2013-03-21 11:30:00 UTC')
  91. expect(ticket.first_response_escalation_at.gmtime.to_s).to eq('2013-03-21 11:30:00 UTC')
  92. expect(ticket.update_escalation_at).to be_nil
  93. expect(ticket.close_escalation_at.gmtime.to_s).to eq('2013-03-21 13:30:00 UTC')
  94. end
  95. end
  96. context 'when matching ticket.priority_id and article.subject' do
  97. subject(:ticket) { create(:ticket, created_at: '2016-03-21 12:30:00 UTC', updated_at: '2016-03-21 12:30:00 UTC') }
  98. let(:calendar) { create(:calendar) }
  99. let(:sla) do
  100. create(:sla,
  101. condition: {
  102. 'ticket.priority_id' => {
  103. operator: 'is',
  104. value: %w[1 2 3],
  105. },
  106. 'article.subject' => {
  107. operator: 'contains',
  108. value: 'SLA TEST',
  109. },
  110. },
  111. calendar: calendar,
  112. first_response_time: 60,
  113. response_time: 120,
  114. solution_time: 180)
  115. end
  116. before do
  117. sla
  118. ticket
  119. create(:'ticket/article', :inbound_email, subject: 'SLA TEST', ticket: ticket, created_at: '2016-03-21 12:30:00 UTC', updated_at: '2016-03-21 12:30:00 UTC')
  120. ticket.reload
  121. end
  122. it 'calculates escalation_at attributes' do
  123. expect(ticket.escalation_at.gmtime.to_s).to eq('2016-03-21 13:30:00 UTC')
  124. expect(ticket.first_response_escalation_at.gmtime.to_s).to eq('2016-03-21 13:30:00 UTC')
  125. expect(ticket.first_response_in_min).to be_nil
  126. expect(ticket.first_response_diff_in_min).to be_nil
  127. expect(ticket.update_escalation_at.gmtime.to_s).to eq('2016-03-21 14:30:00 UTC')
  128. expect(ticket.close_escalation_at.gmtime.to_s).to eq('2016-03-21 15:30:00 UTC')
  129. expect(ticket.close_in_min).to be_nil
  130. expect(ticket.close_diff_in_min).to be_nil
  131. end
  132. end
  133. context 'when matching ticket.priority_id and ticket.title' do
  134. subject(:ticket) { create(:ticket, title: 'SLA TEST', created_at: '2016-03-21 12:30:00 UTC', updated_at: '2016-03-21 12:30:00 UTC') }
  135. let(:calendar) { create(:calendar) }
  136. let(:sla) do
  137. create(:sla,
  138. condition: {
  139. 'ticket.priority_id' => {
  140. operator: 'is',
  141. value: %w[1 2 3],
  142. },
  143. 'ticket.title' => {
  144. operator: 'contains',
  145. value: 'SLA TEST',
  146. },
  147. },
  148. calendar: calendar,
  149. first_response_time: 60,
  150. response_time: 120,
  151. solution_time: 180)
  152. end
  153. before do
  154. sla
  155. ticket
  156. create(:'ticket/article', :inbound_email, ticket: ticket, created_at: '2016-03-21 12:30:00 UTC', updated_at: '2016-03-21 12:30:00 UTC')
  157. ticket.reload
  158. end
  159. it 'calculates escalation_at attributes' do
  160. expect(ticket.escalation_at.gmtime.to_s).to eq('2016-03-21 13:30:00 UTC')
  161. expect(ticket.first_response_escalation_at.gmtime.to_s).to eq('2016-03-21 13:30:00 UTC')
  162. expect(ticket.first_response_in_min).to be_nil
  163. expect(ticket.first_response_diff_in_min).to be_nil
  164. expect(ticket.update_escalation_at.gmtime.to_s).to eq('2016-03-21 14:30:00 UTC')
  165. expect(ticket.close_escalation_at.gmtime.to_s).to eq('2016-03-21 15:30:00 UTC')
  166. expect(ticket.close_in_min).to be_nil
  167. expect(ticket.close_diff_in_min).to be_nil
  168. end
  169. end
  170. context 'when matching ticket.priority_id BUT NOT ticket.title' do
  171. subject(:ticket) { create(:ticket, created_at: '2016-03-21 12:30:00 UTC', updated_at: '2016-03-21 12:30:00 UTC') }
  172. let(:calendar) { create(:calendar) }
  173. let(:sla) do
  174. create(:sla,
  175. condition: {
  176. 'ticket.priority_id' => {
  177. operator: 'is',
  178. value: %w[1 2 3],
  179. },
  180. 'ticket.title' => {
  181. operator: 'contains',
  182. value: 'SLA TEST',
  183. },
  184. },
  185. calendar: calendar,
  186. first_response_time: 60,
  187. response_time: 120,
  188. solution_time: 180)
  189. end
  190. before do
  191. sla
  192. ticket
  193. create(:'ticket/article', :inbound_email, ticket: ticket, created_at: '2016-03-21 12:30:00 UTC', updated_at: '2016-03-21 12:30:00 UTC')
  194. ticket.reload
  195. end
  196. it 'DOES NOT calculate escalation_at attributes' do
  197. expect(ticket.escalation_at).to be_nil
  198. expect(ticket.first_response_escalation_at).to be_nil
  199. expect(ticket.first_response_in_min).to be_nil
  200. expect(ticket.first_response_diff_in_min).to be_nil
  201. expect(ticket.update_escalation_at).to be_nil
  202. expect(ticket.close_escalation_at).to be_nil
  203. expect(ticket.close_in_min).to be_nil
  204. expect(ticket.close_diff_in_min).to be_nil
  205. end
  206. end
  207. end
  208. end