ticket_user_ticket_counter_job.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # This job counts *all* user tickets and stores the count in the user preferences
  3. # It is very similar to what Gql::Types::TicketCountType does but not the same!
  4. # Results of this job are used exclusively in the old UI
  5. class TicketUserTicketCounterJob < ApplicationJob
  6. include HasActiveJobLock
  7. def lock_key
  8. # "TicketUserTicketCounterJob/23/42"
  9. "#{self.class.name}/#{arguments[0]}/#{arguments[1]}"
  10. end
  11. def perform(customer_id, updated_by_id)
  12. # check if update is needed
  13. customer = User.lookup(id: customer_id)
  14. return if !customer
  15. # count open and closed tickets of customer
  16. ticket_count = {
  17. closed: 0,
  18. open: 0,
  19. }
  20. if customer_id != 1
  21. ticket_count.each_key do |ticket_state_category|
  22. ticket_states = Ticket::State.by_category(ticket_state_category)
  23. ticket_state_ids = ticket_states.map(&:id)
  24. tickets = Ticket.where(
  25. customer_id: customer_id,
  26. state_id: ticket_state_ids,
  27. )
  28. ticket_count[ticket_state_category] = tickets.count
  29. end
  30. end
  31. needs_update = false
  32. ticket_count.each_key do |ticket_state_category|
  33. preferences_key = :"tickets_#{ticket_state_category}"
  34. next if customer[:preferences][preferences_key] == ticket_count[ticket_state_category]
  35. needs_update = true
  36. customer[:preferences][preferences_key] = ticket_count[ticket_state_category]
  37. end
  38. return if !needs_update
  39. customer.updated_by_id = updated_by_id
  40. customer.save
  41. end
  42. end