diff --git a/db/manifest b/db/manifest index bfafbbbd0..2143f3c0b 100755 --- a/db/manifest +++ b/db/manifest @@ -157,4 +157,5 @@ notification_scheduled_session.sql music_notation.sql music_session_recurring_mode.sql add_timezone_music_session.sql -scheduled_sessions_2.sql \ No newline at end of file +scheduled_sessions_2.sql +scheduled_sessions_3.sql \ No newline at end of file diff --git a/db/up/scheduled_sessions_3.sql b/db/up/scheduled_sessions_3.sql new file mode 100644 index 000000000..1f88683a1 --- /dev/null +++ b/db/up/scheduled_sessions_3.sql @@ -0,0 +1 @@ +alter table rsvp_requests_rsvp_slots alter column chosen set DEFAULT NULL; \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index 06b33b7b6..052712ad1 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -883,7 +883,7 @@ module JamRuby source_user = creator notification = Notification.new - notification.description = NotificationTypes::SCHEDULED_SESSION_CANCELLED + notification.description = NotificationTypes::SCHEDULED_SESSION_COMMENT notification.source_user_id = source_user.id notification.target_user_id = target_user.id notification.session_id = music_session.id @@ -894,6 +894,7 @@ module JamRuby msg = @@message_factory.scheduled_session_comment( target_user.id, music_session.id, + target_user.photo_url, notification_msg, comment, music_session.description, diff --git a/ruby/lib/jam_ruby/models/rsvp_request.rb b/ruby/lib/jam_ruby/models/rsvp_request.rb index 3c828604d..20ff2b65f 100644 --- a/ruby/lib/jam_ruby/models/rsvp_request.rb +++ b/ruby/lib/jam_ruby/models/rsvp_request.rb @@ -36,7 +36,7 @@ module JamRuby # verify music session exists if music_session.nil? - raise JamRuby::JamArgumentError.new("Invalid session.") + raise StateError, "Invalid session." end # verify invitation exists for this user and session @@ -48,7 +48,7 @@ module JamRuby # verify slot IDs exist in request if params[:rsvp_slots].blank? - raise JamArgumentError, "You must select at least 1 slot." + raise StateError, "You must select at least 1 slot." end RsvpRequest.transaction do @@ -61,21 +61,31 @@ module JamRuby # for each slot requested, do the following: # (1) verify slot exists in db # (2) verify slot is not already chosen - # (3) create RsvpRequestRsvpSlot - # (4) create RsvpRequest + # (3) verify user has not already requested this slot + # (4) create RsvpRequestRsvpSlot + # (5) create RsvpRequest slot_ids.each do |id| rsvp_slot = RsvpSlot.where(:id => id).first # verify slot exists in db if rsvp_slot.nil? - raise JamRuby::JamArgumentError.new("Invalid slot #{id}.") + raise StateError, "Invalid slot #{id}." + end + + # verify user has not already submitted RSVP request for this slot + user_slot = RsvpRequest.joins(:rsvp_requests_rsvp_slots) + .where(:user_id => user.id) + .where(rsvp_requests_rsvp_slots: {rsvp_slot_id: id}) + + if !user_slot.blank? + raise StateError, "You have already submitted an RSVP request for this slot." end chosen_slot = rsvp_slot.rsvp_requests_rsvp_slots.where("chosen = true").first # verify this slot was not already chosen if !chosen_slot.nil? - raise JamRuby::JamArgumentError.new("The #{rsvp_slot.instrument_id} slot has already been approved by the session organizer.") + raise StateError, "The #{rsvp_slot.instrument_id} slot has already been approved by the session organizer." else rsvp_request_rsvp_slot = RsvpRequestRsvpSlot.new rsvp_request_rsvp_slot.rsvp_request = @rsvp @@ -107,11 +117,11 @@ module JamRuby rsvp_request_id = params[:id] - music_session = MusicSession.find(params[:session_id]) + music_session = MusicSession.find_by_id(params[:session_id]) # verify music session exists if music_session.nil? - raise JamRuby::JamArgumentError.new("Invalid session.") + raise StateError, "Invalid session." end # authorize the user attempting to respond to the RSVP request @@ -119,27 +129,54 @@ module JamRuby raise PermissionError, "Only the session organizer can accept or decline and RSVP request." end - RsvpRequest.transaction do - if !params[:rsvp_response].blank? - - instruments = [] + rsvp_request = RsvpRequest.find_by_id(rsvp_request_id) - responses = params[:rsvp_response] - responses.each do |r| - if r.accept - request_slot = RsvpRequestRsvpSlot.find(r.request_slot_id) + if rsvp_request.nil? + raise StateError, "Invalid RSVP request." + end + + RsvpRequest.transaction do + rsvp_responses = params[:rsvp_responses] + if !rsvp_responses.blank? + instruments = [] + accepted_slot = false + + rsvp_responses.each do |r| + request_slot_id = r[:request_slot_id] + request_slot = RsvpRequestRsvpSlot.find_by_id(request_slot_id) + if request_slot.nil? + raise StateError, "Invalid request slot #{request_slot_id}." + end + + rsvp_slot = RsvpSlot.find_by_id(request_slot.rsvp_slot_id) + if rsvp_slot.nil? + raise StateError, "Slot does not exist" + end + + if rsvp_slot.chosen + raise StateError, "The #{rsvp_slot.instrument_id} slot has already been approved for another user." + end + + if r[:accept] + accepted_slot = true request_slot.chosen = true request_slot.save - rsvp_slot = RsvpSlot.find(request_slot.rsvp_slot_id) instruments << rsvp_slot.instrument_id + + else + request_slot.chosen = false + request_slot.save end end - Notification.send_scheduled_session_rsvp_approved(music_session, user, instruments) + # send notification if at least 1 slot was approved + if accepted_slot + Notification.send_scheduled_session_rsvp_approved(music_session, user, instruments) + end else - raise JamRuby::JamArgumentError.new("Invalid request.") + raise StateError, "Invalid request." end end end diff --git a/ruby/lib/jam_ruby/models/rsvp_slot.rb b/ruby/lib/jam_ruby/models/rsvp_slot.rb index 412aa5f33..3f92833ee 100644 --- a/ruby/lib/jam_ruby/models/rsvp_slot.rb +++ b/ruby/lib/jam_ruby/models/rsvp_slot.rb @@ -18,5 +18,11 @@ module JamRuby chosen_slots = RsvpRequestRsvpSlot.where("chosen = true AND rsvp_slot_id = ?", self.id) !chosen_slots.blank? end + + # def has_rsvp_from_user(user) + # user_slot = RsvpRequest.joins(:rsvp_requests_rsvp_slots) + # .where(:rsvp_request_id => ) + # .where(:user_id => user.id) + # end end end