diff --git a/lib/tweet.rb b/lib/tweet.rb index 78afb8736..94c1fafbd 100644 --- a/lib/tweet.rb +++ b/lib/tweet.rb @@ -97,15 +97,24 @@ class Tweet Rails.logger.debug group_id.inspect if tweet.class.to_s == 'Twitter::DirectMessage' - ticket = Ticket.find_by( - customer_id: user.id, - state: Ticket::State.where.not( - state_type_id: Ticket::StateType.where( - name: 'closed', + + article = Ticket::Article.find_by( + from: 'me_bauer', + type_id: Ticket::Article::Type.find_by( name: 'twitter direct-message' ).id, + ) + + if article + ticket = Ticket.find_by( + id: article.ticket_id, + customer_id: user.id, + state: Ticket::State.where.not( + state_type_id: Ticket::StateType.where( + name: 'closed', + ) ) ) - ) - return ticket if ticket + return ticket if ticket + end end Ticket.create( diff --git a/test/integration/twitter_test.rb b/test/integration/twitter_test.rb index 0a2f4518d..cce6ed5ac 100644 --- a/test/integration/twitter_test.rb +++ b/test/integration/twitter_test.rb @@ -220,25 +220,86 @@ class TwitterTest < ActiveSupport::TestCase sleep 5 } - assert( article, 'inbound article created' ) + assert( article, "inbound article '#{text}' created" ) ticket = article.ticket assert( ticket, 'ticket of inbound article exists' ) assert( ticket.articles, 'ticket.articles exists' ) - assert_equal( ticket.articles.count, 1, 'ticket article inbound count' ) + assert_equal( 1, ticket.articles.count, 'ticket article inbound count' ) assert_equal( ticket.state.name, 'new' ) # reply via ticket outbound_article = Ticket::Article.create( ticket_id: ticket.id, to: 'me_bauer', - body: text, + body: 'Will call you later!', type: Ticket::Article::Type.find_by( name: 'twitter direct-message' ), sender: Ticket::Article::Sender.find_by( name: 'Agent' ), internal: false, updated_by_id: 1, created_by_id: 1, ) + ticket.state = Ticket::State.find_by( name: 'pending reminder' ) + ticket.save + assert( outbound_article, 'outbound article created' ) - assert_equal( outbound_article.ticket.articles.count, 2, 'ticket article outbound count' ) + assert_equal( 2, outbound_article.ticket.articles.count, 'ticket article outbound count' ) + + text = 'Ok. ' + hash + dm = client.create_direct_message( + 'armin_theo', + text, + ) + assert( dm, "second dm with ##{hash} created" ) + + # fetch check system account + article = nil + (1..4).each { + Channel.fetch + + # check if ticket and article has been created + article = Ticket::Article.find_by( message_id: dm.id ) + + break if article + + sleep 5 + } + + assert( article, "inbound article '#{text}' created" ) + ticket = article.ticket + assert( ticket, 'ticket of inbound article exists' ) + assert( ticket.articles, 'ticket.articles exists' ) + assert_equal( 3, ticket.articles.count, 'ticket article inbound count' ) + assert_equal( ticket.state.name, 'open' ) + + # close dm ticket, next dm should open a new + ticket.state = Ticket::State.find_by( name: 'closed' ) + ticket.save + + text = 'Thanks for your call . I just have one question. ' + hash + dm = client.create_direct_message( + 'armin_theo', + text, + ) + assert( dm, "third dm with ##{hash} created" ) + + # fetch check system account + article = nil + (1..4).each { + Channel.fetch + + # check if ticket and article has been created + article = Ticket::Article.find_by( message_id: dm.id ) + + break if article + + sleep 5 + } + + assert( article, "inbound article '#{text}' created" ) + ticket = article.ticket + assert( ticket, 'ticket of inbound article exists' ) + assert( ticket.articles, 'ticket.articles exists' ) + assert_equal( 1, ticket.articles.count, 'ticket article inbound count' ) + assert_equal( ticket.state.name, 'new' ) end end