jam-cloud/ruby/lib/jam_ruby/models/rsvp_request.rb

116 lines
3.5 KiB
Ruby

module JamRuby
class RsvpRequest < ActiveRecord::Base
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 :canceled, :inclusion => {:in => [nil, true, false]}
def self.index(session, user = nil)
query = RsvpRequest
.includes(:user)
.joins(
%Q{
INNER JOIN
rsvp_requests_rsvp_slots rrrs
ON
rrrs.rsvp_request_id = rsvp_requests.id
INNER JOIN
rsvp_slots rs
ON rs.id = rrrs.rsvp_slot_id
}
)
.where(
%Q{
rs.music_session_id = '#{session.id}'
}
)
query = query.where("rsvp_requests.user_id = '#{user.id}'") unless user.nil?
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
end