From 29e8e414438a98faf1ff9a17eed201e081119ec9 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Thu, 17 Aug 2023 13:48:49 -0700 Subject: [PATCH] fix: Delete agent bots without deleting the messages (#7754) --- app/models/agent_bot.rb | 2 +- spec/models/agent_bot_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/agent_bot.rb b/app/models/agent_bot.rb index e01f286ab..07119fbca 100644 --- a/app/models/agent_bot.rb +++ b/app/models/agent_bot.rb @@ -23,7 +23,7 @@ class AgentBot < ApplicationRecord has_many :agent_bot_inboxes, dependent: :destroy_async has_many :inboxes, through: :agent_bot_inboxes - has_many :messages, as: :sender, dependent: :restrict_with_exception + has_many :messages, as: :sender, dependent: :nullify belongs_to :account, optional: true enum bot_type: { webhook: 0, csml: 1 } diff --git a/spec/models/agent_bot_spec.rb b/spec/models/agent_bot_spec.rb index b80c068eb..280446f7c 100644 --- a/spec/models/agent_bot_spec.rb +++ b/spec/models/agent_bot_spec.rb @@ -27,4 +27,16 @@ RSpec.describe AgentBot do expect(agent_bot.errors[:outgoing_url]).to include("is too long (maximum is #{Limits::URL_LENGTH_LIMIT} characters)") end end + + context 'when agent bot is deleted' do + let(:agent_bot) { create(:agent_bot) } + let(:message) { create(:message, sender: agent_bot) } + + it 'nullifies the message sender key' do + expect(message.sender).to eq agent_bot + agent_bot.destroy! + + expect(message.reload.sender).to be_nil + end + end end