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

Bl/waitlist notify #254

Merged
9 commits merged into from
Apr 9, 2017
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're calling this after_update, but is the only reason this could ever get updated a check-in? Otherwise you'll be sending this randomly. I think it would be much better to make this part of the controller. That way you could even add a "notify next" button on the frontend and have the controller action half-wired up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you are saying here @ChaseBro, but if I'm not mistaken, as it stands after the check in is the only reason this should be updated. Given the proximity to Carnival, I think we should go with it and open a high priority issue / bug. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened issue #256 about this issue

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