feat: Ability to snooze conversations (#2682)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -306,6 +306,19 @@ RSpec.describe 'Conversations API', type: :request do
|
||||
expect(conversation.reload.status).to eq('pending')
|
||||
end
|
||||
|
||||
it 'toggles the conversation status to snoozed when parameter is passed' do
|
||||
expect(conversation.status).to eq('open')
|
||||
snoozed_until = (DateTime.now.utc + 2.days).to_i
|
||||
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { status: 'snoozed', snoozed_until: snoozed_until },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(conversation.reload.status).to eq('snoozed')
|
||||
expect(conversation.reload.snoozed_until.to_i).to eq(snoozed_until)
|
||||
end
|
||||
|
||||
# TODO: remove this spec when we remove the condition check in controller
|
||||
# Added for backwards compatibility for bot status
|
||||
it 'toggles the conversation status to pending status when parameter bot is passed' do
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Conversations::ReopenSnoozedConversationsJob, type: :job do
|
||||
let!(:snoozed_till_5_minutes_ago) { create(:conversation, status: :snoozed, snoozed_until: 5.minutes.ago) }
|
||||
let!(:snoozed_till_tomorrow) { create(:conversation, status: :snoozed, snoozed_until: 1.day.from_now) }
|
||||
let!(:snoozed_indefinitely) { create(:conversation, status: :snoozed) }
|
||||
|
||||
it 'enqueues the job' do
|
||||
expect { described_class.perform_later }.to have_enqueued_job(described_class)
|
||||
.on_queue('low')
|
||||
end
|
||||
|
||||
context 'when called' do
|
||||
it 'reopens snoozed conversations whose snooze until has passed' do
|
||||
described_class.perform_now
|
||||
|
||||
expect(snoozed_till_5_minutes_ago.reload.status).to eq 'open'
|
||||
expect(snoozed_till_tomorrow.reload.status).to eq 'snoozed'
|
||||
expect(snoozed_indefinitely.reload.status).to eq 'snoozed'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -20,5 +20,10 @@ RSpec.describe TriggerScheduledItemsJob, type: :job do
|
||||
expect(Campaigns::TriggerOneoffCampaignJob).to receive(:perform_later).with(campaign).once
|
||||
described_class.perform_now
|
||||
end
|
||||
|
||||
it 'triggers Conversations::ReopenSnoozedConversationsJob' do
|
||||
expect(Conversations::ReopenSnoozedConversationsJob).to receive(:perform_later).once
|
||||
described_class.perform_now
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -181,14 +181,38 @@ RSpec.describe Conversation, type: :model do
|
||||
end
|
||||
|
||||
describe '#toggle_status' do
|
||||
subject(:toggle_status) { conversation.toggle_status }
|
||||
|
||||
let(:conversation) { create(:conversation, status: :open) }
|
||||
|
||||
it 'toggles conversation status' do
|
||||
expect(toggle_status).to eq(true)
|
||||
it 'toggles conversation status to resolved when open' do
|
||||
conversation = create(:conversation, status: 'open')
|
||||
expect(conversation.toggle_status).to eq(true)
|
||||
expect(conversation.reload.status).to eq('resolved')
|
||||
end
|
||||
|
||||
it 'toggles conversation status to open when resolved' do
|
||||
conversation = create(:conversation, status: 'resolved')
|
||||
expect(conversation.toggle_status).to eq(true)
|
||||
expect(conversation.reload.status).to eq('open')
|
||||
end
|
||||
|
||||
it 'toggles conversation status to open when pending' do
|
||||
conversation = create(:conversation, status: 'pending')
|
||||
expect(conversation.toggle_status).to eq(true)
|
||||
expect(conversation.reload.status).to eq('open')
|
||||
end
|
||||
|
||||
it 'toggles conversation status to open when snoozed' do
|
||||
conversation = create(:conversation, status: 'snoozed')
|
||||
expect(conversation.toggle_status).to eq(true)
|
||||
expect(conversation.reload.status).to eq('open')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#ensure_snooze_until_reset' do
|
||||
it 'resets the snoozed_until when status is toggled' do
|
||||
conversation = create(:conversation, status: 'snoozed', snoozed_until: 2.days.from_now)
|
||||
expect(conversation.snoozed_until).not_to eq nil
|
||||
expect(conversation.toggle_status).to eq(true)
|
||||
expect(conversation.reload.snoozed_until).to eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#mute!' do
|
||||
|
||||
@@ -9,6 +9,30 @@ RSpec.describe Message, type: :model do
|
||||
it { is_expected.to validate_presence_of(:account_id) }
|
||||
end
|
||||
|
||||
describe '#reopen_conversation' do
|
||||
let(:conversation) { create(:conversation) }
|
||||
let(:message) { build(:message, message_type: :incoming, conversation: conversation) }
|
||||
|
||||
it 'reopens resolved conversation when the message is from a contact' do
|
||||
conversation.resolved!
|
||||
message.save!
|
||||
expect(message.conversation.open?).to eq true
|
||||
end
|
||||
|
||||
it 'reopens snoozed conversation when the message is from a contact' do
|
||||
conversation.snoozed!
|
||||
message.save!
|
||||
expect(message.conversation.open?).to eq true
|
||||
end
|
||||
|
||||
it 'will not reopen if the conversation is muted' do
|
||||
conversation.resolved!
|
||||
conversation.mute!
|
||||
message.save!
|
||||
expect(message.conversation.open?).to eq false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when message is created' do
|
||||
let(:message) { build(:message, account: create(:account)) }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user