1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/models/slot.rb

86 lines
2.8 KiB
Ruby
Raw Normal View History

# 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.
2020-03-25 10:16:47 +01:00
class Slot < ApplicationRecord
include NotificationAttachedObject
2016-03-23 18:39:41 +01:00
has_many :slots_reservations, dependent: :destroy
has_many :reservations, through: :slots_reservations
2016-03-23 18:39:41 +01:00
belongs_to :availability
2022-12-28 17:51:27 +01:00
has_many :cart_item_reservation_slots, class_name: 'CartItem::ReservationSlot', dependent: :destroy
2023-01-12 17:48:52 +01:00
after_create_commit :create_places_cache
2023-01-18 17:44:58 +01:00
# @param reservable [Machine,Space,Training,Event,NilClass]
# @return [Integer] the total number of reserved places
def reserved_places(reservable = nil)
if reservable.nil?
places.pluck('reserved_places').reduce(:+)
else
places.detect { |p| p['reservable_type'] == reservable.class.name && p['reservable_id'] == reservable.id }['reserved_places']
end
end
# @param reservables [Array<Machine,Space,Training,Event>,NilClass]
# @return [Array<Integer>] Collection of User's IDs
def reserved_users(reservables = nil)
if reservables.nil?
2023-01-18 17:44:58 +01:00
places.pluck('user_ids').flatten
else
r_places = places.select do |p|
reservables.any? { |r| r.class.name == p['reservable_type'] && r.id == p['reservable_id'] } # rubocop:disable Style/ClassEqualityComparison
end
r_places.pluck('user_ids').flatten
end
end
# @param user_id [Integer,NilClass]
# @param reservables [Array<Machine,Space,Training,Event>,NilClass]
# @return [Boolean]
def reserved_by?(user_id, reservables = nil)
reserved_users(reservables).include?(user_id)
end
2023-01-12 17:48:52 +01:00
# @param reservable [Machine, Space, Training, Event, NilClass]
2023-01-18 17:44:58 +01:00
# @return [Boolean] enough reservation to fill the whole slot?
def full?(reservable = nil)
availability_places = availability.available_places_per_slot(reservable)
return false if availability_places.nil?
2023-01-18 17:44:58 +01:00
reserved_places(reservable) >= availability_places
end
2023-01-12 17:48:52 +01:00
2023-01-18 17:44:58 +01:00
# @param reservable [Machine,Space,Training,Event,NilClass]
# @return [Boolean] any reservation or none?
def reserved?(reservable = nil)
reserved_places(reservable).positive?
end
2023-01-18 17:44:58 +01:00
# @param reservable [Machine,Space,Training,Event,NilClass]
# @return [Boolean] no reservations at all?
def empty?(reservable = nil)
2023-01-18 17:44:58 +01:00
reserved_places(reservable).zero?
end
# @param operator_role [String,NilClass] 'admin' | 'manager' | 'member'
# @param user_id [Integer]
# @param reservable [Machine,Space,Training,Event,NilClass]
# @return [Boolean] the reservation on this slot can be modified?
def modifiable?(operator_role, user_id, reservable = nil)
%w[admin manager].include?(operator_role) || reserved_by?(user_id, [reservable])
end
def duration
(end_at - start_at).seconds
end
2023-01-12 17:48:52 +01:00
private
def create_places_cache
Slots::PlacesCacheService.refresh(self)
end
2016-03-23 18:39:41 +01:00
end