has_escalation_calculation_impact_examples.rb 8.4 KB

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