211 lines
6.5 KiB
Ruby
211 lines
6.5 KiB
Ruby
# represenst the type of lesson package
|
|
module JamRuby
|
|
class LessonSession < ActiveRecord::Base
|
|
|
|
|
|
attr_accessor :accepting, :creating, :countering, :countered_slot, :countered_lesson
|
|
|
|
@@log = Logging.logger[LessonSession]
|
|
|
|
STATUS_REQUESTED = 'requested'
|
|
STATUS_CANCELED = 'canceled'
|
|
STATUS_MISSED = 'missed'
|
|
STATUS_COMPLETED = 'completed'
|
|
STATUS_APPROVED = 'approved'
|
|
|
|
STATUS_TYPES = [STATUS_REQUESTED, STATUS_CANCELED, STATUS_MISSED, STATUS_COMPLETED, STATUS_APPROVED]
|
|
|
|
LESSON_TYPE_SINGLE = 'paid'
|
|
LESSON_TYPE_SINGLE_FREE = 'single-free'
|
|
LESSON_TYPE_TEST_DRIVE = 'test-drive'
|
|
LESSON_TYPES = [LESSON_TYPE_SINGLE, LESSON_TYPE_SINGLE_FREE, LESSON_TYPE_TEST_DRIVE]
|
|
|
|
has_one :music_session, class_name: "JamRuby::MusicSession"
|
|
belongs_to :teacher, class_name: "JamRuby::User"
|
|
belongs_to :lesson_package_purchase, class_name: "JamRuby::LessonPackagePurchase"
|
|
belongs_to :lesson_booking, class_name: "JamRuby::LessonBooking"
|
|
belongs_to :slot, class_name: "JamRuby::LessonBookingSlot", foreign_key: :slot_id
|
|
has_many :lesson_booking_slots, class_name: "JamRuby::LessonBookingSlot"
|
|
|
|
validates :duration, presence: true, numericality: {only_integer: true}
|
|
validates :lesson_booking, presence: true
|
|
validates :lesson_type, inclusion: {in: LESSON_TYPES}
|
|
validates :booked_price, presence: true
|
|
validates :status, presence: true, inclusion: {in: STATUS_TYPES}
|
|
validates :teacher_complete, inclusion: {in: [true, false]}
|
|
validates :student_complete, inclusion: {in: [true, false]}
|
|
validates :teacher_canceled, inclusion: {in: [true, false]}
|
|
validates :student_canceled, inclusion: {in: [true, false]}
|
|
|
|
validate :validate_creating, :if => :creating
|
|
validate :validate_accepted, :if => :accepting
|
|
after_save :after_counter, :if => :countering
|
|
after_save :manage_slot_changes, :if => :slot_changed?
|
|
|
|
def manage_slot_changes
|
|
# if this slot changed, we need to update the time. But LessonBooking does this for us, for requested/accepted .
|
|
# TODO: what to do, what to do.
|
|
end
|
|
|
|
def after_counter
|
|
send_counter(@countered_lesson, @countered_slot)
|
|
end
|
|
|
|
def send_counter(countered_lesson, countered_slot)
|
|
if countered_slot.is_teacher_created?
|
|
UserMailer.student_lesson_counter(countered_lesson, countered_slot).deliver
|
|
else
|
|
UserMailer.teacher_lesson_counter(countered_lesson, countered_slot).deliver
|
|
end
|
|
self.countering = false
|
|
end
|
|
|
|
default_scope { order(:created_at) }
|
|
|
|
def is_requested?
|
|
status == STATUS_REQUESTED
|
|
end
|
|
def is_canceled?
|
|
status == STATUS_CANCELED
|
|
end
|
|
def is_completed?
|
|
status == STATUS_COMPLETED
|
|
end
|
|
def is_missed?
|
|
status == STATUS_MISSED
|
|
end
|
|
def is_approved?
|
|
status == STATUS_APPROVED
|
|
end
|
|
|
|
|
|
def validate_creating
|
|
if !is_requested? && !is_approved?
|
|
self.errors.add(:status, "is not valid for a new lesson session.")
|
|
end
|
|
end
|
|
|
|
def validate_accepted
|
|
if !is_requested?
|
|
self.errors.add(:status, "This session is already #{self.status}.")
|
|
end
|
|
|
|
self.accepting = false
|
|
self.status = STATUS_APPROVED
|
|
end
|
|
|
|
def self.create(booking)
|
|
lesson_session = LessonSession.new
|
|
lesson_session.creating = true
|
|
lesson_session.duration = booking.lesson_length
|
|
lesson_session.lesson_type = booking.lesson_type
|
|
lesson_session.lesson_booking = booking
|
|
lesson_session.booked_price = booking.booked_price
|
|
lesson_session.teacher = booking.teacher
|
|
lesson_session.status = booking.status
|
|
lesson_session.slot = booking.default_slot
|
|
lesson_session.save
|
|
|
|
if lesson_session.errors.any?
|
|
puts "Lesson Session errors #{lesson_session.errors.inspect}"
|
|
end
|
|
lesson_session
|
|
end
|
|
|
|
def student
|
|
music_session.creator
|
|
end
|
|
|
|
def self.index(user, params = {})
|
|
limit = params[:per_page]
|
|
limit ||= 100
|
|
limit = limit.to_i
|
|
|
|
query = LessonSession.joins(:music_session).joins(music_session: :creator)
|
|
query = query.includes([:teacher, :music_session])
|
|
query = query.order('music_sessions.scheduled_start DESC')
|
|
|
|
if params[:as_teacher]
|
|
query = query.where('lesson_sessions.teacher_id = ?', user.id)
|
|
else
|
|
query = query.where('music_sessions.user_id = ?', user.id)
|
|
end
|
|
|
|
|
|
|
|
current_page = params[:page].nil? ? 1 : params[:page].to_i
|
|
next_page = current_page + 1
|
|
|
|
# will_paginate gem
|
|
query = query.paginate(:page => current_page, :per_page => limit)
|
|
|
|
if query.length == 0 # no more results
|
|
{ query: query, next_page: nil}
|
|
elsif query.length < limit # no more results
|
|
{ query: query, next_page: nil}
|
|
else
|
|
{ query: query, next_page: next_page }
|
|
end
|
|
end
|
|
|
|
def update_scheduled_start(week_offset)
|
|
music_session.scheduled_start = default_slot.scheduled_time(week_offset)
|
|
music_session.save
|
|
end
|
|
|
|
# teacher accepts the lesson
|
|
def accept(params)
|
|
LessonSession.transaction do
|
|
|
|
message = params[:message]
|
|
slot = params[:slot]
|
|
|
|
self.accepting = true
|
|
slot = LessonBookingSlot.find(slot)
|
|
|
|
lesson_booking.accept(self, slot)
|
|
|
|
self.slot = slot
|
|
|
|
if self.save
|
|
msg = ChatMessage.create(teacher, nil, message, ChatMessage::CHANNEL_LESSON, nil, student, lesson_booking)
|
|
Notification.send_lesson_message('accept', self, true)
|
|
UserMailer.student_lesson_accepted(self, message).deliver
|
|
UserMailer.teacher_lesson_accepted(self, message).deliver
|
|
end
|
|
end
|
|
end
|
|
|
|
def counter(params)
|
|
proposer = params[:proposer]
|
|
slot = params[:slot]
|
|
message = params[:message]
|
|
|
|
self.countering = true
|
|
slot.proposer = proposer
|
|
slot.lesson_session = self
|
|
self.lesson_booking_slots << slot
|
|
self.countered_slot = slot
|
|
self.countered_lesson = self
|
|
if self.save
|
|
if slot.update_all || lesson_booking.is_requested?
|
|
lesson_booking.counter(self, proposer, slot)
|
|
end
|
|
else
|
|
raise ActiveRecord::Rollback
|
|
end
|
|
|
|
msg = ChatMessage.create(slot.proposer, music_session, message, ChatMessage::CHANNEL_LESSON, nil, slot.recipient, lesson_booking)
|
|
Notification.send_lesson_message('counter', self, slot.is_teacher_created?)
|
|
end
|
|
|
|
def home_url
|
|
APP_CONFIG.external_root_url + "/client#/jamclass"
|
|
end
|
|
|
|
def web_url
|
|
APP_CONFIG.external_root_url + "/client#/jamclass/lesson-request/" + id
|
|
end
|
|
end
|
|
end
|