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

This commit is contained in:
Jens Pfeifer 2018-12-21 17:33:45 +01:00 committed by Thorsten Eckel
parent 4044394139
commit 953d902a74
5 changed files with 46 additions and 36 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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