Skip to content

Commit

Permalink
Silently ignore duplicate errors when saving tipline messages
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba authored Sep 29, 2023
1 parent d629ff7 commit 3a74e22
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
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
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

0 comments on commit 3a74e22

Please sign in to comment.