2014-05-14 20:48:42 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class RsvpRequest < ActiveRecord::Base
|
|
|
|
|
|
|
|
|
|
belongs_to :user, :class_name => "JamRuby::User"
|
2014-05-21 04:57:32 +00:00
|
|
|
has_many :rsvp_requests_rsvp_slots, :class_name => "JamRuby::RsvpRequestRsvpSlot", :foreign_key => "rsvp_request_id"
|
2014-05-20 03:34:56 +00:00
|
|
|
has_many :rsvp_slots, :class_name => "JamRuby::RsvpSlot", :through => :rsvp_requests_rsvp_slots
|
2014-07-07 21:08:36 +00:00
|
|
|
|
|
|
|
|
validates :user, presence: true
|
2014-05-14 20:48:42 +00:00
|
|
|
validates :canceled, :inclusion => {:in => [nil, true, false]}
|
|
|
|
|
|
2014-07-07 21:08:36 +00:00
|
|
|
# pulls all instruments from the associated rsvp_slots
|
|
|
|
|
def instrument_list
|
|
|
|
|
rsvp_slots.map(&:instrument)
|
|
|
|
|
end
|
|
|
|
|
|
2014-06-16 05:46:52 +00:00
|
|
|
def self.index(music_session, user=nil, options={})
|
2014-05-18 04:09:21 +00:00
|
|
|
query = RsvpRequest
|
2014-05-21 05:36:32 +00:00
|
|
|
.includes(:user)
|
2014-05-18 04:09:21 +00:00
|
|
|
.joins(
|
2014-05-18 18:59:57 +00:00
|
|
|
%Q{
|
2014-05-20 03:34:56 +00:00
|
|
|
INNER JOIN
|
|
|
|
|
rsvp_requests_rsvp_slots rrrs
|
2014-05-18 18:59:57 +00:00
|
|
|
ON
|
2014-05-20 03:34:56 +00:00
|
|
|
rrrs.rsvp_request_id = rsvp_requests.id
|
|
|
|
|
INNER JOIN
|
|
|
|
|
rsvp_slots rs
|
|
|
|
|
ON rs.id = rrrs.rsvp_slot_id
|
2014-05-18 18:59:57 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.where(
|
|
|
|
|
%Q{
|
2014-05-21 05:36:32 +00:00
|
|
|
rs.music_session_id = '#{music_session.id}'
|
2014-05-18 18:59:57 +00:00
|
|
|
}
|
|
|
|
|
)
|
2014-05-18 04:09:21 +00:00
|
|
|
|
2014-06-16 05:46:52 +00:00
|
|
|
if options[:status] == 'approved'
|
|
|
|
|
query = query.where("rrrs.chosen = true")
|
|
|
|
|
elsif options[:status] == 'pending'
|
|
|
|
|
query = query.where("rrrs.chosen is null")
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-21 05:36:32 +00:00
|
|
|
query = query.where("rsvp_requests.user_id = ?", user.id) unless user.nil?
|
2014-05-21 04:57:32 +00:00
|
|
|
return query.uniq
|
2014-05-18 04:09:21 +00:00
|
|
|
end
|
2014-05-14 20:48:42 +00:00
|
|
|
|
2014-05-20 06:45:00 +00:00
|
|
|
def self.create(params, user)
|
2014-05-21 21:47:11 +00:00
|
|
|
music_session = MusicSession.find_by_id(params[:session_id])
|
2014-05-21 04:57:32 +00:00
|
|
|
|
|
|
|
|
# verify music session exists
|
|
|
|
|
if music_session.nil?
|
2014-05-22 03:36:34 +00:00
|
|
|
raise StateError, "Invalid session."
|
2014-05-21 04:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
2014-06-07 17:01:03 +00:00
|
|
|
# verify slot IDs exist in request
|
|
|
|
|
if params[:rsvp_slots].blank?
|
|
|
|
|
raise StateError, "You must select at least 1 slot."
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
# verify invitation exists for this user and session
|
2014-05-21 05:36:32 +00:00
|
|
|
invitation = Invitation.where("music_session_id = ? AND receiver_id = ?", music_session.id, user.id)
|
2014-05-21 04:57:32 +00:00
|
|
|
|
2014-06-19 02:32:21 +00:00
|
|
|
if invitation.first.nil? && !music_session.open_rsvps && music_session.creator.id != user.id
|
2014-05-29 03:43:57 +00:00
|
|
|
raise PermissionError, "Only a session invitee can create an RSVP for this session."
|
2014-05-21 04:57:32 +00:00
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
|
|
|
|
|
RsvpRequest.transaction do
|
2014-05-21 04:57:32 +00:00
|
|
|
@rsvp = RsvpRequest.new
|
2014-05-21 21:47:11 +00:00
|
|
|
@rsvp.user = user
|
2014-05-20 06:45:00 +00:00
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
slot_ids = params[:rsvp_slots]
|
2014-05-29 03:43:57 +00:00
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
instruments = []
|
|
|
|
|
|
|
|
|
|
# for each slot requested, do the following:
|
|
|
|
|
# (1) verify slot exists in db
|
|
|
|
|
# (2) verify slot is not already chosen
|
2014-05-22 03:36:34 +00:00
|
|
|
# (3) verify user has not already requested this slot
|
|
|
|
|
# (4) create RsvpRequestRsvpSlot
|
|
|
|
|
# (5) create RsvpRequest
|
2014-05-20 06:45:00 +00:00
|
|
|
slot_ids.each do |id|
|
2014-05-21 04:57:32 +00:00
|
|
|
rsvp_slot = RsvpSlot.where(:id => id).first
|
|
|
|
|
|
|
|
|
|
# verify slot exists in db
|
|
|
|
|
if rsvp_slot.nil?
|
2014-05-22 03:36:34 +00:00
|
|
|
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."
|
2014-05-21 04:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
chosen_slot = rsvp_slot.rsvp_requests_rsvp_slots.where("chosen = true").first
|
|
|
|
|
|
|
|
|
|
# verify this slot was not already chosen
|
|
|
|
|
if !chosen_slot.nil?
|
2014-05-22 03:36:34 +00:00
|
|
|
raise StateError, "The #{rsvp_slot.instrument_id} slot has already been approved by the session organizer."
|
2014-05-21 04:57:32 +00:00
|
|
|
else
|
|
|
|
|
rsvp_request_rsvp_slot = RsvpRequestRsvpSlot.new
|
2014-05-21 21:47:11 +00:00
|
|
|
rsvp_request_rsvp_slot.rsvp_request = @rsvp
|
2014-05-21 04:57:32 +00:00
|
|
|
rsvp_request_rsvp_slot.rsvp_slot = rsvp_slot
|
2014-06-16 12:11:05 +00:00
|
|
|
rsvp_request_rsvp_slot.chosen = true if params[:autoapprove] == true
|
2014-05-21 04:57:32 +00:00
|
|
|
rsvp_request_rsvp_slot.save
|
|
|
|
|
|
|
|
|
|
instruments << rsvp_slot.instrument_id
|
|
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
end
|
|
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
@rsvp.save
|
|
|
|
|
|
2014-05-21 21:47:11 +00:00
|
|
|
unless params[:message].blank?
|
|
|
|
|
session_info_comment = SessionInfoComment.new
|
|
|
|
|
session_info_comment.music_session = music_session
|
|
|
|
|
session_info_comment.user = user
|
|
|
|
|
session_info_comment.comment = params[:message]
|
|
|
|
|
session_info_comment.save
|
2014-06-10 04:35:38 +00:00
|
|
|
Notification.send_scheduled_session_comment(music_session, user, params[:message])
|
2014-05-21 21:47:11 +00:00
|
|
|
end
|
|
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
Notification.send_scheduled_session_rsvp(music_session, user, instruments)
|
|
|
|
|
|
|
|
|
|
@rsvp
|
2014-05-20 06:45:00 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
def self.update(params, user)
|
2014-05-20 06:45:00 +00:00
|
|
|
|
|
|
|
|
rsvp_request_id = params[:id]
|
|
|
|
|
|
2014-05-22 03:36:34 +00:00
|
|
|
music_session = MusicSession.find_by_id(params[:session_id])
|
2014-05-20 06:45:00 +00:00
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
# verify music session exists
|
|
|
|
|
if music_session.nil?
|
2014-05-22 03:36:34 +00:00
|
|
|
raise StateError, "Invalid session."
|
2014-05-21 04:57:32 +00:00
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
# authorize the user attempting to respond to the RSVP request
|
|
|
|
|
if music_session.creator.id != user.id
|
|
|
|
|
raise PermissionError, "Only the session organizer can accept or decline and RSVP request."
|
|
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
|
2014-05-22 03:36:34 +00:00
|
|
|
rsvp_request = RsvpRequest.find_by_id(rsvp_request_id)
|
|
|
|
|
|
|
|
|
|
if rsvp_request.nil?
|
|
|
|
|
raise StateError, "Invalid RSVP request."
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
RsvpRequest.transaction do
|
2014-05-22 03:36:34 +00:00
|
|
|
rsvp_responses = params[:rsvp_responses]
|
|
|
|
|
if !rsvp_responses.blank?
|
2014-05-21 04:57:32 +00:00
|
|
|
instruments = []
|
2014-05-22 03:36:34 +00:00
|
|
|
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
|
|
|
|
|
|
2014-06-13 07:45:45 +00:00
|
|
|
if rsvp_slot.chosen && r[:accept]
|
2014-05-22 03:36:34 +00:00
|
|
|
raise StateError, "The #{rsvp_slot.instrument_id} slot has already been approved for another user."
|
|
|
|
|
end
|
2014-05-21 04:57:32 +00:00
|
|
|
|
2014-05-22 03:36:34 +00:00
|
|
|
if r[:accept]
|
|
|
|
|
accepted_slot = true
|
2014-05-21 04:57:32 +00:00
|
|
|
request_slot.chosen = true
|
|
|
|
|
request_slot.save
|
|
|
|
|
|
|
|
|
|
instruments << rsvp_slot.instrument_id
|
2014-05-22 03:36:34 +00:00
|
|
|
|
|
|
|
|
else
|
|
|
|
|
request_slot.chosen = false
|
|
|
|
|
request_slot.save
|
2014-05-21 04:57:32 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-22 03:36:34 +00:00
|
|
|
# send notification if at least 1 slot was approved
|
|
|
|
|
if accepted_slot
|
|
|
|
|
Notification.send_scheduled_session_rsvp_approved(music_session, user, instruments)
|
|
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
|
2014-05-21 04:57:32 +00:00
|
|
|
else
|
2014-05-22 03:36:34 +00:00
|
|
|
raise StateError, "Invalid request."
|
2014-05-21 04:57:32 +00:00
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-05-22 04:40:17 +00:00
|
|
|
def self.cancel(params, user)
|
|
|
|
|
if params[:id].blank?
|
|
|
|
|
raise StateError, "RSVP request ID is required."
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if params[:session_id].blank?
|
|
|
|
|
raise StateError, "Session ID is required."
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
music_session = MusicSession.find(params[:session_id])
|
|
|
|
|
rsvp_request = RsvpRequest.find(params[:id])
|
|
|
|
|
|
2014-05-20 06:45:00 +00:00
|
|
|
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."
|
2014-05-21 04:57:32 +00:00
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
|
|
|
|
|
RsvpRequest.transaction do
|
|
|
|
|
|
2014-05-22 04:40:17 +00:00
|
|
|
case params[:cancelled]
|
|
|
|
|
when 'yes'
|
|
|
|
|
rsvp_request.canceled = true
|
|
|
|
|
rsvp_request.cancel_all = false
|
|
|
|
|
|
|
|
|
|
when 'all'
|
|
|
|
|
rsvp_request.canceled = true
|
|
|
|
|
rsvp_request.cancel_all = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
rsvp_request.save
|
|
|
|
|
|
2014-05-20 06:45:00 +00:00
|
|
|
# mark corresponding slot's chosen field as false
|
2014-05-22 04:40:17 +00:00
|
|
|
rsvp_request_slots = RsvpRequestRsvpSlot.where("rsvp_request_id = ?", rsvp_request.id)
|
2014-05-20 06:45:00 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2014-05-22 04:40:17 +00:00
|
|
|
unless params[:message].blank?
|
|
|
|
|
session_info_comment = SessionInfoComment.new
|
|
|
|
|
session_info_comment.music_session = music_session
|
|
|
|
|
session_info_comment.user = user
|
|
|
|
|
session_info_comment.comment = params[:message]
|
|
|
|
|
session_info_comment.save
|
2014-06-10 04:35:38 +00:00
|
|
|
Notification.send_scheduled_session_comment(music_session, user, params[:message])
|
2014-05-22 04:40:17 +00:00
|
|
|
end
|
2014-05-20 06:45:00 +00:00
|
|
|
end
|
|
|
|
|
end
|
2014-05-14 20:48:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|