Skip to content

Commit

Permalink
Bl/waitlist notify (#254)
Browse files Browse the repository at this point in the history
* added twilio messenger module, also tweaked the application config to autoload the module

* tried to modified messenger with text processing but failed

* added code to send out text alert to the next person on the wait list after a tool is checked back in

* added a safeguard for phone number in case someone has not put their phonenumber in the system yet

* updated messenger module with try-rescue block

* commented out code for reply forwarding. There is an issue for the forwarding and it seems to be security related. Need to discuss it with people more knowledgable before implemeting this feature

* removed commented code. Will probably add it to a seperate branch. Just trying to merge this code to goat branch

* modified messenger to keep track of error code
  • Loading branch information
blinblinblin authored and eleanorwilson committed Apr 9, 2017
1 parent ddca290 commit 8dc4dc7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
3 changes: 1 addition & 2 deletions app/controllers/checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ def uncheckin
format.html { redirect_to tool_path(checkout.tool), notice: "Error" }
end
end


end


Expand Down Expand Up @@ -205,5 +203,6 @@ def checkin_bak
end
end
end

end

22 changes: 21 additions & 1 deletion app/models/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#

class Checkout < ActiveRecord::Base
include Messenger

# For lookups
def card_number=( card_number )
@card_number = card_number
Expand All @@ -35,6 +37,7 @@ def card_number
validates_associated :tool, :organization, :participant

before_save :checked_out_at, :presence => true
after_update :notify

belongs_to :participant, :touch => true
belongs_to :organization, :touch => true
Expand All @@ -44,4 +47,21 @@ def card_number
scope :old, -> { where('checked_in_at IS NOT NULL') }
scope :current, -> { where('checked_in_at IS NULL') }

end
private

def notify
if (self.checked_in_at != nil)
toolCategory = self.tool.tool_type
waitlist = ToolWaitlist.for_tool_type(toolCategory.id).by_wait_start_time
if (waitlist.count != 0)
nextPerson = waitlist.first.participant
unless (nextPerson.phone_number.blank?)
number = nextPerson.phone_number
content = "#{toolCategory.name} is now available at the trailer. Please come pick it up within 5 minutes!"
send_sms(number, content)
end
end
end
end

end
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class Application < Rails::Application

config.autoload_paths += %W(#{config.root}/lib)

config.active_job.queue_adapter = :delayed_job

config.active_job.queue_adapter = :delayed_job

WillPaginate::ViewHelpers.pagination_options[:inner_window] = 1
WillPaginate::ViewHelpers.pagination_options[:outer_window] = 0
end
Expand Down
29 changes: 19 additions & 10 deletions lib/messenger.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
USER_UNSUBSCRIBED_FROM_TWILIO_ERROR_CODE = 21610

module Messenger

def send_sms(number, content)
Expand All @@ -6,17 +8,24 @@ def send_sms(number, content)

@client = Twilio::REST::Client.new sid, auth

from = "+14123854063 "

message = @client.account.messages.create(
:from => from,
:to => '+1'+number,
:body => content
)
end
from = "+14123854063"

def receive_sms
# The following try-rescue block is needed in case user unsubscribe
# if the user ubsubscribe and we attempt to message them
# the api will report an error

begin
message = @client.account.messages.create(
:from => from,
:to => '+1'+number,
:body => content
)
rescue Twilio::REST::RequestError => e
case e.code
when USER_UNSUBSCRIBED_FROM_TWILIO_ERROR_CODE
puts e.message
end
end
end

end

0 comments on commit 8dc4dc7

Please sign in to comment.