has_escalation_calculation_impact_examples.rb 8.3 KB

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