Reassign organization of ticket if customers organization has changed.
This commit is contained in:
parent
454935f92a
commit
efb163b384
4 changed files with 109 additions and 2 deletions
29
app/models/observer/user/ticket_organization.rb
Normal file
29
app/models/observer/user/ticket_organization.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
||||||
|
|
||||||
|
class Observer::User::TicketOrganization < ActiveRecord::Observer
|
||||||
|
observe 'user'
|
||||||
|
|
||||||
|
def after_create(record)
|
||||||
|
check_organization(record)
|
||||||
|
end
|
||||||
|
def after_update(record)
|
||||||
|
check_organization(record)
|
||||||
|
end
|
||||||
|
|
||||||
|
# check if organization need to be updated
|
||||||
|
def check_organization(record)
|
||||||
|
|
||||||
|
# check if organization has changed
|
||||||
|
return if !record.changes['organization_id']
|
||||||
|
|
||||||
|
# update last 100 tickets of user
|
||||||
|
tickets = Ticket.where( :customer_id => record.id ).limit(100)
|
||||||
|
tickets.each {|ticket|
|
||||||
|
if ticket.organization_id != record.organization_id
|
||||||
|
ticket.organization_id = record.organization_id
|
||||||
|
ticket.save
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -44,6 +44,7 @@ module Zammad
|
||||||
'observer::_ticket::_online_notification_seen',
|
'observer::_ticket::_online_notification_seen',
|
||||||
'observer::_tag::_ticket_history',
|
'observer::_tag::_ticket_history',
|
||||||
'observer::_user::_ref_object_touch',
|
'observer::_user::_ref_object_touch',
|
||||||
|
'observer::_user::_ticket_organization',
|
||||||
'observer::_user::_geo',
|
'observer::_user::_geo',
|
||||||
'observer::_organization::_ref_object_touch'
|
'observer::_organization::_ref_object_touch'
|
||||||
|
|
||||||
|
|
77
test/unit/ticket_customer_organization_update_test.rb
Normal file
77
test/unit/ticket_customer_organization_update_test.rb
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
# encoding: utf-8
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class TicketCustomerOrganizationUpdateTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
|
# create base
|
||||||
|
groups = Group.where( :name => 'Users' )
|
||||||
|
roles = Role.where( :name => 'Agent' )
|
||||||
|
agent1 = User.create_or_update(
|
||||||
|
:login => 'ticket-customer-organization-update-agent1@example.com',
|
||||||
|
:firstname => 'Notification',
|
||||||
|
:lastname => 'Agent1',
|
||||||
|
:email => 'ticket-customer-organization-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 => 'Customer Organization Update',
|
||||||
|
:updated_at => '2015-02-05 16:37:00',
|
||||||
|
:updated_by_id => 1,
|
||||||
|
:created_by_id => 1,
|
||||||
|
)
|
||||||
|
customer1 = User.create_or_update(
|
||||||
|
:login => 'ticket-customer-organization-update-customer1@example.com',
|
||||||
|
:firstname => 'Notification',
|
||||||
|
:lastname => 'Customer1',
|
||||||
|
:email => 'ticket-customer-organization-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,
|
||||||
|
)
|
||||||
|
|
||||||
|
test 'create ticket, update customers organization later' do
|
||||||
|
|
||||||
|
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( customer1.id, ticket.customer.id )
|
||||||
|
assert_equal( organization1.id, ticket.organization.id )
|
||||||
|
|
||||||
|
# update customer organization
|
||||||
|
customer1.organization_id = nil
|
||||||
|
customer1.save
|
||||||
|
|
||||||
|
# verify ticket
|
||||||
|
ticket = Ticket.find(ticket.id)
|
||||||
|
assert_equal( nil, ticket.organization_id )
|
||||||
|
|
||||||
|
# update customer organization
|
||||||
|
customer1.organization_id = organization1.id
|
||||||
|
customer1.save
|
||||||
|
|
||||||
|
# verify ticket
|
||||||
|
ticket = Ticket.find(ticket.id)
|
||||||
|
assert_equal( organization1.id, ticket.organization_id )
|
||||||
|
|
||||||
|
ticket.destroy
|
||||||
|
end
|
||||||
|
end
|
|
@ -29,7 +29,7 @@ class TicketRefObjectTouchTest < ActiveSupport::TestCase
|
||||||
customer1 = User.create_or_update(
|
customer1 = User.create_or_update(
|
||||||
:login => 'ticket-ref-object-update-customer1@example.com',
|
:login => 'ticket-ref-object-update-customer1@example.com',
|
||||||
:firstname => 'Notification',
|
:firstname => 'Notification',
|
||||||
:lastname => 'Agent1',
|
:lastname => 'Customer1',
|
||||||
:email => 'ticket-ref-object-update-customer1@example.com',
|
:email => 'ticket-ref-object-update-customer1@example.com',
|
||||||
:password => 'customerpw',
|
:password => 'customerpw',
|
||||||
:active => true,
|
:active => true,
|
||||||
|
@ -42,7 +42,7 @@ class TicketRefObjectTouchTest < ActiveSupport::TestCase
|
||||||
customer2 = User.create_or_update(
|
customer2 = User.create_or_update(
|
||||||
:login => 'ticket-ref-object-update-customer2@example.com',
|
:login => 'ticket-ref-object-update-customer2@example.com',
|
||||||
:firstname => 'Notification',
|
:firstname => 'Notification',
|
||||||
:lastname => 'Agent2',
|
:lastname => 'Customer2',
|
||||||
:email => 'ticket-ref-object-update-customer2@example.com',
|
:email => 'ticket-ref-object-update-customer2@example.com',
|
||||||
:password => 'customerpw',
|
:password => 'customerpw',
|
||||||
:active => true,
|
:active => true,
|
||||||
|
|
Loading…
Reference in a new issue