2019-10-12 18:08:41 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Conversation, type: :model do
|
|
|
|
describe '.before_create' do
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { build(:conversation, display_id: nil) }
|
2019-10-12 18:08:41 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
conversation.save
|
|
|
|
conversation.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs before_create callbacks' do
|
|
|
|
expect(conversation.display_id).to eq(1)
|
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
|
|
|
|
it 'creates a UUID for every conversation automatically' do
|
|
|
|
uuid_pattern = /[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}$/i
|
2020-06-02 18:20:39 +00:00
|
|
|
expect(conversation.uuid).to match(uuid_pattern)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.after_create' do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:agent) { create(:user, email: 'agent1@example.com', account: account) }
|
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
|
|
let(:conversation) do
|
|
|
|
create(
|
|
|
|
:conversation,
|
|
|
|
account: account,
|
|
|
|
contact: create(:contact, account: account),
|
|
|
|
inbox: inbox,
|
|
|
|
assignee: nil
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs after_create callbacks' do
|
|
|
|
# send_events
|
|
|
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
|
|
|
.with(described_class::CONVERSATION_CREATED, kind_of(Time), conversation: conversation)
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
2020-11-01 07:23:25 +00:00
|
|
|
|
|
|
|
it 'queues AutoResolveConversationsJob post creation if auto resolve duration present' do
|
|
|
|
account.update(auto_resolve_duration: 30)
|
|
|
|
expect do
|
|
|
|
create(
|
|
|
|
:conversation,
|
|
|
|
account: account,
|
|
|
|
contact: create(:contact, account: account),
|
|
|
|
inbox: inbox,
|
|
|
|
assignee: nil
|
|
|
|
)
|
|
|
|
end.to have_enqueued_job(AutoResolveConversationsJob)
|
|
|
|
end
|
2019-10-12 18:08:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.after_update' do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:conversation) do
|
2019-12-24 07:57:25 +00:00
|
|
|
create(:conversation, status: 'open', account: account, assignee: old_assignee)
|
2019-10-12 18:08:41 +00:00
|
|
|
end
|
|
|
|
let(:old_assignee) do
|
|
|
|
create(:user, email: 'agent1@example.com', account: account, role: :agent)
|
|
|
|
end
|
|
|
|
let(:new_assignee) do
|
|
|
|
create(:user, email: 'agent2@example.com', account: account, role: :agent)
|
|
|
|
end
|
|
|
|
let(:assignment_mailer) { double(deliver: true) }
|
2020-10-02 11:03:59 +00:00
|
|
|
let(:label) { create(:label, account: account) }
|
2019-10-12 18:08:41 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
conversation
|
|
|
|
new_assignee
|
|
|
|
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
Current.user = old_assignee
|
|
|
|
|
|
|
|
conversation.update(
|
|
|
|
status: :resolved,
|
|
|
|
locked: true,
|
2020-09-10 13:49:15 +00:00
|
|
|
contact_last_seen_at: Time.now,
|
2020-10-02 11:03:59 +00:00
|
|
|
assignee: new_assignee,
|
|
|
|
label_list: [label.title]
|
2019-10-12 18:08:41 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs after_update callbacks' do
|
|
|
|
# notify_status_change
|
|
|
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
|
|
|
.with(described_class::CONVERSATION_RESOLVED, kind_of(Time), conversation: conversation)
|
|
|
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
|
|
|
.with(described_class::CONVERSATION_READ, kind_of(Time), conversation: conversation)
|
|
|
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
|
|
|
.with(described_class::CONVERSATION_LOCK_TOGGLE, kind_of(Time), conversation: conversation)
|
|
|
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
|
|
|
.with(described_class::ASSIGNEE_CHANGED, kind_of(Time), conversation: conversation)
|
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
|
|
|
|
it 'creates conversation activities' do
|
|
|
|
# create_activity
|
2020-10-13 18:46:35 +00:00
|
|
|
expect(conversation.messages.pluck(:content)).to include("Conversation was marked resolved by #{old_assignee.name}")
|
|
|
|
expect(conversation.messages.pluck(:content)).to include("Assigned to #{new_assignee.name} by #{old_assignee.name}")
|
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{old_assignee.name} added #{label.title}")
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2020-11-01 07:23:25 +00:00
|
|
|
|
|
|
|
it 'adds a message for system auto resolution if marked resolved by system' do
|
|
|
|
conversation2 = create(:conversation, status: 'open', account: account, assignee: old_assignee)
|
|
|
|
account.update(auto_resolve_duration: 40)
|
|
|
|
Current.user = nil
|
|
|
|
conversation2.update(status: :resolved)
|
|
|
|
system_resolved_message = "Conversation was marked resolved by system due to #{account.auto_resolve_duration} days of inactivity"
|
|
|
|
expect(conversation2.messages.pluck(:content)).to include(system_resolved_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not trigger AutoResolutionJob if conversation reopened and account does not have auto resolve duration' do
|
|
|
|
expect { conversation.update(status: :open) }
|
|
|
|
.not_to have_enqueued_job(AutoResolveConversationsJob).with(conversation.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does trigger AutoResolutionJob if conversation reopened and account has auto resolve duration' do
|
|
|
|
account.update(auto_resolve_duration: 40)
|
|
|
|
expect { conversation.update(status: :open) }
|
|
|
|
.to have_enqueued_job(AutoResolveConversationsJob).with(conversation.id)
|
|
|
|
end
|
2019-10-12 18:08:41 +00:00
|
|
|
end
|
|
|
|
|
2020-06-02 18:20:39 +00:00
|
|
|
describe '#round robin' do
|
2019-10-12 18:08:41 +00:00
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:agent) { create(:user, email: 'agent1@example.com', account: account) }
|
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
|
|
let(:conversation) do
|
|
|
|
create(
|
|
|
|
:conversation,
|
|
|
|
account: account,
|
2019-10-20 19:10:18 +00:00
|
|
|
contact: create(:contact, account: account),
|
2019-10-12 18:08:41 +00:00
|
|
|
inbox: inbox,
|
|
|
|
assignee: nil
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2020-07-07 18:44:07 +00:00
|
|
|
create(:inbox_member, inbox: inbox, user: agent)
|
2019-10-12 18:08:41 +00:00
|
|
|
allow(Redis::Alfred).to receive(:rpoplpush).and_return(agent.id)
|
|
|
|
end
|
|
|
|
|
2020-06-02 18:20:39 +00:00
|
|
|
it 'runs round robin on after_save callbacks' do
|
2019-10-12 18:08:41 +00:00
|
|
|
# run_round_robin
|
|
|
|
expect(conversation.reload.assignee).to eq(agent)
|
|
|
|
end
|
2020-02-19 09:10:03 +00:00
|
|
|
|
|
|
|
it 'will not auto assign agent if enable_auto_assignment is false' do
|
|
|
|
inbox.update(enable_auto_assignment: false)
|
|
|
|
|
2020-06-02 18:20:39 +00:00
|
|
|
# run_round_robin
|
|
|
|
expect(conversation.reload.assignee).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'will not auto assign agent if its a bot conversation' do
|
|
|
|
conversation = create(
|
|
|
|
:conversation,
|
|
|
|
account: account,
|
|
|
|
contact: create(:contact, account: account),
|
|
|
|
inbox: inbox,
|
|
|
|
status: 'bot',
|
|
|
|
assignee: nil
|
|
|
|
)
|
2020-02-19 09:10:03 +00:00
|
|
|
|
|
|
|
# run_round_robin
|
|
|
|
expect(conversation.reload.assignee).to eq(nil)
|
|
|
|
end
|
2020-06-02 18:20:39 +00:00
|
|
|
|
|
|
|
it 'gets triggered on update only when status changes to open' do
|
|
|
|
conversation.status = 'resolved'
|
|
|
|
conversation.save!
|
|
|
|
expect(conversation.reload.assignee).to eq(agent)
|
2020-07-07 18:44:07 +00:00
|
|
|
inbox.inbox_members.where(user_id: agent.id).first.destroy!
|
2020-06-02 18:20:39 +00:00
|
|
|
|
|
|
|
# round robin changes assignee in this case since agent doesn't have access to inbox
|
|
|
|
agent2 = create(:user, email: 'agent2@example.com', account: account)
|
2020-07-07 18:44:07 +00:00
|
|
|
create(:inbox_member, inbox: inbox, user: agent2)
|
2020-06-02 18:20:39 +00:00
|
|
|
allow(Redis::Alfred).to receive(:rpoplpush).and_return(agent2.id)
|
|
|
|
conversation.status = 'open'
|
|
|
|
conversation.save!
|
|
|
|
expect(conversation.reload.assignee).to eq(agent2)
|
|
|
|
end
|
2019-10-12 18:08:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update_assignee' do
|
|
|
|
subject(:update_assignee) { conversation.update_assignee(agent) }
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { create(:conversation, assignee: nil) }
|
2019-10-12 18:08:41 +00:00
|
|
|
let(:agent) do
|
|
|
|
create(:user, email: 'agent@example.com', account: conversation.account, role: :agent)
|
|
|
|
end
|
2020-03-01 13:36:13 +00:00
|
|
|
let(:assignment_mailer) { double(deliver: true) }
|
2019-10-12 18:08:41 +00:00
|
|
|
|
|
|
|
it 'assigns the agent to conversation' do
|
|
|
|
expect(update_assignee).to eq(true)
|
|
|
|
expect(conversation.reload.assignee).to eq(agent)
|
|
|
|
end
|
2020-03-01 13:36:13 +00:00
|
|
|
|
2020-05-05 18:40:56 +00:00
|
|
|
it 'creates a new notification for the agent' do
|
2020-05-01 09:23:43 +00:00
|
|
|
expect(update_assignee).to eq(true)
|
2020-05-05 18:40:56 +00:00
|
|
|
expect(agent.notifications.count).to eq(1)
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|
|
|
|
|
2020-05-05 18:40:56 +00:00
|
|
|
it 'does not create assignment notification if notification setting is turned off' do
|
2020-03-01 13:36:13 +00:00
|
|
|
notification_setting = agent.notification_settings.first
|
|
|
|
notification_setting.unselect_all_email_flags
|
2020-05-05 18:40:56 +00:00
|
|
|
notification_setting.unselect_all_push_flags
|
2020-03-01 13:36:13 +00:00
|
|
|
notification_setting.save!
|
|
|
|
|
|
|
|
expect(update_assignee).to eq(true)
|
2020-05-05 18:40:56 +00:00
|
|
|
expect(agent.notifications.count).to eq(0)
|
2020-03-01 13:36:13 +00:00
|
|
|
end
|
2020-10-01 19:11:23 +00:00
|
|
|
|
|
|
|
context 'when agent is current user' do
|
|
|
|
before do
|
|
|
|
Current.user = agent
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates self-assigned message activity' do
|
|
|
|
expect(update_assignee).to eq(true)
|
2020-10-13 18:46:35 +00:00
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{agent.name} self-assigned this conversation")
|
2020-10-01 19:11:23 +00:00
|
|
|
end
|
|
|
|
end
|
2019-10-12 18:08:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-02 11:03:59 +00:00
|
|
|
describe '#update_labels' do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:conversation) { create(:conversation, account: account) }
|
|
|
|
let(:agent) do
|
|
|
|
create(:user, email: 'agent@example.com', account: account, role: :agent)
|
|
|
|
end
|
|
|
|
let(:first_label) { create(:label, account: account) }
|
|
|
|
let(:second_label) { create(:label, account: account) }
|
|
|
|
let(:third_label) { create(:label, account: account) }
|
|
|
|
let(:fourth_label) { create(:label, account: account) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
conversation
|
|
|
|
Current.user = agent
|
|
|
|
|
|
|
|
first_label
|
|
|
|
second_label
|
|
|
|
third_label
|
|
|
|
fourth_label
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds one label to conversation' do
|
|
|
|
labels = [first_label].map(&:title)
|
|
|
|
expect(conversation.update_labels(labels)).to eq(true)
|
|
|
|
expect(conversation.label_list).to match_array(labels)
|
2020-10-13 18:46:35 +00:00
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{agent.name} added #{labels.join(', ')}")
|
2020-10-02 11:03:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds and removes previously added labels' do
|
|
|
|
labels = [first_label, fourth_label].map(&:title)
|
|
|
|
expect(conversation.update_labels(labels)).to eq(true)
|
|
|
|
expect(conversation.label_list).to match_array(labels)
|
2020-10-13 18:46:35 +00:00
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{agent.name} added #{labels.join(', ')}")
|
2020-10-02 11:03:59 +00:00
|
|
|
|
|
|
|
updated_labels = [second_label, third_label].map(&:title)
|
|
|
|
expect(conversation.update_labels(updated_labels)).to eq(true)
|
|
|
|
expect(conversation.label_list).to match_array(updated_labels)
|
2020-10-13 18:46:35 +00:00
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{agent.name} added #{updated_labels.join(', ')}")
|
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{agent.name} removed #{labels.join(', ')}")
|
2020-10-02 11:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 18:08:41 +00:00
|
|
|
describe '#toggle_status' do
|
|
|
|
subject(:toggle_status) { conversation.toggle_status }
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { create(:conversation, status: :open) }
|
2019-10-12 18:08:41 +00:00
|
|
|
|
|
|
|
it 'toggles conversation status' do
|
|
|
|
expect(toggle_status).to eq(true)
|
|
|
|
expect(conversation.reload.status).to eq('resolved')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#lock!' do
|
|
|
|
subject(:lock!) { conversation.lock! }
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { create(:conversation) }
|
2019-10-12 18:08:41 +00:00
|
|
|
|
|
|
|
it 'assigns locks the conversation' do
|
|
|
|
expect(lock!).to eq(true)
|
|
|
|
expect(conversation.reload.locked).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#unlock!' do
|
|
|
|
subject(:unlock!) { conversation.unlock! }
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { create(:conversation) }
|
2019-10-12 18:08:41 +00:00
|
|
|
|
|
|
|
it 'unlocks the conversation' do
|
|
|
|
expect(unlock!).to eq(true)
|
|
|
|
expect(conversation.reload.locked).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
describe '#mute!' do
|
|
|
|
subject(:mute!) { conversation.mute! }
|
|
|
|
|
2020-10-30 16:57:25 +00:00
|
|
|
let(:user) do
|
|
|
|
create(:user, email: 'agent2@example.com', account: create(:account), role: :agent)
|
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
let(:conversation) { create(:conversation) }
|
|
|
|
|
2020-10-30 16:57:25 +00:00
|
|
|
before { Current.user = user }
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
it 'marks conversation as resolved' do
|
|
|
|
mute!
|
|
|
|
expect(conversation.reload.resolved?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks conversation as muted in redis' do
|
|
|
|
mute!
|
|
|
|
expect(Redis::Alfred.get(conversation.send(:mute_key))).not_to eq(nil)
|
|
|
|
end
|
2020-10-30 16:57:25 +00:00
|
|
|
|
|
|
|
it 'creates mute message' do
|
|
|
|
mute!
|
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{user.name} has muted the conversation")
|
|
|
|
end
|
2020-05-26 12:13:59 +00:00
|
|
|
end
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
describe '#unmute!' do
|
|
|
|
subject(:unmute!) { conversation.unmute! }
|
|
|
|
|
2020-10-30 16:57:25 +00:00
|
|
|
let(:user) do
|
|
|
|
create(:user, email: 'agent2@example.com', account: create(:account), role: :agent)
|
|
|
|
end
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
let(:conversation) { create(:conversation).tap(&:mute!) }
|
|
|
|
|
2020-10-30 16:57:25 +00:00
|
|
|
before { Current.user = user }
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
it 'does not change conversation status' do
|
|
|
|
expect { unmute! }.not_to(change { conversation.reload.status })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks conversation as muted in redis' do
|
|
|
|
expect { unmute! }
|
|
|
|
.to change { Redis::Alfred.get(conversation.send(:mute_key)) }
|
|
|
|
.to nil
|
|
|
|
end
|
2020-10-30 16:57:25 +00:00
|
|
|
|
|
|
|
it 'creates unmute message' do
|
|
|
|
unmute!
|
|
|
|
expect(conversation.messages.pluck(:content)).to include("#{user.name} has unmuted the conversation")
|
|
|
|
end
|
2020-10-08 06:32:08 +00:00
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
describe '#muted?' do
|
|
|
|
subject(:muted?) { conversation.muted? }
|
|
|
|
|
|
|
|
let(:conversation) { create(:conversation) }
|
|
|
|
|
|
|
|
it 'return true if conversation is muted' do
|
|
|
|
conversation.mute!
|
|
|
|
expect(muted?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if conversation is not muted' do
|
|
|
|
expect(muted?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 18:08:41 +00:00
|
|
|
describe 'unread_messages' do
|
|
|
|
subject(:unread_messages) { conversation.unread_messages }
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { create(:conversation, agent_last_seen_at: 1.hour.ago) }
|
2019-10-12 18:08:41 +00:00
|
|
|
let(:message_params) do
|
|
|
|
{
|
|
|
|
conversation: conversation,
|
|
|
|
account: conversation.account,
|
|
|
|
inbox: conversation.inbox,
|
2020-06-27 16:04:53 +00:00
|
|
|
sender: conversation.assignee
|
2019-10-12 18:08:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
let!(:message) do
|
|
|
|
create(:message, created_at: 1.minute.ago, **message_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:message, created_at: 1.month.ago, **message_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns unread messages' do
|
2020-01-09 07:36:40 +00:00
|
|
|
expect(unread_messages).to include(message)
|
2019-10-12 18:08:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'unread_incoming_messages' do
|
|
|
|
subject(:unread_incoming_messages) { conversation.unread_incoming_messages }
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { create(:conversation, agent_last_seen_at: 1.hour.ago) }
|
2019-10-12 18:08:41 +00:00
|
|
|
let(:message_params) do
|
|
|
|
{
|
|
|
|
conversation: conversation,
|
|
|
|
account: conversation.account,
|
|
|
|
inbox: conversation.inbox,
|
2020-06-27 16:04:53 +00:00
|
|
|
sender: conversation.assignee,
|
2019-10-12 18:08:41 +00:00
|
|
|
created_at: 1.minute.ago
|
|
|
|
}
|
|
|
|
end
|
|
|
|
let!(:message) do
|
|
|
|
create(:message, message_type: :incoming, **message_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:message, message_type: :outgoing, **message_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns unread incoming messages' do
|
|
|
|
expect(unread_incoming_messages).to contain_exactly(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#push_event_data' do
|
|
|
|
subject(:push_event_data) { conversation.push_event_data }
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
let(:conversation) { create(:conversation) }
|
2019-10-12 18:08:41 +00:00
|
|
|
let(:expected_data) do
|
|
|
|
{
|
2020-10-27 20:44:36 +00:00
|
|
|
additional_attributes: {},
|
2019-10-12 18:08:41 +00:00
|
|
|
meta: {
|
2019-10-20 19:10:18 +00:00
|
|
|
sender: conversation.contact.push_event_data,
|
2019-10-12 18:08:41 +00:00
|
|
|
assignee: conversation.assignee
|
|
|
|
},
|
|
|
|
id: conversation.display_id,
|
|
|
|
messages: [],
|
|
|
|
inbox_id: conversation.inbox_id,
|
2020-02-26 15:45:01 +00:00
|
|
|
status: conversation.status,
|
2020-10-05 17:22:43 +00:00
|
|
|
timestamp: conversation.last_activity_at.to_i,
|
2020-07-25 17:24:45 +00:00
|
|
|
can_reply: true,
|
2020-07-04 14:16:17 +00:00
|
|
|
channel: 'Channel::WebWidget',
|
2020-09-10 13:49:15 +00:00
|
|
|
contact_last_seen_at: conversation.contact_last_seen_at.to_i,
|
2019-10-12 18:08:41 +00:00
|
|
|
agent_last_seen_at: conversation.agent_last_seen_at.to_i,
|
|
|
|
unread_count: 0
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns push event payload' do
|
|
|
|
expect(push_event_data).to eq(expected_data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#lock_event_data' do
|
|
|
|
subject(:lock_event_data) { conversation.lock_event_data }
|
|
|
|
|
|
|
|
let(:conversation) do
|
|
|
|
build(:conversation, display_id: 505, locked: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns lock event payload' do
|
|
|
|
expect(lock_event_data).to eq(id: 505, locked: false)
|
|
|
|
end
|
|
|
|
end
|
2020-03-05 20:13:12 +00:00
|
|
|
|
|
|
|
describe '#botinbox: when conversation created inside inbox with agent bot' do
|
|
|
|
let!(:bot_inbox) { create(:agent_bot_inbox) }
|
|
|
|
let(:conversation) { create(:conversation, inbox: bot_inbox.inbox) }
|
|
|
|
|
|
|
|
it 'returns conversation status as bot' do
|
|
|
|
expect(conversation.status).to eq('bot')
|
|
|
|
end
|
|
|
|
end
|
2020-07-25 17:24:45 +00:00
|
|
|
|
|
|
|
describe '#can_reply?' do
|
|
|
|
describe 'on channels without 24 hour restriction' do
|
|
|
|
let(:conversation) { create(:conversation) }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(conversation.can_reply?).to eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'on channels with 24 hour restriction' do
|
|
|
|
let!(:facebook_channel) { create(:channel_facebook_page) }
|
|
|
|
let!(:facebook_inbox) { create(:inbox, channel: facebook_channel, account: facebook_channel.account) }
|
|
|
|
let!(:conversation) { create(:conversation, inbox: facebook_inbox, account: facebook_channel.account) }
|
|
|
|
|
|
|
|
it 'returns false if there are no incoming messages' do
|
|
|
|
expect(conversation.can_reply?).to eq false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'return false if last incoming message is outside of 24 hour window' do
|
|
|
|
create(
|
|
|
|
:message,
|
|
|
|
account: conversation.account,
|
|
|
|
inbox: facebook_inbox,
|
|
|
|
conversation: conversation,
|
|
|
|
created_at: Time.now - 25.hours
|
|
|
|
)
|
|
|
|
expect(conversation.can_reply?).to eq false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'return true if last incoming message is inside 24 hour window' do
|
|
|
|
create(
|
|
|
|
:message,
|
|
|
|
account: conversation.account,
|
|
|
|
inbox: facebook_inbox,
|
|
|
|
conversation: conversation
|
|
|
|
)
|
|
|
|
expect(conversation.can_reply?).to eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-12 18:08:41 +00:00
|
|
|
end
|