[#139] Send conversation emails (#442)

* [#139] Delayed emails for conversations

* Added the setex and get methods to Redis wrapper
* Set the priorities for the sidekiq queues
* Was not able to use mailhog for testing email in local, switched back to letter opener and added comments on using the SMTP settings
* Added after create hood in messages to queue the sending of mail after 2 minutes using sidekiq worker and also set the redis key for the conversation to avoid the email sending for every message
* Added the sidekiq worker to send the email and delete the conversation redis key
* Added the mailer and mail template
* mailer sends the last 10 messages along with the new messages from the time it was queued

* Send email only in development or if smtp config is set

* Send email only in development or if smtp config is set
* Set the SMTP_PORT in production variable

* Adding redis to circle CI

* Specs for the conversation email changes

* Added specs for conversation email sidekiq worker
* Added specs for conversation mailer
* Added specs in message model for the after create hook for notify email

* Send emails only when there is a reply from agent

* set development to use mailhog

* Adding comments for using letter opener
This commit is contained in:
Sony Mathew
2020-01-23 23:14:07 +05:45
committed by Sojan Jose
parent 1d3ed016be
commit d4b3ba4baa
18 changed files with 184 additions and 13 deletions

View File

@@ -0,0 +1,30 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ConversationMailer, type: :mailer do
describe 'new_message' do
let(:agent) { create(:user, email: 'agent1@example.com') }
let(:conversation) { create(:conversation, assignee: agent) }
let(:message) { create(:message, conversation: conversation) }
let(:mail) { described_class.new_message(message.conversation, Time.zone.now).deliver_now }
let(:class_instance) { described_class.new }
before do
allow(described_class).to receive(:new).and_return(class_instance)
allow(class_instance).to receive(:smtp_config_set_or_development?).and_return(true)
end
it 'renders the subject' do
expect(mail.subject).to eq("[##{message.conversation.display_id}] #{message.content.truncate(30)}")
end
it 'renders the receiver email' do
expect(mail.to).to eq([message&.conversation&.contact&.email])
end
it 'renders the sender email' do
expect(mail.from).to eq([message&.conversation&.assignee&.email])
end
end
end

View File

@@ -22,5 +22,11 @@ RSpec.describe Message, type: :model do
expect(::MessageTemplates::HookExecutionService).to have_received(:new).with(message: message)
expect(hook_execution_service).to have_received(:perform)
end
it 'calls notify email method on after save' do
allow(message).to receive(:notify_via_mail).and_return(true)
message.save!
expect(message).to have_received(:notify_via_mail)
end
end
end

View File

@@ -5,6 +5,7 @@ require File.expand_path('../config/environment', __dir__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'pundit/rspec'
require 'sidekiq/testing'
# Add additional requires below this line. Rails is not loaded until this point!

View File

@@ -0,0 +1,37 @@
require 'rails_helper'
Sidekiq::Testing.fake!
RSpec.describe ConversationEmailWorker, type: :worker do
let(:perform_at) { (Time.zone.today + 6.hours).to_datetime }
let(:scheduled_job) { described_class.perform_at(perform_at, 1, Time.zone.now) }
let(:conversation) { build(:conversation, display_id: nil) }
describe 'testing ConversationEmailWorker' do
before do
conversation.save!
allow(Conversation).to receive(:find).and_return(conversation)
mailer = double
allow(ConversationMailer).to receive(:new_message).and_return(mailer)
allow(mailer).to receive(:deliver_later).and_return(true)
end
it 'worker jobs are enqueued in the mailers queue' do
described_class.perform_async
assert_equal :mailers, described_class.queue
end
it 'goes into the jobs array for testing environment' do
expect do
described_class.perform_async
end.to change(described_class.jobs, :size).by(1)
described_class.new.perform(1, Time.zone.now)
end
context 'with actions performed by the worker' do
it 'calls ConversationMailer' do
described_class.new.perform(1, Time.zone.now)
expect(ConversationMailer).to have_received(:new_message)
end
end
end
end