karma.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Transaction::Karma
  3. =begin
  4. {
  5. object: 'Ticket',
  6. type: 'update',
  7. object_id: 123,
  8. interface_handle: 'application_server', # application_server|websocket|scheduler
  9. changes: {
  10. 'attribute1' => [before, now],
  11. 'attribute2' => [before, now],
  12. },
  13. created_at: Time.zone.now,
  14. user_id: 123,
  15. },
  16. =end
  17. def initialize(item, params = {})
  18. @item = item
  19. @params = params
  20. end
  21. def perform
  22. # return if we run import mode
  23. return if Setting.get('import_mode')
  24. user = User.lookup(id: @item[:user_id])
  25. # ticket
  26. if @item[:object] == 'Ticket'
  27. ticket_karma(user)
  28. ticket_article_karma(user)
  29. end
  30. # add tagging (check last action time)
  31. if @item[:object] == 'Tag'
  32. tagging(user)
  33. end
  34. true
  35. end
  36. def ticket_karma(user)
  37. ticket = Ticket.lookup(id: @item[:object_id])
  38. return if !ticket
  39. if @item[:type] == 'create'
  40. Karma::ActivityLog.add('ticket create', user, 'Ticket', ticket.id)
  41. return
  42. end
  43. if @item[:type] == 'reminder_reached'
  44. return if ticket.owner_id == 1
  45. return if ticket.pending_time && ticket.pending_time > Time.zone.now - 2.days
  46. Karma::ActivityLog.add('ticket reminder overdue (+2 days)', ticket.owner, 'Ticket', ticket.id)
  47. return
  48. end
  49. if @item[:type] == 'escalation'
  50. return if ticket.owner_id == 1
  51. Karma::ActivityLog.add('ticket escalated', ticket.owner, 'Ticket', ticket.id)
  52. return
  53. end
  54. return if @item[:type] != 'update'
  55. return if !@item[:changes]
  56. return if !@item[:changes]['state_id']
  57. state_before = Ticket::State.lookup(id: @item[:changes]['state_id'][0])
  58. state_now = Ticket::State.lookup(id: @item[:changes]['state_id'][1])
  59. # close
  60. if state_before.state_type.name != 'closed' && state_now.state_type.name == 'closed'
  61. # did user send a response to customer before?
  62. current_time = @item[:created_at]
  63. ticket.articles.reverse.each do |local_article|
  64. next if local_article.created_at > current_time
  65. next if local_article.created_by_id != @item[:user_id]
  66. next if local_article.internal
  67. local_type = Ticket::Article::Type.lookup(id: local_article.type_id)
  68. return false if !local_type
  69. next if !local_type.communication
  70. local_sender = Ticket::Article::Sender.lookup(id: local_article.sender_id)
  71. return false if !local_sender
  72. Karma::ActivityLog.add('ticket close', user, 'Ticket', ticket.id)
  73. break
  74. end
  75. end
  76. # pending state
  77. if (!state_before.next_state_id && state_before.state_type.name != 'pending reminder') && (state_now.next_state_id || state_now.state_type.name == 'pending reminder')
  78. Karma::ActivityLog.add('ticket pending state', user, 'Ticket', ticket.id)
  79. end
  80. true
  81. end
  82. def ticket_article_karma(user)
  83. return if !@item[:article_id]
  84. article = Ticket::Article.lookup(id: @item[:article_id])
  85. return if !article
  86. # get sender
  87. sender = Ticket::Article::Sender.lookup(id: article.sender_id)
  88. return if !sender
  89. return if sender.name != 'Agent'
  90. # get type
  91. type = Ticket::Article::Type.lookup(id: article.type_id)
  92. return if !type
  93. return if !type.communication
  94. ### answer sent (check last action time / within what time?)
  95. articles = Ticket::Article.where(ticket_id: article.ticket_id).order(created_at: :asc, id: :asc)
  96. if articles.count > 1
  97. last_sender_customer = nil
  98. last_customer_contact = nil
  99. articles.each do |local_article|
  100. next if local_article.id == article.id
  101. next if local_article.internal
  102. local_type = Ticket::Article::Type.lookup(id: local_article.type_id)
  103. return false if !local_type
  104. next if !local_type.communication
  105. local_sender = Ticket::Article::Sender.lookup(id: local_article.sender_id)
  106. return false if !local_sender
  107. next if local_sender.name == 'System'
  108. last_sender_customer = local_sender.name == 'Customer'
  109. next if local_sender.name != 'Customer'
  110. last_customer_contact = local_article.created_at
  111. end
  112. if last_sender_customer && last_customer_contact
  113. diff = article.created_at - last_customer_contact
  114. if diff >= 0
  115. if diff < 1.hour
  116. Karma::ActivityLog.add('ticket answer 1h', user, 'Ticket', @item[:object_id])
  117. elsif diff < 2.hours
  118. Karma::ActivityLog.add('ticket answer 2h', user, 'Ticket', @item[:object_id])
  119. elsif diff < 12.hours
  120. Karma::ActivityLog.add('ticket answer 12h', user, 'Ticket', @item[:object_id])
  121. elsif diff < 24.hours
  122. Karma::ActivityLog.add('ticket answer 24h', user, 'Ticket', @item[:object_id])
  123. end
  124. end
  125. end
  126. end
  127. ### text module
  128. if article.preferences[:text_module_ids].present?
  129. Karma::ActivityLog.add('text module', user, 'Ticket', @item[:object_id])
  130. end
  131. true
  132. end
  133. def tagging(user)
  134. return if @item[:type] != 'create'
  135. tag = Tag.lookup(id: @item[:object_id])
  136. return if !tag
  137. Karma::ActivityLog.add('tagging', user, tag.tag_object.name, tag.o_id)
  138. end
  139. end