Fixed issue #2125 - Auto responder is not set from correct group.

This commit is contained in:
Martin Edenhofer 2018-07-13 19:50:10 +02:00
parent bc489aecef
commit 673b7c3d65
3 changed files with 364 additions and 212 deletions

View file

@ -24,7 +24,7 @@ class Observer::Ticket::Article::FillupFromEmail < ActiveRecord::Observer
return true if type['name'] != 'email' return true if type['name'] != 'email'
# set subject if empty # set subject if empty
ticket = Ticket.lookup(id: record.ticket_id) ticket = record.ticket
if !record.subject || record.subject == '' if !record.subject || record.subject == ''
record.subject = ticket.title record.subject = ticket.title
end end
@ -44,7 +44,7 @@ class Observer::Ticket::Article::FillupFromEmail < ActiveRecord::Observer
# set sender # set sender
email_address = ticket.group.email_address email_address = ticket.group.email_address
if !email_address if !email_address
raise "No email address found for group '#{ticket.group.name}'" raise "No email address found for group '#{ticket.group.name}' (#{ticket.group_id})"
end end
# remember email address for background job # remember email address for background job

View file

@ -824,13 +824,74 @@ perform changes on ticket
end end
end end
perform_notification = {}
changed = false changed = false
perform.each do |key, value| perform.each do |key, value|
(object_name, attribute) = key.split('.', 2) (object_name, attribute) = key.split('.', 2)
raise "Unable to update object #{object_name}.#{attribute}, only can update tickets and send notifications!" if object_name != 'ticket' && object_name != 'notification' raise "Unable to update object #{object_name}.#{attribute}, only can update tickets and send notifications!" if object_name != 'ticket' && object_name != 'notification'
# send notification # send notification (after changes are done)
if object_name == 'notification' if object_name == 'notification'
perform_notification[key] = value
next
end
# update tags
if key == 'ticket.tags'
next if value['value'].blank?
tags = value['value'].split(/,/)
if value['operator'] == 'add'
tags.each do |tag|
tag_add(tag, current_user_id || 1)
end
elsif value['operator'] == 'remove'
tags.each do |tag|
tag_remove(tag, current_user_id || 1)
end
else
logger.error "Unknown #{attribute} operator #{value['operator']}"
end
next
end
# delete ticket
if key == 'ticket.action'
next if value['value'].blank?
next if value['value'] != 'delete'
destroy!
next
end
# lookup pre_condition
if value['pre_condition']
if value['pre_condition'].match?(/^not_set/)
value['value'] = 1
elsif value['pre_condition'].match?(/^current_user\./)
raise 'Unable to use current_user, got no current_user_id for ticket.perform_changes' if !current_user_id
value['value'] = current_user_id
end
end
# update ticket
next if self[attribute].to_s == value['value'].to_s
changed = true
self[attribute] = value['value']
logger.debug { "set #{object_name}.#{attribute} = #{value['value'].inspect} for ticket_id #{id}" }
end
if changed
save!
end
perform_notification.each do |key, value|
perform_changes_notification(key, value, perform_origin, article)
end
true
end
def perform_changes_notification(_key, value, perform_origin, article)
# value['recipient'] was a string in the past (single-select) so we convert it to array if needed # value['recipient'] was a string in the past (single-select) so we convert it to array if needed
value_recipient = value['recipient'] value_recipient = value['recipient']
@ -970,20 +1031,20 @@ perform changes on ticket
recipients_checked.push(email) recipients_checked.push(email)
end end
next if recipients_checked.blank? return if recipients_checked.blank?
recipient_string = recipients_checked.join(', ') recipient_string = recipients_checked.join(', ')
group = self.group group_id = self.group_id
next if !group return if !group_id
email_address = group.email_address email_address = Group.find(group_id).email_address
if !email_address if !email_address
logger.info "Unable to send trigger based notification to #{recipient_string} because no email address is set for group '#{group.name}'" logger.info "Unable to send trigger based notification to #{recipient_string} because no email address is set for group '#{group.name}'"
next return
end end
if !email_address.channel_id if !email_address.channel_id
logger.info "Unable to send trigger based notification to #{recipient_string} because no channel is set for email address '#{email_address.email}' (id: #{email_address.id})" logger.info "Unable to send trigger based notification to #{recipient_string} because no channel is set for email address '#{email_address.email}' (id: #{email_address.id})"
next return
end end
# articles.last breaks (returns the wrong article) # articles.last breaks (returns the wrong article)
@ -1037,54 +1098,6 @@ perform changes on ticket
preferences: attachment[:preferences], preferences: attachment[:preferences],
) )
end end
next
end
# update tags
if key == 'ticket.tags'
next if value['value'].blank?
tags = value['value'].split(/,/)
if value['operator'] == 'add'
tags.each do |tag|
tag_add(tag, current_user_id || 1)
end
elsif value['operator'] == 'remove'
tags.each do |tag|
tag_remove(tag, current_user_id || 1)
end
else
logger.error "Unknown #{attribute} operator #{value['operator']}"
end
next
end
# delete ticket
if key == 'ticket.action'
next if value['value'].blank?
next if value['value'] != 'delete'
destroy!
next
end
# lookup pre_condition
if value['pre_condition']
if value['pre_condition'].match?(/^not_set/)
value['value'] = 1
elsif value['pre_condition'].match?(/^current_user\./)
raise 'Unable to use current_user, got no current_user_id for ticket.perform_changes' if !current_user_id
value['value'] = current_user_id
end
end
# update ticket
next if self[attribute].to_s == value['value'].to_s
changed = true
self[attribute] = value['value']
logger.debug { "set #{object_name}.#{attribute} = #{value['value'].inspect}" }
end
return if !changed
save
true true
end end
@ -1136,6 +1149,8 @@ perform active triggers on ticket
Transaction.execute(local_options) do Transaction.execute(local_options) do
triggers.each do |trigger| triggers.each do |trigger|
logger.debug { "Probe trigger (#{trigger.name}/#{trigger.id}) for this object (Ticket:#{ticket.id}/Loop:#{local_options[:loop_count]})" }
condition = trigger.condition condition = trigger.condition
# check if one article attribute is used # check if one article attribute is used

View file

@ -715,4 +715,141 @@ Some Text'
end end
test 'recursive trigger with auto responder' do
group1 = Group.create!(
name: 'Group dispatch',
active: true,
created_by_id: 1,
updated_by_id: 1,
)
group2 = Group.create!(
name: 'Group with auto responder',
active: true,
email_address: EmailAddress.first,
created_by_id: 1,
updated_by_id: 1,
)
trigger1 = Trigger.create!(
name: "002 - move ticket to #{group2.name}",
condition: {
'ticket.action' => {
'operator' => 'is',
'value' => 'create',
},
'ticket.group_id' => {
'operator' => 'is',
'value' => group1.id.to_s,
},
'ticket.organization_id' => {
'operator' => 'is',
'pre_condition' => 'specific',
'value' => User.lookup(email: 'nicole.braun@zammad.org').organization_id.to_s,
}
},
perform: {
'ticket.group_id' => {
'value' => group2.id.to_s,
},
},
disable_notification: true,
active: true,
created_by_id: 1,
updated_by_id: 1,
)
trigger2 = Trigger.create_or_update(
name: "001 auto reply for tickets in group #{group1.name}",
condition: {
'ticket.action' => {
'operator' => 'is',
'value' => 'create',
},
'ticket.state_id' => {
'operator' => 'is',
'value' => Ticket::State.lookup(name: 'new').id.to_s,
},
'ticket.group_id' => {
'operator' => 'is not',
'value' => group1.id.to_s,
},
},
perform: {
'notification.email' => {
'body' => "some text<br>\#{ticket.customer.lastname}<br>\#{ticket.title}<br>\#{article.body}",
'recipient' => 'ticket_customer',
'subject' => "Thanks for your inquiry (\#{ticket.title})!",
},
'ticket.priority_id' => {
'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
},
'ticket.tags' => {
'operator' => 'add',
'value' => 'aa, kk',
},
},
disable_notification: true,
active: true,
created_by_id: 1,
updated_by_id: 1,
)
ticket1 = Ticket.create!(
title: "some <b>title</b>\n äöüß",
group: group1,
customer: User.lookup(email: 'nicole.braun@zammad.org'),
updated_by_id: 1,
created_by_id: 1,
)
Ticket::Article.create!(
ticket_id: ticket1.id,
from: 'some_sender@example.com',
to: 'some_recipient@example.com',
subject: 'some subject',
message_id: 'some@id',
body: "some message <b>note</b>\nnew line",
internal: false,
sender: Ticket::Article::Sender.find_by(name: 'Customer'),
type: Ticket::Article::Type.find_by(name: 'web'),
updated_by_id: 1,
created_by_id: 1,
)
ticket1.reload
assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
assert_equal('Group dispatch', ticket1.group.name, 'ticket1.group verify')
assert_equal('new', ticket1.state.name, 'ticket1.state verify')
assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
assert_equal([], ticket1.tag_list)
Observer::Transaction.commit
ticket1.reload
assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
assert_equal('Group with auto responder', ticket1.group.name, 'ticket1.group verify')
assert_equal('new', ticket1.state.name, 'ticket1.state verify')
assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
assert_equal(%w[aa kk], ticket1.tag_list)
email_raw = "From: nicole.braun@zammad.org
To: zammad@example.com
Subject: test 1
X-Zammad-Ticket-Group: #{group1.name}
test 1"
ticket2, article2, user2 = Channel::EmailParser.new.process({ trusted: true }, email_raw)
assert_equal('test 1', ticket2.title, 'ticket2.title verify')
assert_equal('Group with auto responder', ticket2.group.name, 'ticket2.group verify')
assert_equal('new', ticket2.state.name, 'ticket2.state verify')
assert_equal('3 high', ticket2.priority.name, 'ticket2.priority verify')
assert_equal(2, ticket2.articles.count, 'ticket2.articles verify')
assert_equal(%w[aa kk], ticket2.tag_list)
end
end end