Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Sep 29, 2023
2 parents c22ae98 + 35fd636 commit 38997ff
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ jobs:
include:
- stage: tests
name: functional-tests
script: docker-compose exec -e PATTERN='models mailers integration workers lib contract' api test/run-tests.sh
script: docker-compose exec -e TEST_RETRY_COUNT=3 -e PATTERN='models mailers integration workers lib contract' api test/run-tests.sh
- stage: tests
name: unit-tests
script: docker-compose exec -e PATTERN='controllers contract' api test/run-tests.sh
script: docker-compose exec -e TEST_RETRY_COUNT=3 -e PATTERN='controllers contract' api test/run-tests.sh
- stage: tests
name: contract-tests
script: docker-compose exec -e PATTERN='controllers models mailers integration workers lib' api test/run-tests.sh
script: docker-compose exec -e TEST_RETRY_COUNT=3 -e PATTERN='controllers models mailers integration workers lib' api test/run-tests.sh
notifications:
slack:
secure: dhqNhrJ0FVPnjtxa7R6k0s+1h/gMFNeK8zYJLZw+mK/FJ41K1u82Y8E6IDFbgNcKyAJ27ielvzGgWGSkDVltEnPR+ph15OMcy05TM9Pr2tWNusbDECOaEQgn4vGOq0shmiahE9tTOQpgc1TzhzIF9o1xgocah2PCLKiiH06kiiRlNZkaeQSJRFrXsPDDK8jIUtkLLUvFGQA6fq/lOh4tN6/N+K6+fo86wSxarkjv3d6h2flqvQqvqkbNpkv/UBC2Y1QACP+EX6uA0ySer8K5X6Q0Trrkjjacwvo5j74UVa+UYrBD+vr7Mgnr5aNFNId6M2nHd92ZiPM+6VDllCWsDLvJ2qFFy8cOO693EjrU7puaPp29+sptriIC71dk1oHSIEpPWwuaEZKzpwP4Swe322ne2th2VrjMhIye2Ru0519Lr2Dl4iTVV+hxoeVta3Nng23rUjrGoAbcw2FO1jmhANp8JWatv/V4PmlrS1/kYbiCfltWOMbKAD9f1EOTFnPCJsp3hPL238Ic+kvNJ8LM+ItNNR5cly+JPto6nSnMO8uhig9i78Mp2hVpnvrwhDLYntWEP2vcaNhP8oRys7X2iq5PRmsRwr9SyJReczd6i5gnvskXqKat6mNfPeGMBKSYof/1ve3Um0wEtwaYxvU5y/ZezFc3Kzzi6vt4P86j+mg=
Expand Down
2 changes: 1 addition & 1 deletion app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def self.store_sent_tipline_message(uid, external_id, sent_at, payload_json, tea
tm.payload = payload
tm.team_id = team_id
tm.skip_check_ability = true
tm.save!
tm.save_ignoring_duplicate!
end

def self.create_project_media_from_message(message)
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/smooch_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def send_resource_to_user(uid, workflow, resource_uuid, language)
if ['image', 'audio', 'video'].include?(resource.header_type)
type = resource.header_type
type = 'video' if type == 'audio' # Audio gets converted to video with a cover
type = 'file' if type == 'video' && RequestStore.store[:smooch_bot_provider] == 'ZENDESK' # Smooch doesn't support video
self.send_message_to_user(uid, message, { 'type' => type, 'mediaUrl' => CheckS3.rewrite_url(resource.header_media_url) })
sleep 2 # Wait a couple of seconds before sending the main menu
self.send_message_for_state(uid, workflow, 'main', language)
Expand Down
8 changes: 8 additions & 0 deletions app/models/tipline_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ class TiplineMessage < ApplicationRecord
validates_presence_of :team, :uid, :platform, :language, :direction, :sent_at, :payload, :state
validates_inclusion_of :state, in: ['sent', 'received', 'delivered']

def save_ignoring_duplicate!
begin
self.save!
rescue ActiveRecord::RecordNotUnique
Rails.logger.info("[Smooch Bot] Not storing tipline message because it already exists. ID: #{self.external_id}. State: #{self.state}.")
end
end

class << self
def from_smooch_payload(msg, payload, event = nil, language = nil)
msg = msg.with_indifferent_access
Expand Down
2 changes: 1 addition & 1 deletion app/workers/smooch_tipline_message_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def perform(message_json, payload_json)
end

tm = TiplineMessage.from_smooch_payload(message_json, payload_json, event, language)
tm.save
tm.save_ignoring_duplicate!

User.current = nil
end
Expand Down
49 changes: 49 additions & 0 deletions test/models/tipline_message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,53 @@ def setup
create_tipline_message state: 'invalid'
end
end

test "should ignore duplicate when saving" do
id = random_string
data = {
uid: random_string,
team_id: create_team.id,
language: 'en',
platform: 'WhatsApp',
sent_at: DateTime.now,
payload: {'foo' => 'bar'}
}
assert_nothing_raised do
assert_difference 'TiplineMessage.count' do
tm = TiplineMessage.new(data)
tm.direction = :outgoing
tm.external_id = id
tm.state = 'sent'
tm.save_ignoring_duplicate!
end
assert_difference 'TiplineMessage.count' do
tm = TiplineMessage.new(data)
tm.direction = :outgoing
tm.external_id = id
tm.state = 'delivered'
tm.save_ignoring_duplicate!
end
assert_difference 'TiplineMessage.count' do
tm = TiplineMessage.new(data)
tm.direction = :incoming
tm.external_id = random_string
tm.state = 'received'
tm.save_ignoring_duplicate!
end
assert_no_difference 'TiplineMessage.count' do
tm = TiplineMessage.new(data)
tm.direction = :outgoing
tm.external_id = id
tm.state = 'sent'
tm.save_ignoring_duplicate!
end
assert_no_difference 'TiplineMessage.count' do
tm = TiplineMessage.new(data)
tm.direction = :outgoing
tm.external_id = id
tm.state = 'delivered'
tm.save_ignoring_duplicate!
end
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

Dir[Rails.root.join("test/support/**/*.rb")].each {|f| require f}

Minitest::Retry.use!
Minitest::Retry.use!(retry_count: ENV['TEST_RETRY_COUNT'].to_i || 0)
TestDatabaseHelper.setup_database_partitions!

class ActionController::TestCase
Expand Down

0 comments on commit 38997ff

Please sign in to comment.