diff --git a/app/controllers/checkouts_controller.rb b/app/controllers/checkouts_controller.rb index c6a5f554..923fde83 100644 --- a/app/controllers/checkouts_controller.rb +++ b/app/controllers/checkouts_controller.rb @@ -160,8 +160,6 @@ def uncheckin format.html { redirect_to tool_path(checkout.tool), notice: "Error" } end end - - end @@ -205,5 +203,6 @@ def checkin_bak end end end + end diff --git a/app/models/checkout.rb b/app/models/checkout.rb index d3593823..96c2b6f1 100644 --- a/app/models/checkout.rb +++ b/app/models/checkout.rb @@ -22,6 +22,8 @@ # class Checkout < ActiveRecord::Base + include Messenger + # For lookups def card_number=( card_number ) @card_number = card_number @@ -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 @@ -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 \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 6ca421a0..37c32fbd 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/lib/messenger.rb b/lib/messenger.rb index 3cc461f3..11de1700 100644 --- a/lib/messenger.rb +++ b/lib/messenger.rb @@ -1,3 +1,5 @@ +USER_UNSUBSCRIBED_FROM_TWILIO_ERROR_CODE = 21610 + module Messenger def send_sms(number, content) @@ -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 \ No newline at end of file