2019-06-06 16:34:53 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Availability stores time slots that are available to reservation for an associated reservable
|
|
|
|
# Eg. a 3D printer will be reservable on thursday from 9 to 11 pm
|
|
|
|
# Availabilities may be subdivided into Slots (of 1h), for some types of reservables (eg. Machine)
|
2020-03-25 10:16:47 +01:00
|
|
|
class Availability < ApplicationRecord
|
2016-09-05 15:15:31 +02:00
|
|
|
# elastic initialisations
|
|
|
|
include Elasticsearch::Model
|
|
|
|
index_name 'fablab'
|
|
|
|
document_type 'availabilities'
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
has_many :machines_availabilities, dependent: :destroy
|
|
|
|
has_many :machines, through: :machines_availabilities
|
|
|
|
accepts_nested_attributes_for :machines, allow_destroy: true
|
|
|
|
|
|
|
|
has_many :trainings_availabilities, dependent: :destroy
|
|
|
|
has_many :trainings, through: :trainings_availabilities
|
|
|
|
|
2017-02-15 13:18:03 +01:00
|
|
|
has_many :spaces_availabilities, dependent: :destroy
|
|
|
|
has_many :spaces, through: :spaces_availabilities
|
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
has_many :slots, dependent: :destroy
|
2022-07-12 17:46:01 +02:00
|
|
|
has_many :slots_reservations, through: :slots
|
2016-03-23 18:39:41 +01:00
|
|
|
has_many :reservations, through: :slots
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
has_one :event, dependent: :destroy
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
has_many :availability_tags, dependent: :destroy
|
|
|
|
has_many :tags, through: :availability_tags
|
|
|
|
accepts_nested_attributes_for :tags, allow_destroy: true
|
|
|
|
|
2020-02-07 17:37:00 +01:00
|
|
|
has_many :plans_availabilities, dependent: :destroy
|
|
|
|
has_many :plans, through: :plans_availabilities
|
|
|
|
accepts_nested_attributes_for :plans, allow_destroy: true
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
scope :machines, -> { where(available_type: 'machines') }
|
2016-06-22 12:54:12 +02:00
|
|
|
scope :trainings, -> { includes(:trainings).where(available_type: 'training') }
|
2017-02-15 13:18:03 +01:00
|
|
|
scope :spaces, -> { includes(:spaces).where(available_type: 'space') }
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2016-04-05 09:57:09 +02:00
|
|
|
validates :start_at, :end_at, presence: true
|
2019-11-13 12:13:22 +01:00
|
|
|
validate :length_must_be_slot_multiple, unless: proc { end_at.blank? or start_at.blank? }
|
2016-03-23 18:39:41 +01:00
|
|
|
validate :should_be_associated
|
|
|
|
|
2016-09-05 15:15:31 +02:00
|
|
|
## elastic callbacks
|
2019-06-13 16:29:12 +02:00
|
|
|
after_save { AvailabilityIndexerWorker.perform_async(:index, id) }
|
|
|
|
after_destroy { AvailabilityIndexerWorker.perform_async(:delete, id) }
|
2016-09-05 15:15:31 +02:00
|
|
|
|
|
|
|
# elastic mapping
|
2018-04-04 14:05:48 +02:00
|
|
|
settings index: { number_of_replicas: 0 } do
|
2016-09-05 15:15:31 +02:00
|
|
|
mappings dynamic: 'true' do
|
|
|
|
indexes 'available_type', analyzer: 'simple'
|
2017-01-03 17:07:23 +01:00
|
|
|
indexes 'subType', index: 'not_analyzed'
|
2016-09-05 15:15:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def safe_destroy
|
2017-02-15 13:18:03 +01:00
|
|
|
case available_type
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'machines'
|
2023-01-24 14:03:01 +01:00
|
|
|
reservations = find_reservations('Machine', machine_ids)
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'training'
|
2023-01-24 14:03:01 +01:00
|
|
|
reservations = find_reservations('Training', training_ids)
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'space'
|
2023-01-24 14:03:01 +01:00
|
|
|
reservations = find_reservations('Space', space_ids)
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'event'
|
2023-01-24 14:03:01 +01:00
|
|
|
reservations = find_reservations('Event', [event&.id])
|
2018-12-03 15:10:04 +01:00
|
|
|
else
|
2022-07-26 17:27:33 +02:00
|
|
|
Rails.logger.warn "[safe_destroy] Availability with unknown type #{available_type}"
|
2018-12-03 15:10:04 +01:00
|
|
|
reservations = []
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2018-12-03 15:10:04 +01:00
|
|
|
if reservations.size.zero?
|
2016-03-23 18:39:41 +01:00
|
|
|
# this update may not call any rails callbacks, that's why we use direct SQL update
|
2023-01-24 14:03:01 +01:00
|
|
|
update_column(:destroying, true) # rubocop:disable Rails/SkipsModelValidations
|
2016-03-23 18:39:41 +01:00
|
|
|
destroy
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
# @param reservable_type [String]
|
|
|
|
# @param reservable_ids [Array<Integer>]
|
|
|
|
def find_reservations(reservable_type, reservable_ids)
|
|
|
|
Reservation.where(reservable_type: reservable_type, reservable_id: reservable_ids)
|
|
|
|
.joins(:slots)
|
|
|
|
.where(slots: { availability_id: id })
|
|
|
|
end
|
|
|
|
|
2017-02-28 16:51:56 +01:00
|
|
|
## compute the total number of places over the whole space availability
|
|
|
|
def available_space_places
|
2018-12-03 15:10:04 +01:00
|
|
|
return unless available_type == 'space'
|
|
|
|
|
2020-06-10 11:52:54 +02:00
|
|
|
duration = slot_duration || Setting.get('slot_duration').to_i
|
2020-04-15 18:08:02 +02:00
|
|
|
((end_at - start_at) / duration.minutes).to_i * nb_total_places
|
2017-02-28 16:51:56 +01:00
|
|
|
end
|
|
|
|
|
2016-07-14 18:36:52 +02:00
|
|
|
def title(filter = {})
|
2017-02-15 13:18:03 +01:00
|
|
|
case available_type
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'machines'
|
2023-01-24 14:03:01 +01:00
|
|
|
return machines.to_ary.delete_if { |m| filter[:machine_ids].exclude?(m.id) }.map(&:name).join(' - ') if filter[:machine_ids]
|
2018-12-03 15:10:04 +01:00
|
|
|
|
|
|
|
machines.map(&:name).join(' - ')
|
|
|
|
when 'event'
|
|
|
|
event.name
|
|
|
|
when 'training'
|
|
|
|
trainings.map(&:name).join(' - ')
|
|
|
|
when 'space'
|
|
|
|
spaces.map(&:name).join(' - ')
|
|
|
|
else
|
2022-07-26 17:27:33 +02:00
|
|
|
Rails.logger.warn "[title] Availability with unknown type #{available_type}"
|
2018-12-03 15:10:04 +01:00
|
|
|
'???'
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-29 10:30:17 +02:00
|
|
|
# check if the reservations are complete?
|
|
|
|
# if a nb_total_places hasn't been defined, then places are unlimited
|
2023-01-18 17:44:58 +01:00
|
|
|
# @return [Boolean]
|
2022-06-29 10:30:17 +02:00
|
|
|
def full?
|
2023-01-02 19:20:02 +01:00
|
|
|
return false if nb_total_places.blank? && available_type != 'machines'
|
2018-12-03 15:10:04 +01:00
|
|
|
|
2022-07-12 17:46:01 +02:00
|
|
|
if available_type == 'event'
|
2018-12-03 15:10:04 +01:00
|
|
|
event.nb_free_places.zero?
|
2022-07-12 17:46:01 +02:00
|
|
|
else
|
|
|
|
slots.map(&:full?).reduce(:&)
|
2016-06-29 17:37:22 +02:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2023-01-18 17:44:58 +01:00
|
|
|
# @return [Array<Integer>] Collection of User's IDs
|
|
|
|
def reserved_users
|
|
|
|
slots.map(&:reserved_users).flatten
|
|
|
|
end
|
|
|
|
|
2023-01-19 14:39:41 +01:00
|
|
|
# @param user [User]
|
|
|
|
# @return [Boolean]
|
|
|
|
def reserved_by?(user_id)
|
|
|
|
reserved_users.include?(user_id)
|
|
|
|
end
|
|
|
|
|
2023-01-18 17:44:58 +01:00
|
|
|
def reserved?
|
|
|
|
slots.map(&:reserved?).reduce(:&)
|
|
|
|
end
|
|
|
|
|
2023-01-02 19:20:02 +01:00
|
|
|
# check availability don't have any reservation
|
|
|
|
def empty?
|
|
|
|
slots.map(&:empty?).reduce(:&)
|
|
|
|
end
|
|
|
|
|
2022-07-20 11:22:00 +02:00
|
|
|
def available_places_per_slot(reservable = nil)
|
2017-02-15 13:18:03 +01:00
|
|
|
case available_type
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'training'
|
2022-07-20 11:22:00 +02:00
|
|
|
nb_total_places || reservable&.nb_total_places || trainings.map(&:nb_total_places).max
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'event'
|
|
|
|
event.nb_total_places
|
|
|
|
when 'space'
|
2022-07-20 11:22:00 +02:00
|
|
|
nb_total_places || reservable&.default_places || spaces.map(&:default_places).max
|
2022-07-12 17:46:01 +02:00
|
|
|
when 'machines'
|
2022-07-20 11:22:00 +02:00
|
|
|
reservable.nil? ? machines.count : 1
|
2018-12-03 15:10:04 +01:00
|
|
|
else
|
2023-01-18 17:44:58 +01:00
|
|
|
raise TypeError, "unknown available type #{available_type} for availability #{id}"
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 16:29:12 +02:00
|
|
|
# the resulting JSON will be indexed in ElasticSearch, as /fablab/availabilities
|
2016-09-06 16:32:41 +02:00
|
|
|
def as_indexed_json
|
|
|
|
json = JSON.parse(to_json)
|
2017-01-03 12:07:16 +01:00
|
|
|
json['hours_duration'] = (end_at - start_at) / (60 * 60)
|
2018-12-03 15:10:04 +01:00
|
|
|
json['subType'] = case available_type
|
|
|
|
when 'machines'
|
2019-06-06 16:34:53 +02:00
|
|
|
machines_availabilities.map { |ma| ma.machine.friendly_id }
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'training'
|
2019-06-06 16:34:53 +02:00
|
|
|
trainings_availabilities.map { |ta| ta.training.friendly_id }
|
2018-12-03 15:10:04 +01:00
|
|
|
when 'event'
|
|
|
|
[event.category.friendly_id]
|
|
|
|
when 'space'
|
2019-06-06 16:34:53 +02:00
|
|
|
spaces_availabilities.map { |sa| sa.space.friendly_id }
|
2018-12-03 15:10:04 +01:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2017-01-03 17:07:23 +01:00
|
|
|
json['bookable_hours'] = json['hours_duration'] * json['subType'].length
|
|
|
|
json['date'] = start_at.to_date
|
2016-09-06 16:32:41 +02:00
|
|
|
json.to_json
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
2018-12-03 15:10:04 +01:00
|
|
|
|
2019-11-13 12:13:22 +01:00
|
|
|
def length_must_be_slot_multiple
|
2020-04-28 09:45:12 +02:00
|
|
|
return unless available_type == 'machines' || available_type == 'space'
|
|
|
|
|
2020-06-10 11:52:54 +02:00
|
|
|
duration = slot_duration || Setting.get('slot_duration').to_i
|
2020-04-15 18:08:02 +02:00
|
|
|
return unless end_at < (start_at + duration.minutes)
|
2020-03-11 11:31:06 +01:00
|
|
|
|
2020-04-15 18:08:02 +02:00
|
|
|
errors.add(:end_at, I18n.t('availabilities.length_must_be_slot_multiple', MIN: duration))
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def should_be_associated
|
2019-06-06 16:34:53 +02:00
|
|
|
return unless available_type == 'machines' && machine_ids.count.zero?
|
|
|
|
|
|
|
|
errors.add(:machine_ids, I18n.t('availabilities.must_be_associated_with_at_least_1_machine'))
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|