chore: general improvements (#204)

* chore: update scheduled messages author association to nullable and adjust related specs

* chore: update sender handling for WhatsApp messages and add external sender name
This commit is contained in:
Gabriel Jablonski 2026-02-01 14:25:06 -03:00 committed by GitHub
parent 4483b7457a
commit fb6fec167b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 66 additions and 13 deletions

View File

@ -433,8 +433,16 @@ function handleReplyTo() {
}
const avatarInfo = computed(() => {
// If no sender, return bot info
// If no sender, check for external sender name
if (!props.sender) {
const externalSenderName = props.contentAttributes?.externalSenderName;
if (externalSenderName === 'WhatsApp') {
return {
name: t('CONVERSATION.WHATSAPP'),
src: '',
iconName: 'i-woot-whatsapp',
};
}
return {
name: t('CONVERSATION.BOT'),
src: '',

View File

@ -255,6 +255,7 @@
"MESSAGE_ERROR": "Unable to send this message, please try again later",
"SENT_BY": "Sent by:",
"BOT": "Bot",
"WHATSAPP": "WhatsApp",
"SEND_FAILED": "Couldn't send message! Try again",
"TRY_AGAIN": "retry",
"ASSIGNMENT": {

View File

@ -254,6 +254,7 @@
"MESSAGE_ERROR": "Não foi possível enviar esta mensagem, por favor, tente novamente mais tarde",
"SENT_BY": "Enviado por:",
"BOT": "Robôs",
"WHATSAPP": "WhatsApp",
"SEND_FAILED": "Não foi possível enviar a mensagem! Tente novamente",
"TRY_AGAIN": "tentar novamente",
"ASSIGNMENT": {

View File

@ -22,7 +22,7 @@ class AutomationRule < ApplicationRecord
include Reauthorizable
belongs_to :account
has_many :scheduled_messages, as: :author, dependent: :destroy
has_many :scheduled_messages, as: :author, dependent: :nullify
has_many_attached :files
validate :json_conditions_format

View File

@ -3,7 +3,7 @@
# Table name: scheduled_messages
#
# id :bigint not null, primary key
# author_type :string not null
# author_type :string
# content :text
# scheduled_at :datetime
# status :integer default("draft"), not null
@ -11,7 +11,7 @@
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# author_id :bigint not null
# author_id :bigint
# conversation_id :bigint not null
# inbox_id :bigint not null
# message_id :bigint
@ -43,7 +43,7 @@ class ScheduledMessage < ApplicationRecord
belongs_to :account
belongs_to :inbox
belongs_to :conversation
belongs_to :author, polymorphic: true
belongs_to :author, polymorphic: true, optional: true
belongs_to :message, optional: true
has_one_attached :attachment

View File

@ -100,7 +100,7 @@ class User < ApplicationRecord
has_many :messages, as: :sender, dependent: :nullify
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', source: :inviter, dependent: :nullify
has_many :scheduled_messages, as: :author, dependent: :destroy
has_many :scheduled_messages, as: :author, dependent: :nullify
has_many :custom_filters, dependent: :destroy_async
has_many :dashboard_apps, dependent: :nullify

View File

@ -93,8 +93,7 @@ module Whatsapp::BaileysHandlers::MessagesUpsert # rubocop:disable Metrics/Modul
account_id: @inbox.account_id,
inbox_id: @inbox.id,
source_id: raw_message_id,
sender: incoming? ? @contact : @inbox.account.account_users.first.user,
sender_type: incoming? ? 'Contact' : 'User',
sender: incoming? ? @contact : nil,
message_type: incoming? ? :incoming : :outgoing,
content_attributes: message_content_attributes
)
@ -110,6 +109,7 @@ module Whatsapp::BaileysHandlers::MessagesUpsert # rubocop:disable Metrics/Modul
type = message_type
msg = unwrap_ephemeral_message(@raw_message[:message])
content_attributes = { external_created_at: baileys_extract_message_timestamp(@raw_message[:messageTimestamp]) }
content_attributes[:external_sender_name] = 'WhatsApp' unless incoming?
if type == 'reaction'
content_attributes[:in_reply_to_external_id] = msg.dig(:reactionMessage, :key, :id)
content_attributes[:is_reaction] = true

View File

@ -156,8 +156,7 @@ module Whatsapp::ZapiHandlers::ReceivedCallback # rubocop:disable Metrics/Module
account_id: @inbox.account_id,
inbox_id: @inbox.id,
source_id: raw_message_id,
sender: incoming_message? ? @contact : @inbox.account.account_users.first.user,
sender_type: incoming_message? ? 'Contact' : 'User',
sender: incoming_message? ? @contact : nil,
message_type: incoming_message? ? :incoming : :outgoing,
content_attributes: message_content_attributes
)
@ -170,6 +169,7 @@ module Whatsapp::ZapiHandlers::ReceivedCallback # rubocop:disable Metrics/Module
def message_content_attributes
type = message_type
content_attributes = { external_created_at: @raw_message[:momment] / 1000 }
content_attributes[:external_sender_name] = 'WhatsApp' unless incoming_message?
if type == 'reaction'
content_attributes[:in_reply_to_external_id] = @raw_message.dig(:reaction, :referencedMessage, :messageId)

View File

@ -0,0 +1,10 @@
class ChangeScheduledMessagesAuthorToNullable < ActiveRecord::Migration[7.1]
def up
change_column_null :scheduled_messages, :author_id, true
change_column_null :scheduled_messages, :author_type, true
end
def down
raise ActiveRecord::IrreversibleMigration, "Can't revert because there might be scheduled messages with null author_id/author_type"
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2026_01_22_175206) do
ActiveRecord::Schema[7.1].define(version: 2026_02_01_162122) do
# These extensions should be enabled to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@ -1134,8 +1134,8 @@ ActiveRecord::Schema[7.1].define(version: 2026_01_22_175206) do
t.bigint "account_id", null: false
t.bigint "conversation_id", null: false
t.bigint "inbox_id", null: false
t.string "author_type", null: false
t.bigint "author_id", null: false
t.string "author_type"
t.bigint "author_id"
t.bigint "message_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false

View File

@ -443,6 +443,23 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
expect(response).to have_http_status(:success)
expect(account.automation_rules.count).to eq(0)
end
it 'deletes automation rule even when it has sent scheduled messages' do
conversation = create(:conversation, account: account, inbox: inbox, contact: contact)
scheduled_message = create(:scheduled_message,
account: account,
inbox: inbox,
conversation: conversation,
author: automation_rule)
scheduled_message.update_column(:status, ScheduledMessage.statuses[:sent]) # rubocop:disable Rails/SkipsModelValidations
delete "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
headers: administrator.create_new_auth_token
expect(response).to have_http_status(:success)
expect(account.automation_rules.count).to eq(0)
expect(scheduled_message.reload.author_id).to be_nil
end
end
end
end

View File

@ -282,4 +282,20 @@ RSpec.describe User do
expect(user.signature_separator).to eq('--')
end
end
describe 'destroy' do
it 'nullifies scheduled messages author when user has sent scheduled messages' do
account = create(:account)
create(:account_user, user: user, account: account)
inbox = create(:inbox, account: account)
contact = create(:contact, account: account)
conversation = create(:conversation, account: account, inbox: inbox, contact: contact)
scheduled_message = create(:scheduled_message, account: account, inbox: inbox, conversation: conversation, author: user)
scheduled_message.update_column(:status, ScheduledMessage.statuses[:sent]) # rubocop:disable Rails/SkipsModelValidations
user.destroy!
expect(scheduled_message.reload.author_id).to be_nil
end
end
end