2019-05-07 12:24:51 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-04 16:55:02 +02:00
|
|
|
# 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
|
2016-03-23 18:39:41 +01:00
|
|
|
include NotifyWith::NotificationAttachedObject
|
|
|
|
|
2017-02-28 13:23:31 +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)
|
2023-01-19 11:34:48 +01:00
|
|
|
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
|
|
|
|
|
2023-01-19 14:39:41 +01:00
|
|
|
# @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?
|
2022-07-20 11:22:00 +02:00
|
|
|
def full?(reservable = nil)
|
|
|
|
availability_places = availability.available_places_per_slot(reservable)
|
2022-07-13 16:28:43 +02:00
|
|
|
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?
|
2022-07-13 16:28:43 +02:00
|
|
|
end
|
|
|
|
|
2023-01-18 17:44:58 +01:00
|
|
|
# @param reservable [Machine,Space,Training,Event,NilClass]
|
|
|
|
# @return [Boolean] no reservations at all?
|
2023-01-02 19:20:02 +01:00
|
|
|
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)
|
2023-01-19 14:39:41 +01:00
|
|
|
%w[admin manager].include?(operator_role) || reserved_by?(user_id, [reservable])
|
2023-01-02 19:20:02 +01:00
|
|
|
end
|
|
|
|
|
2022-07-13 16:28:43 +02:00
|
|
|
def duration
|
|
|
|
(end_at - start_at).seconds
|
2017-02-27 16:15:27 +01:00
|
|
|
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
|