diff --git a/app/models/bot/smooch.rb b/app/models/bot/smooch.rb index c6036891ce..81a54dbd52 100644 --- a/app/models/bot/smooch.rb +++ b/app/models/bot/smooch.rb @@ -682,12 +682,36 @@ 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] + self.delay.store_tipline_message(uid, external_id, sent_at, payload_json, team_id, platform) + response + end + + def self.store_tipline_message(uid, external_id, sent_at, payload_json, team_id, platform) + payload = JSON.parse(payload_json) + tm = TiplineMessage.new + tm.uid = uid + tm.state = 'sent' + tm.direction = :outgoing + tm.language = self.get_user_language(uid) + 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) diff --git a/app/models/tipline_message.rb b/app/models/tipline_message.rb index cd1b6a84a7..baf831521f 100644 --- a/app/models/tipline_message.rb +++ b/app/models/tipline_message.rb @@ -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), } diff --git a/db/migrate/20230920055719_add_state_column_to_tipline_message.rb b/db/migrate/20230920055719_add_state_column_to_tipline_message.rb new file mode 100644 index 0000000000..acefc827ac --- /dev/null +++ b/db/migrate/20230920055719_add_state_column_to_tipline_message.rb @@ -0,0 +1,10 @@ +class AddStateColumnToTiplineMessage < ActiveRecord::Migration[6.1] + def change + add_column :tipline_messages, :state, :string + remove_index :tipline_messages, name: "index_tipline_messages_on_external_id" + add_index :tipline_messages, :external_id + # updated state for existing records + TiplineMessage.where(direction: "incoming").update_all(state: 'received') + TiplineMessage.where(direction: "outgoing").update_all(state: 'delivered') + end +end diff --git a/db/schema.rb b/db/schema.rb index 40e1696ac6..1258e70daa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_09_14_152816) do +ActiveRecord::Schema.define(version: 2023_09_20_055719) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -639,7 +639,8 @@ t.bigint "team_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["external_id"], name: "index_tipline_messages_on_external_id", unique: true + t.string "state" + t.index ["external_id"], name: "index_tipline_messages_on_external_id" t.index ["team_id"], name: "index_tipline_messages_on_team_id" t.index ["uid"], name: "index_tipline_messages_on_uid" end @@ -779,7 +780,8 @@ end create_table "versions", id: :serial, force: :cascade do |t| - t.string "item_type", null: false + t.string "item_type" + t.string "{:null=>false}" t.string "item_id", null: false t.string "event", null: false t.string "whodunnit"