From 8ab56ef23545186c47711738ee615046edf73306 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 12 Jun 2017 15:24:10 +0200 Subject: [PATCH] If user organization relation/featrue is used for different propose, do not touch references. --- .../observer/organization/ref_object_touch.rb | 5 +- app/models/observer/user/ref_object_touch.rb | 18 ++- app/models/organization/assets.rb | 7 +- .../organization_ref_object_touch_test.rb | 132 ++++++++++++++++- test/unit/user_ref_object_touch_test.rb | 137 ++++++++++++++++-- 5 files changed, 274 insertions(+), 25 deletions(-) diff --git a/app/models/observer/organization/ref_object_touch.rb b/app/models/observer/organization/ref_object_touch.rb index 8f97798db..043113b28 100644 --- a/app/models/observer/organization/ref_object_touch.rb +++ b/app/models/observer/organization/ref_object_touch.rb @@ -20,8 +20,11 @@ class Observer::Organization::RefObjectTouch < ActiveRecord::Observer # return if we run import mode return if Setting.get('import_mode') + # featrue used for different propose, do not touch references + return if User.where(organization_id: record.id).count > 100 + # touch organizations tickets - Ticket.select('id').where( organization_id: record.id ).each(&:touch) + Ticket.select('id').where(organization_id: record.id).each(&:touch) # touch current members record.member_ids.uniq.each { |user_id| diff --git a/app/models/observer/user/ref_object_touch.rb b/app/models/observer/user/ref_object_touch.rb index 9c2a76233..b63f043dd 100644 --- a/app/models/observer/user/ref_object_touch.rb +++ b/app/models/observer/user/ref_object_touch.rb @@ -25,16 +25,24 @@ class Observer::User::RefObjectTouch < ActiveRecord::Observer organization_id_changed = record.changes['organization_id'] if organization_id_changed && organization_id_changed[0] != organization_id_changed[1] if organization_id_changed[0] - organization = Organization.find(organization_id_changed[0]) - organization.touch - member_ids = organization.member_ids + + # featrue used for different propose, do not touch references + if User.where(organization_id: organization_id_changed[0]).count < 100 + organization = Organization.find(organization_id_changed[0]) + organization.touch + member_ids = organization.member_ids + end end end # touch new/current organization if record.organization - record.organization.touch - member_ids += record.organization.member_ids + + # featrue used for different propose, do not touch references + if User.where(organization_id: record.organization_id).count < 100 + record.organization.touch + member_ids += record.organization.member_ids + end end # touch old/current customer diff --git a/app/models/organization/assets.rb b/app/models/organization/assets.rb index 86840cf20..d0108676f 100644 --- a/app/models/organization/assets.rb +++ b/app/models/organization/assets.rb @@ -39,7 +39,12 @@ returns # loops, will be updated with lookup attributes later data[ app_model_organization ][ id ] = local_attributes - if local_attributes['member_ids'] + if local_attributes['member_ids'].present? + + # featrue used for different propose, do limit refernces + if local_attributes['member_ids'].count > 100 + local_attributes['member_ids'] = local_attributes['member_ids'].sort[0, 100] + end local_attributes['member_ids'].each { |local_user_id| next if data[ app_model_user ][ local_user_id ] user = User.lookup(id: local_user_id) diff --git a/test/unit/organization_ref_object_touch_test.rb b/test/unit/organization_ref_object_touch_test.rb index c9cb26f97..04a7c9ebe 100644 --- a/test/unit/organization_ref_object_touch_test.rb +++ b/test/unit/organization_ref_object_touch_test.rb @@ -2,11 +2,8 @@ require 'test_helper' class OrganizationRefObjectTouchTest < ActiveSupport::TestCase - agent1 = nil - organization1 = nil - customer1 = nil - customer2 = nil - test 'aaa - setup' do + + test 'check if ticket and customer has been updated' do # create base groups = Group.where(name: 'Users') @@ -63,9 +60,6 @@ class OrganizationRefObjectTouchTest < ActiveSupport::TestCase updated_by_id: 1, created_by_id: 1, ) - end - - test 'b - check if ticket and customer has been updated' do ticket = Ticket.create( title: "some title1\n äöüß", @@ -125,4 +119,126 @@ class OrganizationRefObjectTouchTest < ActiveSupport::TestCase assert(delete, 'ticket destroy') travel_back end + + test 'check if ticket and customer has not been updated (different featrue propose)' do + + # create base + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent1 = User.create_or_update( + login: 'organization-ref-object-not-update-agent1@example.com', + firstname: 'Notification', + lastname: 'Agent1', + email: 'organization-ref-object-not-update-agent1@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + organization1 = Organization.create_if_not_exists( + name: 'Ref Object Update Org 1 (no update)', + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + organization2 = Organization.create_if_not_exists( + name: 'Ref Object Update Org 2 (no update)', + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + customer1 = User.create_or_update( + login: 'organization-ref-object-not-update-customer1@example.com', + firstname: 'Notification', + lastname: 'Agent1', + email: 'organization-ref-object-not-update-customer1@example.com', + password: 'customerpw', + active: true, + organization_id: organization1.id, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + customer2 = User.create_or_update( + login: 'organization-ref-object-not-update-customer2@example.com', + firstname: 'Notification', + lastname: 'Agent2', + email: 'organization-ref-object-not-update-customer2@example.com', + password: 'customerpw', + active: true, + organization_id: organization2.id, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + (1..100).each { |count| + User.create_or_update( + login: "organization-ref-object-update-customer3-#{count}@example.com", + firstname: 'Notification', + lastname: 'Agent2', + email: "organization-ref-object-update-customer3-#{count}@example.com", + password: 'customerpw', + active: true, + organization_id: organization1.id, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + } + + ticket = Ticket.create( + title: "some title1\n äöüß", + group: Group.lookup(name: 'Users'), + customer_id: customer1.id, + owner_id: agent1.id, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_at: '2015-02-05 16:39:00', + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket, 'ticket created') + assert_equal(ticket.customer.id, customer1.id) + assert_equal(ticket.organization.id, organization1.id) + + customer1 = User.find(customer1.id) + assert_not_equal('2015-02-05 16:37:00 UTC', customer1.updated_at.to_s) + customer1_updated_at = customer1.updated_at + + travel 4.seconds + organization1.name = 'Ref Object Update Org 1 (no update)/1' + organization1.save + organization1_updated_at = organization1.updated_at + + # check if ticket and customer has been touched + ticket = Ticket.find(ticket.id) + assert_equal('2015-02-05 16:39:00 UTC', ticket.updated_at.to_s) + + customer1 = User.find(customer1.id) + assert_equal(customer1_updated_at.to_s, customer1.updated_at.to_s) + + travel 4.seconds + + customer2.organization_id = organization1.id + customer2.save + + # check if customer1 and organization has been touched + customer1 = User.find(customer1.id) + assert_equal(customer1_updated_at.to_s, customer1.updated_at.to_s) + + organization1 = Organization.find(organization1.id) + assert_equal(organization1_updated_at.to_s, organization1.updated_at.to_s) + + delete = ticket.destroy + assert(delete, 'ticket destroy') + travel_back + end + end diff --git a/test/unit/user_ref_object_touch_test.rb b/test/unit/user_ref_object_touch_test.rb index cf8117485..f4d949c04 100644 --- a/test/unit/user_ref_object_touch_test.rb +++ b/test/unit/user_ref_object_touch_test.rb @@ -2,11 +2,7 @@ require 'test_helper' class UserRefObjectTouchTest < ActiveSupport::TestCase - agent1 = nil - organization1 = nil - customer1 = nil - customer2 = nil - test 'aaa - setup' do + test 'check if ticket and organization has been updated' do # create base groups = Group.where(name: 'Users') @@ -57,9 +53,6 @@ class UserRefObjectTouchTest < ActiveSupport::TestCase updated_by_id: 1, created_by_id: 1, ) - end - - test 'b - check if ticket and organization has been updated' do ticket = Ticket.create( title: "some title1\n äöüß", @@ -75,7 +68,7 @@ class UserRefObjectTouchTest < ActiveSupport::TestCase assert_equal(ticket.customer.id, customer1.id) assert_equal(ticket.organization.id, organization1.id) - sleep 4 + travel 4.seconds customer1.firstname = 'firstname customer1' customer1.save @@ -88,7 +81,131 @@ class UserRefObjectTouchTest < ActiveSupport::TestCase assert(false, 'organization1.updated_at has not been updated') end - sleep 4 + travel 4.seconds + + ticket.customer_id = customer2.id + ticket.save + + # check if customer1, customer2 and organization has been touched + customer1 = User.find(customer1.id) + if customer1.updated_at > 3.seconds.ago + assert(true, 'customer1.updated_at has been updated') + else + assert(false, 'customer1.updated_at has not been updated') + end + + customer2 = User.find(customer2.id) + if customer2.updated_at > 3.seconds.ago + assert(true, 'customer2.updated_at has been updated') + else + assert(false, 'customer2.updated_at has not been updated') + end + + organization1 = Organization.find(organization1.id) + if organization1.updated_at > 3.seconds.ago + assert(true, 'organization1.updated_at has been updated') + else + assert(false, 'organization1.updated_at has not been updated') + end + + delete = ticket.destroy + assert(delete, 'ticket destroy') + end + + test 'check if ticket and organization has not been updated (different featrue propose)' do + + # create base + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent1 = User.create_or_update( + login: 'user-ref-object-update-agent1@example.com', + firstname: 'Notification', + lastname: 'Agent1', + email: 'user-ref-object-update-agent1@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + organization1 = Organization.create_if_not_exists( + name: 'Ref Object Update Org (not updated)', + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + customer1 = User.create_or_update( + login: 'user-ref-object-update-customer1@example.com', + firstname: 'Notification', + lastname: 'Agent1', + email: 'user-ref-object-update-customer1@example.com', + password: 'customerpw', + active: true, + organization_id: organization1.id, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + customer2 = User.create_or_update( + login: 'user-ref-object-update-customer2@example.com', + firstname: 'Notification', + lastname: 'Agent2', + email: 'user-ref-object-update-customer2@example.com', + password: 'customerpw', + active: true, + organization_id: nil, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + + (1..100).each { |count| + User.create_or_update( + login: "user-ref-object-update-customer3-#{count}@example.com", + firstname: 'Notification', + lastname: 'Agent2', + email: "user-ref-object-update-customer3-#{count}@example.com", + password: 'customerpw', + active: true, + organization_id: organization1.id, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + } + + ticket = Ticket.create( + title: "some title1\n äöüß", + group: Group.lookup(name: 'Users'), + customer_id: customer1.id, + owner_id: agent1.id, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket, 'ticket created') + assert_equal(ticket.customer.id, customer1.id) + assert_equal(ticket.organization.id, organization1.id) + organization1_updated_at = ticket.organization.updated_at + + travel 4.seconds + + customer1.firstname = 'firstname customer1' + customer1.save + customer1_updated_at = customer1.updated_at + + # check if organization has been touched + organization1 = Organization.find(organization1.id) + assert_equal(organization1_updated_at.to_s, ticket.updated_at.to_s) + + travel 4.seconds ticket.customer_id = customer2.id ticket.save