Refactoring: Migrated TicketUserTicketCounterJob from Delayed::Job to Active Job.

This commit is contained in:
Jens Pfeifer 2018-12-21 17:35:15 +01:00 committed by Thorsten Eckel
parent 953d902a74
commit 21f1841f5b
4 changed files with 82 additions and 51 deletions

View file

@ -0,0 +1,39 @@
class TicketUserTicketCounterJob < ApplicationJob
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|
preferences_key = ('tickets_' + ticket_state_category.to_s).to_sym
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

View file

@ -17,11 +17,9 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
return true if !record.customer_id
# send background job
Delayed::Job.enqueue(
Observer::Ticket::UserTicketCounter::BackgroundJob.new(
record.customer_id,
UserInfo.current_user_id || record.updated_by_id,
)
TicketUserTicketCounterJob.perform_later(
record.customer_id,
UserInfo.current_user_id || record.updated_by_id,
)
end

View file

@ -1,46 +0,0 @@
class Observer::Ticket::UserTicketCounter::BackgroundJob
def initialize(customer_id, updated_by_id)
@customer_id = customer_id
@updated_by_id = updated_by_id
end
def perform
# open ticket count
tickets_open = 0
tickets_closed = 0
if @customer_id != 1
state_open = Ticket::State.by_category(:open)
tickets_open = Ticket.where(
customer_id: @customer_id,
state_id: state_open,
).count()
# closed ticket count
state_closed = Ticket::State.by_category(:closed)
tickets_closed = Ticket.where(
customer_id: @customer_id,
state_id: state_closed,
).count()
end
# check if update is needed
customer = User.lookup(id: @customer_id)
return true if !customer
need_update = false
if customer[:preferences][:tickets_open] != tickets_open
need_update = true
customer[:preferences][:tickets_open] = tickets_open
end
if customer[:preferences][:tickets_closed] != tickets_closed
need_update = true
customer[:preferences][:tickets_closed] = tickets_closed
end
return true if !need_update
customer.updated_by_id = @updated_by_id
customer.save
end
end

View file

@ -0,0 +1,40 @@
require 'rails_helper'
RSpec.describe TicketUserTicketCounterJob, type: :job do
let!(:customer) { create(:user) }
let!(:tickets) do
ticket_states = {
open: Ticket::State.by_category(:open).first,
closed: Ticket::State.by_category(:closed).first,
}
tickets = {
open: [
create(:ticket, state_id: ticket_states[:open].id, customer_id: customer.id),
create(:ticket, state_id: ticket_states[:open].id, customer_id: customer.id),
],
closed: [
create(:ticket, state_id: ticket_states[:closed].id, customer_id: customer.id),
],
}
end
it 'checks if customer has no ticket count in preferences' do
customer.reload
expect(customer[:preferences][:tickets_open]).to be_falsey
expect(customer[:preferences][:tickets_closed]).to be_falsey
end
it 'checks if customer ticket count has been updated in preferences' do
TicketUserTicketCounterJob.perform_now(
customer.id,
customer.id,
)
customer.reload
expect(customer[:preferences][:tickets_open]).to be tickets[:open].count
expect(customer[:preferences][:tickets_closed]).to be tickets[:closed].count
end
end