* fix(conversations): enforce NOT NULL + FK on contact_id Conversations had contact_id nullable with no FK to contacts. Combined with dependent: :destroy_async on Contact#conversations, deleting a contact could leave conversations pointing to a missing contact, breaking the conversations#index API with "undefined method 'additional_attributes' for nil" from the contact partial. Changes: - Migration cleans up existing orphans, sets contact_id NOT NULL and adds a FK with ON DELETE CASCADE so the invariant is enforced at the DB level (complements the existing Rails presence validation). - ContactMergeAction uses update_all for conversations/messages/notes/ contact_inboxes so a failing callback cannot silently leave records pointing to the mergee contact before it is destroyed. - Drop the now-redundant orphan filter in Conversations::ResolutionJob and its spec; the invariant is enforced at the schema level. * fix: address review feedback - Drop the ON DELETE CASCADE FK on conversations.contact_id. Several conversation-owned tables (messages, mentions, conversation_participants, reporting_events, csat_survey_responses, calls, applied_slas, sla_events, and polymorphic notifications) still have plain conversation_id references without FK cascades. The DB-level cascade would skip Conversation's dependent: cleanup and replace the NULL-contact bug with orphan children, and would also conflict with the existing non-cascade FKs on scheduled_messages/recurring_scheduled_messages. Keep the invariant at the Rails layer (NOT NULL + presence validation + dependent: :destroy_async). - Clean up orphan conversations in the migration via Rails destroy so dependent associations are propagated correctly, instead of a raw DELETE FROM conversations that would orphan all child rows. - Revert ContactMergeAction.merge_* methods back to per-record update! so Conversation#after_update_commit still fires (notify_status_change / CONVERSATION_CONTACT_CHANGED) for contact_id changes. The bang form still removes the silent-failure risk of the original .update call.
69 lines
3.1 KiB
Ruby
69 lines
3.1 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe Conversations::ResolutionJob do
|
|
subject(:job) { described_class.perform_later(account: account) }
|
|
|
|
let!(:account) { create(:account) }
|
|
let(:label) { create(:label, title: 'auto-resolved', account: account) }
|
|
let!(:conversation) { create(:conversation, account: account) }
|
|
|
|
it 'enqueues the job' do
|
|
expect { job }.to have_enqueued_job(described_class)
|
|
.with(account: account)
|
|
.on_queue('low')
|
|
end
|
|
|
|
it 'does nothing when there is no auto resolve duration' do
|
|
described_class.perform_now(account: account)
|
|
expect(conversation.reload.status).to eq('open')
|
|
end
|
|
|
|
context 'when auto_resolve_ignore_waiting is true' do
|
|
it 'resolves non-waiting conversations if time of inactivity is more than auto resolve duration' do
|
|
account.update!(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: true) # 10 days in minutes
|
|
conversation.update!(last_activity_at: 13.days.ago, waiting_since: nil)
|
|
described_class.perform_now(account: account)
|
|
expect(conversation.reload.status).to eq('resolved')
|
|
end
|
|
|
|
it 'does not resolve waiting conversations even if time of inactivity is more than auto resolve duration' do
|
|
account.update!(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: true) # 10 days in minutes
|
|
conversation.update!(last_activity_at: 13.days.ago, waiting_since: 13.days.ago)
|
|
described_class.perform_now(account: account)
|
|
expect(conversation.reload.status).to eq('open')
|
|
end
|
|
end
|
|
|
|
context 'when auto_resolve_ignore_waiting is false' do
|
|
it 'resolves all conversations if time of inactivity is more than auto resolve duration' do
|
|
account.update!(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: false) # 10 days in minutes
|
|
# Create one waiting conversation and one non-waiting conversation
|
|
waiting_conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: 13.days.ago)
|
|
non_waiting_conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: nil)
|
|
|
|
described_class.perform_now(account: account)
|
|
|
|
expect(waiting_conversation.reload.status).to eq('resolved')
|
|
expect(non_waiting_conversation.reload.status).to eq('resolved')
|
|
end
|
|
end
|
|
|
|
it 'adds a label after resolution' do
|
|
account.update!(auto_resolve_label: 'auto-resolved', auto_resolve_after: 14_400)
|
|
conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: 13.days.ago)
|
|
|
|
described_class.perform_now(account: account)
|
|
|
|
expect(conversation.reload.status).to eq('resolved')
|
|
expect(conversation.reload.label_list).to include('auto-resolved')
|
|
end
|
|
|
|
it 'resolves only a limited number of conversations in a single execution' do
|
|
stub_const('Limits::BULK_ACTIONS_LIMIT', 2)
|
|
account.update!(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: false) # 10 days in minutes
|
|
create_list(:conversation, 3, account: account, last_activity_at: 13.days.ago)
|
|
described_class.perform_now(account: account)
|
|
expect(account.conversations.resolved.count).to eq(Limits::BULK_ACTIONS_LIMIT)
|
|
end
|
|
end
|