From 953d902a744b365dcaec0bdf253f410ff4b5b9ac Mon Sep 17 00:00:00 2001 From: Jens Pfeifer Date: Fri, 21 Dec 2018 17:33:45 +0100 Subject: [PATCH] Refactoring: Migrated TicketOnlineNotificationSeenJob from Delayed::Job to Active Job. --- .../ticket_online_notification_seen_job.rb | 21 ++++++++++++++++ .../ticket/online_notification_seen.rb | 2 +- .../background_job.rb | 25 ------------------- app/models/ticket.rb | 11 +------- ...icket_online_notification_seen_job_spec.rb | 23 +++++++++++++++++ 5 files changed, 46 insertions(+), 36 deletions(-) create mode 100644 app/jobs/ticket_online_notification_seen_job.rb delete mode 100644 app/models/observer/ticket/online_notification_seen/background_job.rb create mode 100644 spec/jobs/ticket_online_notification_seen_job_spec.rb diff --git a/app/jobs/ticket_online_notification_seen_job.rb b/app/jobs/ticket_online_notification_seen_job.rb new file mode 100644 index 000000000..48410207e --- /dev/null +++ b/app/jobs/ticket_online_notification_seen_job.rb @@ -0,0 +1,21 @@ +class TicketOnlineNotificationSeenJob < ApplicationJob + def perform(ticket_id, user_id) + user_id = user_id || 1 + + # set all online notifications to seen + Transaction.execute do + ticket = Ticket.lookup(id: ticket_id) + OnlineNotification.list_by_object('Ticket', ticket_id).each do |notification| + next if notification.seen + + seen = ticket.online_notification_seen_state(notification.user_id) + next if !seen + next if seen == notification.seen + + notification.seen = true + notification.updated_by_id = user_id + notification.save! + end + end + end +end diff --git a/app/models/observer/ticket/online_notification_seen.rb b/app/models/observer/ticket/online_notification_seen.rb index 04d3a3285..4125464da 100644 --- a/app/models/observer/ticket/online_notification_seen.rb +++ b/app/models/observer/ticket/online_notification_seen.rb @@ -27,6 +27,6 @@ class Observer::Ticket::OnlineNotificationSeen < ActiveRecord::Observer # set all online notifications to seen # send background job - Delayed::Job.enqueue(Observer::Ticket::OnlineNotificationSeen::BackgroundJob.new(record.id, record.updated_by_id)) + TicketOnlineNotificationSeenJob.perform_later(record.id, record.updated_by_id) end end diff --git a/app/models/observer/ticket/online_notification_seen/background_job.rb b/app/models/observer/ticket/online_notification_seen/background_job.rb deleted file mode 100644 index d79bda580..000000000 --- a/app/models/observer/ticket/online_notification_seen/background_job.rb +++ /dev/null @@ -1,25 +0,0 @@ -class Observer::Ticket::OnlineNotificationSeen::BackgroundJob - def initialize(ticket_id, user_id) - @ticket_id = ticket_id - @user_id = user_id || 1 - end - - def perform - - # set all online notifications to seen - Transaction.execute do - ticket = Ticket.lookup(id: @ticket_id) - OnlineNotification.list_by_object('Ticket', @ticket_id).each do |notification| - next if notification.seen - - seen = ticket.online_notification_seen_state(notification.user_id) - next if !seen - next if seen == notification.seen - - notification.seen = true - notification.updated_by_id = @user_id - notification.save! - end - end - end -end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index de2217b5f..d9febb4d3 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -357,7 +357,7 @@ returns =begin -check if online notifcation should be shown in general as already seen with current state +check if online notification should be shown in general as already seen with current state ticket = Ticket.find(1) seen = ticket.online_notification_seen_state(user_id_check) @@ -366,15 +366,6 @@ returns result = true # or false -check if online notifcation should be shown for this user as already seen with current state - - ticket = Ticket.find(1) - seen = ticket.online_notification_seen_state(check_user_id) - -returns - - result = true # or false - =end def online_notification_seen_state(user_id_check = nil) diff --git a/spec/jobs/ticket_online_notification_seen_job_spec.rb b/spec/jobs/ticket_online_notification_seen_job_spec.rb new file mode 100644 index 000000000..1c9eb90ac --- /dev/null +++ b/spec/jobs/ticket_online_notification_seen_job_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +RSpec.describe TicketOnlineNotificationSeenJob, type: :job do + let!(:user) { create(:user) } + let!(:other_user) { create(:user) } + let!(:ticket) { create(:ticket, owner: user, created_by_id: user.id) } + let!(:online_notification) do + create(:online_notification, o_id: ticket.id, user_id: user.id) + end + + it 'checks if online notification has not been seen' do + expect(online_notification.reload.seen).to be false + end + + it 'checks if online notification has been seen' do + ticket.state_id = Ticket::State.lookup(name: 'closed').id + ticket.save! + + expect do + TicketOnlineNotificationSeenJob.perform_now(ticket.id, user.id) + end.to change { online_notification.reload.seen } + end +end