2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2018-12-21 16:35:15 +00:00
|
|
|
class TicketUserTicketCounterJob < ApplicationJob
|
2019-11-13 07:03:47 +00:00
|
|
|
include HasActiveJobLock
|
|
|
|
|
|
|
|
def lock_key
|
|
|
|
# "TicketUserTicketCounterJob/23/42"
|
|
|
|
"#{self.class.name}/#{arguments[0]}/#{arguments[1]}"
|
|
|
|
end
|
|
|
|
|
2018-12-21 16:35:15 +00:00
|
|
|
def perform(customer_id, updated_by_id)
|
|
|
|
|
|
|
|
# check if update is needed
|
|
|
|
customer = User.lookup(id: customer_id)
|
|
|
|
return if !customer
|
|
|
|
|
|
|
|
# count open and closed tickets of customer
|
|
|
|
ticket_count = {
|
|
|
|
closed: 0,
|
|
|
|
open: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
if customer_id != 1
|
|
|
|
ticket_count.each_key do |ticket_state_category|
|
|
|
|
ticket_states = Ticket::State.by_category(ticket_state_category)
|
|
|
|
ticket_state_ids = ticket_states.map(&:id)
|
|
|
|
tickets = Ticket.where(
|
|
|
|
customer_id: customer_id,
|
|
|
|
state_id: ticket_state_ids,
|
|
|
|
)
|
|
|
|
ticket_count[ticket_state_category] = tickets.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
needs_update = false
|
|
|
|
ticket_count.each_key do |ticket_state_category|
|
2020-09-30 09:07:01 +00:00
|
|
|
preferences_key = :"tickets_#{ticket_state_category}"
|
2018-12-21 16:35:15 +00:00
|
|
|
next if customer[:preferences][preferences_key] == ticket_count[ticket_state_category]
|
|
|
|
|
|
|
|
needs_update = true
|
|
|
|
customer[:preferences][preferences_key] = ticket_count[ticket_state_category]
|
|
|
|
end
|
|
|
|
return if !needs_update
|
|
|
|
|
|
|
|
customer.updated_by_id = updated_by_id
|
|
|
|
customer.save
|
|
|
|
end
|
|
|
|
end
|