Reassign organization of ticket if customers organization has changed.

This commit is contained in:
Martin Edenhofer 2015-04-05 22:27:40 +02:00
parent 454935f92a
commit efb163b384
4 changed files with 109 additions and 2 deletions

View 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

View file

@ -44,6 +44,7 @@ module Zammad
'observer::_ticket::_online_notification_seen',
'observer::_tag::_ticket_history',
'observer::_user::_ref_object_touch',
'observer::_user::_ticket_organization',
'observer::_user::_geo',
'observer::_organization::_ref_object_touch'

View 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

View file

@ -29,7 +29,7 @@ class TicketRefObjectTouchTest < ActiveSupport::TestCase
customer1 = User.create_or_update(
:login => 'ticket-ref-object-update-customer1@example.com',
:firstname => 'Notification',
:lastname => 'Agent1',
:lastname => 'Customer1',
:email => 'ticket-ref-object-update-customer1@example.com',
:password => 'customerpw',
:active => true,
@ -42,7 +42,7 @@ class TicketRefObjectTouchTest < ActiveSupport::TestCase
customer2 = User.create_or_update(
:login => 'ticket-ref-object-update-customer2@example.com',
:firstname => 'Notification',
:lastname => 'Agent2',
:lastname => 'Customer2',
:email => 'ticket-ref-object-update-customer2@example.com',
:password => 'customerpw',
:active => true,