has_ticket_contact_attributes_impact_examples.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'Ticket::Article::HasTicketContactAttributesImpact' do
  3. describe '#update_ticket_article_attributes callback' do
  4. subject(:ticket) { create(:ticket, created_at: '2013-03-28 23:49:00 UTC', updated_at: '2013-03-28 23:49:00 UTC') }
  5. let(:calendar) do
  6. create(:calendar,
  7. business_hours: {
  8. mon: {
  9. active: true,
  10. timeframes: [ ['08:00', '18:00'] ]
  11. },
  12. tue: {
  13. active: true,
  14. timeframes: [ ['08:00', '18:00'] ]
  15. },
  16. wed: {
  17. active: true,
  18. timeframes: [ ['08:00', '18:00'] ]
  19. },
  20. thu: {
  21. active: true,
  22. timeframes: [ ['08:00', '18:00'] ]
  23. },
  24. fri: {
  25. active: true,
  26. timeframes: [ ['08:00', '18:00'] ]
  27. },
  28. sat: {
  29. active: false,
  30. timeframes: [ ['08:00', '17:00'] ]
  31. },
  32. sun: {
  33. active: false,
  34. timeframes: [ ['08:00', '17:00'] ]
  35. },
  36. })
  37. end
  38. let(:sla) { create(:sla, calendar: calendar, first_response_time: 60, response_time: 120, solution_time: 180) }
  39. before do
  40. sla
  41. ticket
  42. end
  43. context 'when inbound email Article is created' do
  44. it 'updates ticket article attributes' do
  45. article_inbound = create(:'ticket/article', :inbound_email, ticket: ticket, created_at: '2013-03-28 23:49:00 UTC', updated_at: '2013-03-28 23:49:00 UTC')
  46. ticket.reload
  47. expect(ticket.last_contact_at.to_s).to eq(article_inbound.created_at.to_s)
  48. expect(ticket.last_contact_customer_at.to_s).to eq(article_inbound.created_at.to_s)
  49. expect(ticket.last_contact_agent_at).to be_nil
  50. expect(ticket.first_response_at).to be_nil
  51. expect(ticket.close_at).to be_nil
  52. end
  53. end
  54. context 'when outbound email Article is created in response to inbound email Article' do
  55. it 'updates ticket article attributes' do
  56. article_inbound = create(:'ticket/article', :inbound_email, ticket: ticket, created_at: '2013-03-28 23:49:00 UTC', updated_at: '2013-03-28 23:49:00 UTC')
  57. ticket.reload
  58. article_outbound = create(:'ticket/article', :outbound_email, ticket: ticket, created_at: '2013-03-29 07:00:03 UTC', updated_at: '2013-03-29 07:00:03 UTC')
  59. ticket.reload
  60. expect(ticket.last_contact_at.to_s).to eq(article_outbound.created_at.to_s)
  61. expect(ticket.last_contact_customer_at.to_s).to eq(article_inbound.created_at.to_s)
  62. expect(ticket.last_contact_agent_at.to_s).to eq(article_outbound.created_at.to_s)
  63. expect(ticket.first_response_at.to_s).to eq(article_outbound.created_at.to_s)
  64. expect(ticket.first_response_in_min).to eq(0)
  65. expect(ticket.first_response_diff_in_min).to eq(60)
  66. expect(ticket.close_at).to be_nil
  67. end
  68. end
  69. context 'when inbound phone Article is created' do
  70. it 'updates ticket article attributes' do
  71. article_inbound = create(:'ticket/article', :inbound_phone, ticket: ticket, created_at: '2013-03-28 23:49:00 UTC', updated_at: '2013-03-28 23:49:00 UTC')
  72. ticket.reload
  73. expect(ticket.last_contact_at.to_s).to eq(article_inbound.created_at.to_s)
  74. expect(ticket.last_contact_customer_at.to_s).to eq(article_inbound.created_at.to_s)
  75. expect(ticket.last_contact_agent_at).to be_nil
  76. expect(ticket.first_response_at).to be_nil
  77. expect(ticket.close_at).to be_nil
  78. end
  79. end
  80. context 'when outbound note Article is created in response to inbound phone Article' do
  81. it 'updates ticket article attributes' do
  82. article_inbound = create(:'ticket/article', :inbound_phone, ticket: ticket, created_at: '2013-03-28 23:49:00 UTC', updated_at: '2013-03-28 23:49:00 UTC')
  83. ticket.reload
  84. create(:'ticket/article', :outbound_note, ticket: ticket, created_at: '2013-03-28 23:52:00 UTC', updated_at: '2013-03-28 23:52:00 UTC')
  85. ticket.reload
  86. expect(ticket.last_contact_at.to_s).to eq(article_inbound.created_at.to_s)
  87. expect(ticket.last_contact_customer_at.to_s).to eq(article_inbound.created_at.to_s)
  88. expect(ticket.last_contact_agent_at).to be_nil
  89. expect(ticket.first_response_at).to be_nil
  90. expect(ticket.close_at).to be_nil
  91. end
  92. end
  93. context 'when outbound phone Article is created after outbound note Article is created in response to inbound phone Article' do
  94. it 'updates ticket article attributes' do
  95. article_inbound = create(:'ticket/article', :inbound_phone, ticket: ticket, created_at: '2013-03-28 23:49:00 UTC', updated_at: '2013-03-28 23:49:00 UTC')
  96. ticket.reload
  97. create(:'ticket/article', :outbound_note, ticket: ticket, created_at: '2013-03-28 23:52:00 UTC', updated_at: '2013-03-28 23:52:00 UTC')
  98. ticket.reload
  99. article_outbound = create(:'ticket/article', :outbound_phone, ticket: ticket, created_at: '2013-03-28 23:55:00 UTC', updated_at: '2013-03-28 23:55:00 UTC')
  100. ticket.reload
  101. expect(ticket.last_contact_at.to_s).to eq(article_outbound.created_at.to_s)
  102. expect(ticket.last_contact_customer_at.to_s).to eq(article_inbound.created_at.to_s)
  103. expect(ticket.last_contact_agent_at.to_s).to eq(article_outbound.created_at.to_s)
  104. expect(ticket.first_response_at.to_s).to eq(article_outbound.created_at.to_s)
  105. expect(ticket.close_at).to be_nil
  106. end
  107. end
  108. context 'when inbound web Article is created' do
  109. subject(:ticket) { create(:ticket, created_at: '2016-11-01 13:56:21 UTC', updated_at: '2016-11-01 13:56:21 UTC') }
  110. let(:calendar) do
  111. create(:calendar,
  112. business_hours: {
  113. mon: {
  114. active: true,
  115. timeframes: [ ['08:00', '20:00'] ]
  116. },
  117. tue: {
  118. active: true,
  119. timeframes: [ ['08:00', '20:00'] ]
  120. },
  121. wed: {
  122. active: true,
  123. timeframes: [ ['08:00', '20:00'] ]
  124. },
  125. thu: {
  126. active: true,
  127. timeframes: [ ['08:00', '20:00'] ]
  128. },
  129. fri: {
  130. active: true,
  131. timeframes: [ ['08:00', '20:00'] ]
  132. },
  133. sat: {
  134. active: false,
  135. timeframes: [ ['08:00', '17:00'] ]
  136. },
  137. sun: {
  138. active: false,
  139. timeframes: [ ['08:00', '17:00'] ]
  140. },
  141. },
  142. public_holidays: {
  143. '2016-11-01' => {
  144. 'active' => true,
  145. 'summary' => 'test 1',
  146. },
  147. })
  148. end
  149. let(:sla) { create(:sla, condition: {}, calendar: calendar, first_response_time: 60, response_time: 120, solution_time: nil) }
  150. before do
  151. sla
  152. ticket
  153. create(:'ticket/article', :inbound_web, ticket: ticket, created_at: '2016-11-01 13:56:21 UTC', updated_at: '2016-11-01 13:56:21 UTC')
  154. ticket.reload
  155. end
  156. it 'calculates escalation_at attributes' do
  157. expect(ticket.escalation_at.gmtime.to_s).to eq('2016-11-02 08:00:00 UTC')
  158. expect(ticket.first_response_escalation_at.gmtime.to_s).to eq('2016-11-02 08:00:00 UTC')
  159. expect(ticket.update_escalation_at.gmtime.to_s).to eq('2016-11-02 09:00:00 UTC')
  160. expect(ticket.close_escalation_at).to be_nil
  161. end
  162. context 'when replied via outbound email' do
  163. before do
  164. create(:'ticket/article', :outbound_email, ticket: ticket, created_at: '2016-11-07 13:26:36 UTC', updated_at: '2016-11-07 13:26:36 UTC')
  165. ticket.reload
  166. end
  167. it 'calculates escalation_at attributes' do
  168. expect(ticket.escalation_at).to be_nil
  169. expect(ticket.first_response_escalation_at).to be_nil
  170. expect(ticket.update_escalation_at).to be_nil
  171. expect(ticket.close_escalation_at).to be_nil
  172. end
  173. end
  174. end
  175. context "when Setting 'ticket_last_contact_behaviour' is set to 'based_on_customer_reaction'" do
  176. subject(:ticket) { create(:ticket, created_at: '2018-05-01 13:56:21 UTC', updated_at: '2018-05-01 13:56:21 UTC') }
  177. let(:calendar) do
  178. create(:calendar,
  179. business_hours: {
  180. mon: {
  181. active: true,
  182. timeframes: [ ['08:00', '20:00'] ]
  183. },
  184. tue: {
  185. active: true,
  186. timeframes: [ ['08:00', '20:00'] ]
  187. },
  188. wed: {
  189. active: true,
  190. timeframes: [ ['08:00', '20:00'] ]
  191. },
  192. thu: {
  193. active: true,
  194. timeframes: [ ['08:00', '20:00'] ]
  195. },
  196. fri: {
  197. active: true,
  198. timeframes: [ ['08:00', '20:00'] ]
  199. },
  200. sat: {
  201. active: false,
  202. timeframes: [ ['08:00', '17:00'] ]
  203. },
  204. sun: {
  205. active: false,
  206. timeframes: [ ['08:00', '17:00'] ]
  207. },
  208. },
  209. public_holidays: {
  210. '2016-11-01' => {
  211. 'active' => true,
  212. 'summary' => 'test 1',
  213. },
  214. })
  215. end
  216. let(:sla) { create(:sla, condition: {}, calendar: calendar, first_response_time: 60, response_time: 120, solution_time: nil) }
  217. before do
  218. Setting.set('ticket_last_contact_behaviour', 'based_on_customer_reaction')
  219. sla
  220. end
  221. it 'updates ticket article attributes' do
  222. ticket
  223. article = create(:'ticket/article', :inbound_phone, ticket: ticket, created_at: '2018-05-01 13:56:21 UTC', updated_at: '2018-05-01 13:56:21 UTC')
  224. ticket.reload
  225. expect(ticket.last_contact_at.to_s).to eq(article.created_at.to_s)
  226. expect(ticket.last_contact_customer_at.to_s).to eq(article.created_at.to_s)
  227. expect(ticket.last_contact_agent_at).to be_nil
  228. expect(ticket.first_response_at).to be_nil
  229. expect(ticket.close_at).to be_nil
  230. article = create(:'ticket/article', :inbound_phone, ticket: ticket, created_at: '2018-05-01 14:56:21 UTC', updated_at: '2018-05-01 14:56:21 UTC')
  231. ticket.reload
  232. expect(ticket.last_contact_at.to_s).to eq(article.created_at.to_s)
  233. expect(ticket.last_contact_customer_at.to_s).to eq(article.created_at.to_s)
  234. expect(ticket.last_contact_agent_at).to be_nil
  235. expect(ticket.first_response_at).to be_nil
  236. expect(ticket.close_at).to be_nil
  237. article_customer = create(:'ticket/article', :inbound_phone, ticket: ticket, created_at: '2018-05-01 15:56:21 UTC', updated_at: '2018-05-01 15:56:21 UTC')
  238. ticket.reload
  239. expect(ticket.last_contact_at.to_s).to eq(article_customer.created_at.to_s)
  240. expect(ticket.last_contact_customer_at.to_s).to eq(article_customer.created_at.to_s)
  241. expect(ticket.last_contact_agent_at).to be_nil
  242. expect(ticket.first_response_at).to be_nil
  243. expect(ticket.close_at).to be_nil
  244. article_agent = create(:'ticket/article', :outbound_phone, ticket: ticket, created_at: '2018-05-01 16:56:21 UTC', updated_at: '2018-05-01 16:56:21 UTC')
  245. ticket.reload
  246. expect(ticket.last_contact_at.to_s).to eq(article_agent.created_at.to_s)
  247. expect(ticket.last_contact_customer_at.to_s).to eq(article_customer.created_at.to_s)
  248. expect(ticket.last_contact_agent_at.to_s).to eq(article_agent.created_at.to_s)
  249. expect(ticket.first_response_at.to_s).to eq(article_agent.created_at.to_s)
  250. expect(ticket.close_at).to be_nil
  251. end
  252. end
  253. end
  254. end