Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Creating an actions for successful calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrei committed Jul 13, 2017
1 parent f7e7908 commit f8ea44b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/models/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Call < ApplicationRecord
enum status: %i[unstarted started connected failed]
belongs_to :page
belongs_to :member
belongs_to :action

validates :page, presence: true
validates :member_phone_number, presence: true
Expand Down
6 changes: 6 additions & 0 deletions app/services/call_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def run
end

if @call.persisted? && !@call.failed?
create_action
publish_event
end
end
Expand Down Expand Up @@ -97,6 +98,11 @@ def place_call
end
end

def create_action
@action = Action.create!(page: @page, member: @call.member)
@call.update!(action: @action)
end

def publish_event
return if @call.member.blank? # handle this in worker
CallEvent::New.publish(@call, @extra_params)
Expand Down
5 changes: 4 additions & 1 deletion app/services/call_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def payload
action_target_call_status: @call.target_call_status,
action_target: @call.target.to_hash
},
meta: { call_id: @call.id }
meta: {
call_id: @call.id,
action_id: @call.action.id
}
}
end
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170713183132_add_action_id_to_calls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddActionIdToCalls < ActiveRecord::Migration[5.1]
def change
add_column :calls, :action_id, :integer, index: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170626194936) do
ActiveRecord::Schema.define(version: 20170713183132) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -74,6 +74,7 @@
t.integer "twilio_error_code"
t.json "target"
t.integer "status", default: 0
t.integer "action_id"
t.index ["target_call_info"], name: "index_calls_on_target_call_info", using: :gin
end

Expand Down
5 changes: 5 additions & 0 deletions spec/services/call_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
expect(CallEvent::New).to receive(:publish).with(an_instance_of(Call), an_instance_of(Hash))
CallCreator.new(params).run
end

it 'creates an action' do
CallCreator.new(params).run
expect(Call.last.action).to be_an_instance_of(Action)
end
end

context 'given valid params' do
Expand Down

0 comments on commit f8ea44b

Please sign in to comment.