Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Email collect message hooks #331

Merged
merged 28 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cefb0f1
[#273] email collect message hooks
sojan-official Nov 30, 2019
39d72c9
should happen only on web widget conversations
sojan-official Nov 30, 2019
0420478
Fix specs
sojan-official Dec 1, 2019
2e222d1
Reduce cognitive complexity
sojan-official Dec 2, 2019
01566b6
Tests for existing methods in widgetcontrollers
sojan-official Dec 2, 2019
adb9607
End point to update contact
sojan-official Dec 2, 2019
b8a48a8
Save the attributes in content attributes
sojan-official Dec 5, 2019
1da773e
Show input if message_type = template
pranavrajs Dec 6, 2019
5f4e1df
Token Service
pranavrajs Dec 9, 2019
9000fa1
Fix import errors
pranavrajs Dec 15, 2019
0d1dd18
Add bot image
pranavrajs Dec 15, 2019
fbf37e0
Fix rspec
pranavrajs Dec 24, 2019
d326f7e
Spec for hook execution services
sojan-official Dec 26, 2019
8a7b1a0
Tests to behave like production
sojan-official Dec 26, 2019
cb24885
Spec for widget messages controller
sojan-official Dec 26, 2019
86d1a96
Returns token after updating contact
sojan-official Dec 27, 2019
4ebb791
Update the token on update contact
pranavrajs Dec 28, 2019
2731788
Remove contact_id from cw_conversation
pranavrajs Jan 7, 2020
7db6c15
Add contact_inboxes_id to converstions
pranavrajs Jan 7, 2020
cae0787
Revert schema.rb rubocop changes
pranavrajs Jan 7, 2020
3eb046d
Fix contact_inbox_id attribute
pranavrajs Jan 7, 2020
175abbc
Parse name from email, fix code climate issue
pranavrajs Jan 8, 2020
bc576fe
Fix schema.rb
pranavrajs Jan 8, 2020
5b797c1
Fix breaking specs
pranavrajs Jan 8, 2020
ecf36d6
Spec fixes
sojan-official Jan 8, 2020
7d1c1a6
Fix rubocop
sojan-official Jan 8, 2020
e7f26fe
Rubocop Fixes
pranavrajs Jan 9, 2020
122b70b
Fix breaking migration
pranavrajs Jan 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Spec for hook execution services
  • Loading branch information
sojan-official authored and pranavrajs committed Jan 9, 2020
commit d326f7e5953a74f58c94a8faaa76897bca757ab1
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ require:
- rubocop-rspec
inherit_from: .rubocop_todo.yml

Metrics/LineLength:
Layout/LineLength:
Max: 150
Metrics/ClassLength:
Max: 125
RSpec/ExampleLength:
Max: 15
Documentation:
Style/Documentation:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/widgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ class WidgetsController < ActionController::Base
before_action :set_contact
before_action :build_contact

private
def index
render
end

def set_contact
return if @auth_token_params[:contact_id].nil?
private

@contact = @web_widget.inbox.contacts.find(@auth_token_params[:contact_id])
def set_web_widget
@web_widget = ::Channel::WebWidget.find_by!(website_token: permitted_params[:website_token])
end

def set_token
Expand All @@ -21,8 +23,10 @@ def set_token
end
end

def set_web_widget
@web_widget = ::Channel::WebWidget.find_by!(website_token: permitted_params[:website_token])
def set_contact
return if @auth_token_params[:contact_id].nil?

@contact = @web_widget.inbox.contacts.find(@auth_token_params[:contact_id])
end

def build_contact
Expand All @@ -38,8 +42,4 @@ def build_contact
def permitted_params
params.permit(:website_token, :cw_conversation)
end

def secret_key
Rails.application.secrets.secret_key_base
end
end
5 changes: 3 additions & 2 deletions spec/controllers/widgets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
expect(response).to be_successful
end

it 'raises when called with website_token' do
expect { get widget_url }.to raise_exception ActiveRecord::RecordNotFound
it 'returns 404 when called with out website_token' do
get widget_url
expect(response.status).to eq(404)
end
end
end
11 changes: 7 additions & 4 deletions spec/factories/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
message_type { 'incoming' }
content_type { 'text' }
fb_id { SecureRandom.uuid }
account
inbox
conversation
user { build(:user) }
account { create(:account) }

after(:build) do |message|
message.user ||= create(:user, account: message.account)
message.conversation ||= create(:conversation, account: message.account)
message.inbox ||= create(:inbox, account: message.account)
end
end
end
15 changes: 15 additions & 0 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,19 @@
it { is_expected.to validate_presence_of(:conversation_id) }
it { is_expected.to validate_presence_of(:account_id) }
end

context 'when message is created' do
let(:message) { build(:message) }

it 'triggers ::MessageTemplates::HookExecutionService' do
hook_execution_service = double
allow(::MessageTemplates::HookExecutionService).to receive(:new).and_return(hook_execution_service)
allow(hook_execution_service).to receive(:perform).and_return(true)

message.save!

expect(::MessageTemplates::HookExecutionService).to have_received(:new).with(message: message)
expect(hook_execution_service).to have_received(:perform)
end
end
end
20 changes: 20 additions & 0 deletions spec/services/message_templates/hook_execution_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails_helper'

describe ::MessageTemplates::HookExecutionService do
context 'when it is a first message from web widget' do
it 'calls ::MessageTemplates::Template::EmailCollect' do
message = create(:message)
# this hook will only get executed for conversations with out any template messages
message.conversation.messages.template.destroy_all

email_collect_service = double
allow(::MessageTemplates::Template::EmailCollect).to receive(:new).and_return(email_collect_service)
allow(email_collect_service).to receive(:perform).and_return(true)

described_class.new(message: message).perform

expect(::MessageTemplates::Template::EmailCollect).to have_received(:new).with(conversation: message.conversation)
expect(email_collect_service).to have_received(:perform)
end
end
end
12 changes: 12 additions & 0 deletions spec/services/message_templates/template/email_collect_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails_helper'

describe ::MessageTemplates::Template::EmailCollect do
context 'when this hook is called' do
let(:conversation) { create(:conversation) }

it 'creates the email collect messages' do
described_class.new(conversation: conversation).perform
expect(conversation.messages.count).to eq(3)
end
end
end