Skip to content

Commit

Permalink
CV2-3680: Store tipline messages we send (#1664)
Browse files Browse the repository at this point in the history
* CV2-3680: store tipline messages we sent

* CV2-3680: remove external_id uniqueness validation

* CV2-3680: fix tests and add more tests

* CV2-3680: apply PR comments

* CV2-3680: fix tests

* CV2-3680: fix platform fallback
  • Loading branch information
melsawy authored Sep 28, 2023
1 parent 1ae10ed commit c01ad0e
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 259 deletions.
31 changes: 28 additions & 3 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,37 @@ def self.send_error_message(message, is_supported)
def self.send_message_to_user(uid, text, extra = {}, force = false, preview_url = true)
return if self.config['smooch_disabled'] && !force
if RequestStore.store[:smooch_bot_provider] == 'TURN'
self.turnio_send_message_to_user(uid, text, extra, force, preview_url)
response = self.turnio_send_message_to_user(uid, text, extra, force, preview_url)
elsif RequestStore.store[:smooch_bot_provider] == 'CAPI'
self.capi_send_message_to_user(uid, text, extra, force, preview_url)
response = self.capi_send_message_to_user(uid, text, extra, force, preview_url)
else
self.zendesk_send_message_to_user(uid, text, extra, force, preview_url)
response = self.zendesk_send_message_to_user(uid, text, extra, force, preview_url)
end
# store a TiplineMessage
external_id = self.get_id_from_send_response(response)
sent_at = DateTime.now
payload_json = { text: text }.merge(extra).to_json
team_id = self.config['team_id'].to_i
platform = RequestStore.store[:smooch_bot_platform] || 'Unknown'
language = self.get_user_language(uid)
self.delay.store_sent_tipline_message(uid, external_id, sent_at, payload_json, team_id, platform, language)
response
end

def self.store_sent_tipline_message(uid, external_id, sent_at, payload_json, team_id, platform, language)
payload = JSON.parse(payload_json)
tm = TiplineMessage.new
tm.uid = uid
tm.state = 'sent'
tm.direction = :outgoing
tm.language = language
tm.platform = platform
tm.sent_at = sent_at
tm.external_id = external_id
tm.payload = payload
tm.team_id = team_id
tm.skip_check_ability = true
tm.save!
end

def self.create_project_media_from_message(message)
Expand Down
6 changes: 4 additions & 2 deletions app/models/tipline_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class TiplineMessage < ApplicationRecord

belongs_to :team

validates_uniqueness_of :external_id
validates_presence_of :team, :uid, :platform, :language, :direction, :sent_at, :payload
validates_presence_of :team, :uid, :platform, :language, :direction, :sent_at, :payload, :state
validates_inclusion_of :state, in: ['sent', 'received', 'delivered']

class << self
def from_smooch_payload(msg, payload, event = nil, language = nil)
Expand All @@ -26,12 +26,14 @@ def from_smooch_payload(msg, payload, event = nil, language = nil)
when 'message:appUser'
{
direction: :incoming,
state: 'received',
sent_at: parse_timestamp(msg['received']),
platform: Bot::Smooch.get_platform_from_message(msg),
}
when 'message:delivery:channel'
{
direction: :outgoing,
state: 'delivered',
sent_at: parse_timestamp(payload['timestamp']),
platform: Bot::Smooch.get_platform_from_payload(payload),
}
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20230920055719_add_state_column_to_tipline_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddStateColumnToTiplineMessage < ActiveRecord::Migration[6.1]
def change
add_column :tipline_messages, :state, :string
# remove uniq index
remove_index :tipline_messages, name: "index_tipline_messages_on_external_id"
# updated state for existing records
TiplineMessage.where(direction: "incoming", state: nil).update_all(state: 'received')
TiplineMessage.where(direction: "outgoing", state: nil).update_all(state: 'delivered')
add_index :tipline_messages, [:external_id, :state], unique: true
end
end
Loading

0 comments on commit c01ad0e

Please sign in to comment.