background_job.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. class Observer::Ticket::UserTicketCounter::BackgroundJob
  2. def initialize(customer_id, updated_by_id)
  3. @customer_id = customer_id
  4. @updated_by_id = updated_by_id
  5. end
  6. def perform
  7. # open ticket count
  8. tickets_open = 0
  9. tickets_closed = 0
  10. if @customer_id != 1
  11. state_open = Ticket::State.by_category(:open)
  12. tickets_open = Ticket.where(
  13. customer_id: @customer_id,
  14. state_id: state_open,
  15. ).count()
  16. # closed ticket count
  17. state_closed = Ticket::State.by_category(:closed)
  18. tickets_closed = Ticket.where(
  19. customer_id: @customer_id,
  20. state_id: state_closed,
  21. ).count()
  22. end
  23. # check if update is needed
  24. customer = User.lookup(id: @customer_id)
  25. return true if !customer
  26. need_update = false
  27. if customer[:preferences][:tickets_open] != tickets_open
  28. need_update = true
  29. customer[:preferences][:tickets_open] = tickets_open
  30. end
  31. if customer[:preferences][:tickets_closed] != tickets_closed
  32. need_update = true
  33. customer[:preferences][:tickets_closed] = tickets_closed
  34. end
  35. return true if !need_update
  36. customer.updated_by_id = @updated_by_id
  37. customer.save
  38. end
  39. end