From 96f62b1364f5bf2d9370f32dbf9768af5a9488ec Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 20 May 2014 02:45:00 -0400 Subject: [PATCH] VRFS-1690 VRFS-1691 VRFS-1692 RSVP API work --- ruby/lib/jam_ruby/models/notification.rb | 12 +-- ruby/lib/jam_ruby/models/rsvp_request.rb | 88 ++++++++++++++++--- ruby/lib/jam_ruby/models/rsvp_slot.rb | 4 + .../jam_ruby/models/session_info_comment.rb | 9 +- .../spec/jam_ruby/models/notification_spec.rb | 15 ++-- .../api_rsvp_requests_controller.rb | 46 +++++++++- web/config/routes.rb | 2 +- web/spec/requests/rsvp_requests_api_spec.rb | 67 ++++++++++++++ 8 files changed, 212 insertions(+), 31 deletions(-) create mode 100644 web/spec/requests/rsvp_requests_api_spec.rb diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index 5c0f55bf2..9fbb8bf57 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -769,7 +769,7 @@ module JamRuby return if music_session.nil? - rsvp_requests = RsvpRequest.requests_by_session(music_session) + rsvp_requests = RsvpRequest.index(music_session) rsvp_requests.each do |rsvp| target_user = rsvp.user @@ -810,7 +810,7 @@ module JamRuby return if music_session.nil? - rsvp_requests = RsvpRequest.requests_by_session(music_session) + rsvp_requests = RsvpRequest.index(music_session) rsvp_requests.each do |rsvp| target_user = rsvp.user @@ -851,7 +851,7 @@ module JamRuby return if music_session.nil? - rsvp_requests = RsvpRequest.requests_by_session(music_session) + rsvp_requests = RsvpRequest.index(music_session) rsvp_requests.each do |rsvp| target_user = rsvp.user @@ -888,15 +888,15 @@ module JamRuby end end - def send_scheduled_session_comment(music_session, comment) + def send_scheduled_session_comment(music_session, creator, comment) return if music_session.nil? || comment.blank? - rsvp_requests = RsvpRequest.requests_by_session(music_session) + rsvp_requests = RsvpRequest.index(music_session) rsvp_requests.each do |rsvp| target_user = rsvp.user - source_user = music_session.creator + source_user = creator notification = Notification.new notification.description = NotificationTypes::SCHEDULED_SESSION_CANCELLED diff --git a/ruby/lib/jam_ruby/models/rsvp_request.rb b/ruby/lib/jam_ruby/models/rsvp_request.rb index 5f0ec12b8..84dce5c8d 100644 --- a/ruby/lib/jam_ruby/models/rsvp_request.rb +++ b/ruby/lib/jam_ruby/models/rsvp_request.rb @@ -4,19 +4,10 @@ module JamRuby belongs_to :user, :class_name => "JamRuby::User" has_many :rsvp_request_rsvp_slots, :class_name => "JamRuby::RsvpRequestRsvpSlot" has_many :rsvp_slots, :class_name => "JamRuby::RsvpSlot", :through => :rsvp_requests_rsvp_slots - - # validates :message, length: {maximum: 1000}, no_profanity: true validates :canceled, :inclusion => {:in => [nil, true, false]} - # def self.create(params) - # # slot_ids = - # rsvp = RsvpRequest.new - # # rsv - # rsvp.save - # end - - def self.requests_by_session(session, user = nil) + def self.index(session, user = nil) query = RsvpRequest .includes(:user) .joins( @@ -40,6 +31,83 @@ module JamRuby return query end + def self.create(params, user) + if rsvp_request.user_id != user.id + raise PermissionError, "Only a session invitee create the RSVP." + + RsvpRequest.transaction do + rsvp = RsvpRequest.new + + if params[:slot_ids].blank? + raise JamRuby::JamArgumentError.new("You must select at least 1 slot.") + end + + slot_ids = params[:slot_ids] + + slot_ids.each do |id| + end + rsvp.save + + Notification.send_scheduled_session_rsvp() + end + end + + def self.update(params) + + rsvp_request_id = params[:id] + + if !params[:decision].blank? + case params[:decision] + when "accept" + slot_ids = params[:slot_ids] + + slot_ids.each do |id| + request_slot = RsvpRequestRsvpSlot.where("rsvp_request_id = '#{rsvp_request_id}' AND rsvp_slot_id = '#{id}'").first + request_slot.rsvp_request_id = rsvp_request_id + request_slot.rsvp_slot_id = id + request_slot.chosen = true + request_slot.save + end + + # send notification + Notification.send_scheduled_session_rsvp_approved(music_session, user) + + when "reject" + + end + + else + raise JamRuby::JamArgumentError.new("Invalid request.") + end + end + + def self.cancel(rsvp_request, music_session, user, message) + if music_session.creator.id != user.id && rsvp_request.user_id != user.id + raise PermissionError, "Only the session organizer or RSVP creator can cancel the RSVP." + + RsvpRequest.transaction do + + # mark corresponding slot's chosen field as false + rsvp_request_slots = RsvpRequestRsvpSlot.find("rsvp_request_id = '#{rsvp_request.id}'") + + rsvp_request_slots.each do |slot| + if slot.chosen + slot.chosen = false + slot.save + end + end + + # send notification + if music_session.creator.id == user.id + Notification.send_scheduled_session_rsvp_cancelled_org(music_session, user) + else + Notification.send_scheduled_session_rsvp_cancelled(music_session, user) + end + + Notification.send_scheduled_session_comment(music_session, user, message) + end + end + # XXX we need to validate that only one RsvpRequest.chosen = true for a given RsvpSlot # in other words, you can have many requests to a slot, but only 0 or 1 rsvp_request.chosen = true) end diff --git a/ruby/lib/jam_ruby/models/rsvp_slot.rb b/ruby/lib/jam_ruby/models/rsvp_slot.rb index 274cf21bd..a0a5f3b86 100644 --- a/ruby/lib/jam_ruby/models/rsvp_slot.rb +++ b/ruby/lib/jam_ruby/models/rsvp_slot.rb @@ -7,5 +7,9 @@ module JamRuby has_many :rsvp_requests, :class_name => "JamRuby::RsvpRequest", :through => :rsvp_requests_rsvp_slots # TODO: validates :proficiency_level + + def is_chosen + RsvpRequestRsvpSlot.exists?("chosen=true AND rsvp_slot_id='#{self.id}") + end end end diff --git a/ruby/lib/jam_ruby/models/session_info_comment.rb b/ruby/lib/jam_ruby/models/session_info_comment.rb index 58e7b75eb..fa0833807 100644 --- a/ruby/lib/jam_ruby/models/session_info_comment.rb +++ b/ruby/lib/jam_ruby/models/session_info_comment.rb @@ -7,13 +7,10 @@ module JamRuby default_scope order('created_at DESC') - belongs_to(:music_session, - :class_name => "JamRuby::MusicSession", - :foreign_key => "music_session_id") + belongs_to(:music_session, :class_name => "JamRuby::MusicSession", :foreign_key => "music_session_id") + belongs_to(:user, :class_name => "JamRuby::User", :foreign_key => "creator_id") - belongs_to(:user, - :class_name => "JamRuby::User", - :foreign_key => "creator_id") + # validates :comment, length: {maximum: 1000}, no_profanity: true end end \ No newline at end of file diff --git a/ruby/spec/jam_ruby/models/notification_spec.rb b/ruby/spec/jam_ruby/models/notification_spec.rb index 8b33f3122..bbb9335f5 100644 --- a/ruby/spec/jam_ruby/models/notification_spec.rb +++ b/ruby/spec/jam_ruby/models/notification_spec.rb @@ -170,7 +170,7 @@ describe Notification do it "sends no notification if session is nil" do sender = FactoryGirl.create(:user) calls = count_publish_to_user_calls - notification = Notification.send_scheduled_session_rsvp(nil, sender, nil) + notification = Notification.send_scheduled_session_rsvp(nil, sender, 'Blah', nil) UserMailer.deliveries.length.should == 0 calls[:count].should == 0 @@ -179,7 +179,7 @@ describe Notification do it "sends no notification if user is nil" do session = FactoryGirl.create(:music_session) calls = count_publish_to_user_calls - notification = Notification.send_scheduled_session_rsvp(session, nil, nil) + notification = Notification.send_scheduled_session_rsvp(session, nil, 'Blah', nil) UserMailer.deliveries.length.should == 0 calls[:count].should == 0 @@ -222,7 +222,7 @@ describe Notification do it "sends no notification if session is nil" do sender = FactoryGirl.create(:user) calls = count_publish_to_user_calls - notification = Notification.send_scheduled_session_rsvp_cancelled(nil, sender) + notification = Notification.send_scheduled_session_rsvp_cancelled(nil, sender, 'Blah') UserMailer.deliveries.length.should == 0 calls[:count].should == 0 @@ -231,7 +231,7 @@ describe Notification do it "sends no notification if user is nil" do session = FactoryGirl.create(:music_session) calls = count_publish_to_user_calls - notification = Notification.send_scheduled_session_rsvp_cancelled(session, nil) + notification = Notification.send_scheduled_session_rsvp_cancelled(session, nil, 'Blah') UserMailer.deliveries.length.should == 0 calls[:count].should == 0 @@ -248,7 +248,7 @@ describe Notification do it "sends no notification if session is nil" do receiver = FactoryGirl.create(:user) calls = count_publish_to_user_calls - notification = Notification.send_scheduled_session_rsvp_cancelled_org(nil, receiver) + notification = Notification.send_scheduled_session_rsvp_cancelled_org(nil, receiver, 'Blah') UserMailer.deliveries.length.should == 0 calls[:count].should == 0 @@ -257,7 +257,7 @@ describe Notification do it "sends no notification if user is nil" do session = FactoryGirl.create(:music_session) calls = count_publish_to_user_calls - notification = Notification.send_scheduled_session_rsvp_cancelled_org(session, nil) + notification = Notification.send_scheduled_session_rsvp_cancelled_org(session, nil, 'Blah') UserMailer.deliveries.length.should == 0 calls[:count].should == 0 @@ -355,9 +355,10 @@ describe Notification do end it "sends no notification if comment is empty" do + sender = FactoryGirl.create(:user) session = FactoryGirl.create(:music_session) calls = count_publish_to_user_calls - notification = Notification.send_scheduled_session_comment(session, '') + notification = Notification.send_scheduled_session_comment(session, sender, '') UserMailer.deliveries.length.should == 0 calls[:count].should == 0 diff --git a/web/app/controllers/api_rsvp_requests_controller.rb b/web/app/controllers/api_rsvp_requests_controller.rb index 167fd4da7..2940d5402 100644 --- a/web/app/controllers/api_rsvp_requests_controller.rb +++ b/web/app/controllers/api_rsvp_requests_controller.rb @@ -1,9 +1,53 @@ class ApiRsvpRequestsController < ApiController + before_filter :auth_user + respond_to :json - def create + def index + if params[:session_id].blank? + render :json => {:message => "Session ID is required"}, :status => 400 + else + music_session = MusicSession.find(params[:session_id]) + # retrieve all requests for this session + if music_session.creator.id == current_user.id + @rsvp_requests = RsvpRequest.index(music_session) + + # scope the response to the current user + else + @rsvp_requests = RsvpRequest.index(music_session, current_user) + end + + respond_with @rsvp_requests, responder: ApiResponder, :status => 200 + + end + end + + def create + if params[:id].blank? || params[:session_id].blank? + render :json => {:message => "Session ID is required."}, :status => 400 + else + music_session = MusicSession.find(params[:session_id]) + @rsvp = RsvpRequest.create(params, current_user) + respond_with @rsvp, responder: ApiResponder, :status => 201 + end + end + + def show + @rsvp_request = RsvpRequest.find(params[:id]) + respond_with @rsvp_request, responder: ApiResponder, :status => 200 + end + + def destroy + if params[:id].blank? || params[:session_id].blank? + render :json => {:message => "RSVP request ID and session ID are required."}, :status => 400 + else + music_session = MusicSession.find(params[:session_id]) + rsvp_request = RsvpRequest.find(params[:id]) + RsvpRequest.cancel(rsvp_request, music_session, current_user, params[:message]) + respond_with responder: ApiResponder, :status => 204 + end end end \ No newline at end of file diff --git a/web/config/routes.rb b/web/config/routes.rb index bd6961167..4f285a4c2 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -167,7 +167,7 @@ SampleApp::Application.routes.draw do match '/sessions/:id/tracks/:track_id' => 'api_music_sessions#track_destroy', :via => :delete # RSVP requests - match '/sessions/:id/rsvp_requests' => 'api_music_sessions#rsvp_requests_index', :via => :get + match '/rsvp_requests' => 'api_rsvp_requests#rsvp_requests_index', :via => :get match '/rsvp_requests' => 'api_rsvp_requests#create', :via => :post match '/rsvp_requests/:id' => 'api_rsvp_requests#show', :via => :get, :as => 'api_rsvp_request_detail' match '/rsvp_requests/:id' => 'api_rsvp_requests#destroy', :via => :delete diff --git a/web/spec/requests/rsvp_requests_api_spec.rb b/web/spec/requests/rsvp_requests_api_spec.rb new file mode 100644 index 000000000..56d3b26bc --- /dev/null +++ b/web/spec/requests/rsvp_requests_api_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe "RSVP Request API ", :type => :api do + + include Rack::Test::Methods + + subject { page } + + before(:each) do + MusicSession.delete_all + end + + describe "index" do + it "should prevent request without session ID" do + end + + it "should allow session creator to view all" do + end + + it "should allow RSVP creator to view only his list" do + end + + it "should allow others to view list" do + end + end + + + describe "create" do + it "should allow session invitee to create RSVP" do + end + + it "should not allow non-invitee to create RSVP" do + end + + it "should require at least 1 slot selection" do + end + + it "should prevent RSVP for chosen slot" do + end + end + + describe "show" do + it "should allow RSVP creator to view" do + end + + it "should allow session creator to view" do + end + + it "should allow anyone else to view" do + end + end + + describe "destroy" do + it "should allow RSVP creator to cancel" do + end + + it "should allow session creator to cancel" do + end + + it "should not allow anyone else to cancel" do + end + + it "should set chosen to false on all slots" do + end + end + +end \ No newline at end of file