1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/models/slot.rb
2023-02-15 10:29:54 +01:00

54 lines
1.7 KiB
Ruby

# frozen_string_literal: true
# A Time range, slicing an Availability.
# Slots duration are defined globally by Setting.get('slot_duration') but can be
# overridden per availability.
class Slot < ApplicationRecord
include NotifyWith::NotificationAttachedObject
has_many :slots_reservations, dependent: :destroy
has_many :reservations, through: :slots_reservations
belongs_to :availability
has_many :cart_item_reservation_slots, class_name: 'CartItem::ReservationSlot', dependent: :destroy
after_create_commit :create_places_cache
attr_accessor :is_reserved, :machine, :space, :title, :can_modify, :current_user_slots_reservations_ids, :current_user_pending_reservations_ids
# @param reservable [Machine, Space, Training, Event, NilClass]
def full?(reservable = nil)
availability_places = availability.available_places_per_slot(reservable)
return false if availability_places.nil?
reserved_places = if reservable.nil?
places.pluck('reserved_places').reduce(:+)
else
rp = places.detect do |p|
p['reservable_type'] == reservable.class.name && p['reservable_id'] == reservable&.id
end
rp['reserved_places']
end
reserved_places >= availability_places
end
def empty?(reservable = nil)
if reservable.nil?
slots_reservations.where(canceled_at: nil).count.zero?
else
slots_reservations.includes(:reservation).where(canceled_at: nil).where('reservations.reservable': reservable).count.zero?
end
end
def duration
(end_at - start_at).seconds
end
private
def create_places_cache
Slots::PlacesCacheService.refresh(self)
end
end