Browse Source

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

Jens Pfeifer 6 years ago
parent
commit
953d902a74

+ 21 - 0
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

+ 1 - 1
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

+ 0 - 25
app/models/observer/ticket/online_notification_seen/background_job.rb

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

+ 1 - 10
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)

+ 23 - 0
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