From 21f1841f5b6ce1bec75cff8a7fe3af963c291cec Mon Sep 17 00:00:00 2001 From: Jens Pfeifer Date: Fri, 21 Dec 2018 17:35:15 +0100 Subject: [PATCH] Refactoring: Migrated TicketUserTicketCounterJob from Delayed::Job to Active Job. --- app/jobs/ticket_user_ticket_counter_job.rb | 39 ++++++++++++++++ .../observer/ticket/user_ticket_counter.rb | 8 ++-- .../user_ticket_counter/background_job.rb | 46 ------------------- .../ticket_user_ticket_counter_job_spec.rb | 40 ++++++++++++++++ 4 files changed, 82 insertions(+), 51 deletions(-) create mode 100644 app/jobs/ticket_user_ticket_counter_job.rb delete mode 100644 app/models/observer/ticket/user_ticket_counter/background_job.rb create mode 100644 spec/jobs/ticket_user_ticket_counter_job_spec.rb diff --git a/app/jobs/ticket_user_ticket_counter_job.rb b/app/jobs/ticket_user_ticket_counter_job.rb new file mode 100644 index 000000000..b459ec3aa --- /dev/null +++ b/app/jobs/ticket_user_ticket_counter_job.rb @@ -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 diff --git a/app/models/observer/ticket/user_ticket_counter.rb b/app/models/observer/ticket/user_ticket_counter.rb index b2de03b1e..a19c05f45 100644 --- a/app/models/observer/ticket/user_ticket_counter.rb +++ b/app/models/observer/ticket/user_ticket_counter.rb @@ -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 diff --git a/app/models/observer/ticket/user_ticket_counter/background_job.rb b/app/models/observer/ticket/user_ticket_counter/background_job.rb deleted file mode 100644 index 22a2edcab..000000000 --- a/app/models/observer/ticket/user_ticket_counter/background_job.rb +++ /dev/null @@ -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 diff --git a/spec/jobs/ticket_user_ticket_counter_job_spec.rb b/spec/jobs/ticket_user_ticket_counter_job_spec.rb new file mode 100644 index 000000000..67b137d12 --- /dev/null +++ b/spec/jobs/ticket_user_ticket_counter_job_spec.rb @@ -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