From e43f9a81e41c3c47a20fca33aa2560e4d1c54f64 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 1 Apr 2015 13:47:10 +0200 Subject: [PATCH] Set all notifications of merged ticket to seen. --- app/models/online_notification.rb | 56 ++++++++++++++++++++----- app/models/ticket.rb | 18 +++++--- test/unit/online_notifiaction_test.rb | 59 ++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 17 deletions(-) diff --git a/app/models/online_notification.rb b/app/models/online_notification.rb index 1c0a25c92..e53bea887 100644 --- a/app/models/online_notification.rb +++ b/app/models/online_notification.rb @@ -50,9 +50,7 @@ add a new online notification for this user mark online notification as seen OnlineNotification.seen( - :id => 2, - :user => UserObject, # optional, if passed all - # notfications for the given user are marked as seen + :id => 2, ) =end @@ -108,18 +106,33 @@ return all online notifications of an user return all online notifications of an object - notifications = OnlineNotification.by_object( 'Ticket', 123 ) + notifications = OnlineNotification.list_by_object( 'Ticket', 123 ) + +optional with seend attribute + + notifications = OnlineNotification.list_by_object( 'Ticket', 123, false ) + =end - def self.by_object( object_name, o_id ) + def self.list_by_object( object_name, o_id, seen = nil) object_id = ObjectLookup.by_name( object_name ) - notifications = OnlineNotification.where( - :object_lookup_id => object_id, - :o_id => o_id, - ). - order( 'created_at DESC, id DESC' ). - limit( 10_000 ) + if seen == nil + notifications = OnlineNotification.where( + :object_lookup_id => object_id, + :o_id => o_id, + ). + order( 'created_at DESC, id DESC' ). + limit( 10_000 ) + else + notifications = OnlineNotification.where( + :object_lookup_id => object_id, + :o_id => o_id, + :seen => seen, + ). + order( 'created_at DESC, id DESC' ). + limit( 10_000 ) + end list = [] notifications.each do |item| data = item.attributes @@ -134,6 +147,27 @@ return all online notifications of an object =begin +mark online notification as seen by object + + OnlineNotification.seen_by_object( 'Ticket', 123 ) + +=end + + def self.seen_by_object(object_name, o_id) + object_id = ObjectLookup.by_name( object_name ) + notifications = OnlineNotification.where( + :object_lookup_id => object_id, + :o_id => o_id, + :seen => false, + ) + notifications.each do |notification| + notification.seen = true + notification.save + end + end + +=begin + return all online notifications of an user with assets OnlineNotification.list_full( user ) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index ff67cef5d..612d03d4e 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -116,6 +116,7 @@ merge tickets ticket = Ticket.find(123) result = ticket.merge_to( :ticket_id => 123, + :user_id => 123, ) returns @@ -141,11 +142,13 @@ returns # create new merge article Ticket::Article.create( - :ticket_id => self.id, - :type_id => Ticket::Article::Type.lookup( :name => 'note' ).id, - :sender_id => Ticket::Article::Sender.lookup( :name => Z_ROLENAME_AGENT ).id, - :body => 'merged', - :internal => false + :ticket_id => self.id, + :type_id => Ticket::Article::Type.lookup( :name => 'note' ).id, + :sender_id => Ticket::Article::Sender.lookup( :name => Z_ROLENAME_AGENT ).id, + :body => 'merged', + :internal => false, + :created_by_id => data[:user_id], + :updated_by_id => data[:user_id], ) # add history to both @@ -167,6 +170,11 @@ returns # save ticket self.save + + # set all online notifications to seen + OnlineNotification.seen_by_object( 'Ticket', self.id ) + + true end private diff --git a/test/unit/online_notifiaction_test.rb b/test/unit/online_notifiaction_test.rb index 072700b43..66b14e023 100644 --- a/test/unit/online_notifiaction_test.rb +++ b/test/unit/online_notifiaction_test.rb @@ -77,6 +77,51 @@ class OnlineNotificationTest < ActiveSupport::TestCase }, ] }, + + # test 2 + { + :create => { + :ticket => { + :group_id => Group.lookup( :name => 'Users' ).id, + :customer_id => customer_user.id, + :owner_id => User.lookup( :login => '-' ).id, + :title => 'Unit Test 2 (äöüß)!', + :state_id => Ticket::State.lookup( :name => 'new' ).id, + :priority_id => Ticket::Priority.lookup( :name => '2 normal' ).id, + :updated_by_id => agent_user1.id, + :created_by_id => agent_user1.id, + }, + :article => { + :updated_by_id => agent_user1.id, + :created_by_id => agent_user1.id, + :type_id => Ticket::Article::Type.lookup( :name => 'phone' ).id, + :sender_id => Ticket::Article::Sender.lookup( :name => 'Customer' ).id, + :from => 'Unit Test ', + :body => 'Unit Test 123', + :internal => false + }, + }, + :update => { + :ticket => { + :title => 'Unit Test 2 (äöüß) - update!', + :state_id => Ticket::State.lookup( :name => 'open' ).id, + :priority_id => Ticket::Priority.lookup( :name => '1 low' ).id, + :updated_by_id => customer_user.id, + }, + }, + :check => [ + { + :type => 'create', + :object => 'Ticket', + :created_by_id => agent_user1.id, + }, + { + :type => 'update', + :object => 'Ticket', + :created_by_id => customer_user.id, + }, + ] + }, ] tickets = [] tests.each { |test| @@ -112,6 +157,18 @@ class OnlineNotificationTest < ActiveSupport::TestCase notification_check( OnlineNotification.list(agent_user2, 10), test[:check] ) } + # merge tickets - also remove notifications of merged tickets + tickets[0].merge_to( + :ticket_id => tickets[1].id, + :user_id => 1, + ) + notifications = OnlineNotification.list_by_object( 'Ticket', tickets[0].id, false ) + assert( notifications.empty?, "still not seen notifications for merged ticket available") + + notifications = OnlineNotification.list_by_object( 'Ticket', tickets[1].id, true ) + assert( notifications.empty?, "no notifications for master ticket available") + + # delete tickets tickets.each { |ticket| ticket_id = ticket.id @@ -120,7 +177,7 @@ class OnlineNotificationTest < ActiveSupport::TestCase assert( !found, "Ticket destroyed") # check if notifications for ticket still exist - notifications = OnlineNotification.by_object( 'Ticket', ticket_id ) + notifications = OnlineNotification.list_by_object( 'Ticket', ticket_id ) assert( notifications.empty?, "still notifications for destroyed ticket available") } end