Skip to content

Commit

Permalink
Refactoring: Migrated TicketOnlineNotificationSeenJob from Delayed::J…
Browse files Browse the repository at this point in the history
…ob to Active Job.
  • Loading branch information
jepf authored and thorsteneckel committed Dec 21, 2018
1 parent 4044394 commit 953d902
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
21 changes: 21 additions & 0 deletions app/jobs/ticket_online_notification_seen_job.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/observer/ticket/online_notification_seen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def _check(record)

# 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

This file was deleted.

11 changes: 1 addition & 10 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def merge_to(data)

=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)
Expand All @@ -366,15 +366,6 @@ def merge_to(data)
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)
Expand Down
23 changes: 23 additions & 0 deletions spec/jobs/ticket_online_notification_seen_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 953d902

Please sign in to comment.