feat: move Captain conversation outcomes to episode grain [CW-7792] (#15315)
Moves Captain conversation outcomes from one row per conversation to one row per **engagement episode**: a new row each time demand for Captain (re)starts - first eligible message, a reopen after resolution, or (reserved) explicit assignment. Each episode has its own demand anchor, window, and trigger, so returning customers count as new demand and later cycles can't overwrite an earlier episode's handoff reason, resolution, or CSAT. Also adds `conversation_outcomes` associations on Account, Inbox, Conversation, and Captain::Assistant. ## Why this wasn't in #15233 The episode design came out of reviewing the wiring PR: per-conversation grain couldn't answer per-cycle questions without a patch per field. The table is unreleased with no writers, so changing the grain now is a pure schema swap - and landing it first means the tracker gets reviewed against the final model. ## What changed - Adds `episode_trigger`, `started_at`, `ended_at` - Drops `reopen_count` and `last_reopened_at` - reopens are episode rows now - Uniqueness moves from `(account, assistant, conversation)` to per-boundary `(account, conversation, started_at)` - Two partial unique indexes: one open episode per conversation, one initial episode per stream - Model: trigger enum, `started_at` uniqueness validation, `chronological`/`covering` scopes
This commit is contained in:
@@ -15,6 +15,13 @@ RSpec.describe ConversationOutcome, type: :model do
|
||||
.backed_by_column_of_type(:string)
|
||||
.with_prefix(:handoff_reason)
|
||||
}
|
||||
|
||||
it {
|
||||
expect(subject).to define_enum_for(:episode_trigger)
|
||||
.with_values(described_class::EPISODE_TRIGGERS.index_by(&:itself))
|
||||
.backed_by_column_of_type(:string)
|
||||
.with_prefix(:trigger)
|
||||
}
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
@@ -48,18 +55,34 @@ RSpec.describe ConversationOutcome, type: :model do
|
||||
expect(outcome.errors[:inbox]).to be_present
|
||||
end
|
||||
|
||||
it 'requires unique conversations per assistant and account' do
|
||||
existing = create(:conversation_outcome)
|
||||
it 'allows a later episode on the same conversation' do
|
||||
existing = create(:conversation_outcome, started_at: 1.hour.ago, ended_at: 30.minutes.ago)
|
||||
second_episode = build(
|
||||
:conversation_outcome,
|
||||
account: existing.account,
|
||||
assistant: existing.assistant,
|
||||
conversation: existing.conversation,
|
||||
inbox: existing.inbox,
|
||||
episode_trigger: 'reopen',
|
||||
started_at: 30.minutes.ago
|
||||
)
|
||||
|
||||
expect(second_episode).to be_valid
|
||||
end
|
||||
|
||||
it 'rejects a duplicate boundary on the same conversation' do
|
||||
existing = create(:conversation_outcome, started_at: 1.hour.ago)
|
||||
duplicate = build(
|
||||
:conversation_outcome,
|
||||
account: existing.account,
|
||||
assistant: existing.assistant,
|
||||
conversation: existing.conversation,
|
||||
inbox: existing.inbox
|
||||
inbox: existing.inbox,
|
||||
started_at: existing.started_at
|
||||
)
|
||||
|
||||
expect(duplicate).not_to be_valid
|
||||
expect(duplicate.errors[:conversation_id]).to be_present
|
||||
expect(duplicate.errors[:started_at]).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user