2019-01-17 16:26:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides helper methods for Availability resources and properties
|
|
|
|
class Availabilities::AvailabilitiesService
|
|
|
|
|
|
|
|
def initialize(current_user, maximum_visibility = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@maximum_visibility = maximum_visibility
|
|
|
|
@service = Availabilities::StatusService.new(current_user.admin? ? 'admin' : 'user')
|
|
|
|
end
|
|
|
|
|
|
|
|
# list all slots for the given machine, with reservations info, relatives to the given user
|
|
|
|
def machines(machine_id, user)
|
|
|
|
machine = Machine.friendly.find(machine_id)
|
2020-01-16 14:50:38 +01:00
|
|
|
reservations = reservations(machine, user)
|
2019-01-17 16:26:03 +01:00
|
|
|
availabilities = availabilities(machine, 'machines', user)
|
|
|
|
|
|
|
|
slots = []
|
|
|
|
availabilities.each do |a|
|
2020-04-15 18:08:02 +02:00
|
|
|
slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION
|
|
|
|
((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i|
|
|
|
|
next unless (a.start_at + (i * slot_duration).minutes) > DateTime.current || user.admin?
|
2019-01-17 16:26:03 +01:00
|
|
|
|
|
|
|
slot = Slot.new(
|
2020-04-15 18:08:02 +02:00
|
|
|
start_at: a.start_at + (i * slot_duration).minutes,
|
|
|
|
end_at: a.start_at + (i * slot_duration).minutes + slot_duration.minutes,
|
2019-01-17 16:26:03 +01:00
|
|
|
availability_id: a.id,
|
|
|
|
availability: a,
|
|
|
|
machine: machine,
|
|
|
|
title: ''
|
|
|
|
)
|
|
|
|
slot = @service.machine_reserved_status(slot, reservations, @current_user)
|
|
|
|
slots << slot
|
|
|
|
end
|
|
|
|
end
|
|
|
|
slots
|
|
|
|
end
|
|
|
|
|
|
|
|
# list all slots for the given space, with reservations info, relatives to the given user
|
|
|
|
def spaces(space_id, user)
|
|
|
|
space = Space.friendly.find(space_id)
|
2020-01-16 14:50:38 +01:00
|
|
|
reservations = reservations(space, user)
|
2019-01-17 16:55:25 +01:00
|
|
|
availabilities = availabilities(space, 'space', user)
|
2019-01-17 16:26:03 +01:00
|
|
|
|
|
|
|
slots = []
|
|
|
|
availabilities.each do |a|
|
2020-04-15 18:08:02 +02:00
|
|
|
slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION
|
|
|
|
((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i|
|
|
|
|
next unless (a.start_at + (i * slot_duration).minutes) > DateTime.current || user.admin?
|
2019-01-17 16:26:03 +01:00
|
|
|
|
|
|
|
slot = Slot.new(
|
2020-04-15 18:08:02 +02:00
|
|
|
start_at: a.start_at + (i * slot_duration).minutes,
|
|
|
|
end_at: a.start_at + (i * slot_duration).minutes + slot_duration.minutes,
|
2019-01-17 16:26:03 +01:00
|
|
|
availability_id: a.id,
|
|
|
|
availability: a,
|
|
|
|
space: space,
|
|
|
|
title: ''
|
|
|
|
)
|
|
|
|
slot = @service.space_reserved_status(slot, reservations, user)
|
|
|
|
slots << slot
|
|
|
|
end
|
|
|
|
end
|
|
|
|
slots.each do |s|
|
2019-05-07 12:24:51 +02:00
|
|
|
s.title = I18n.t('availabilities.not_available') if s.complete? && !s.is_reserved
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
slots
|
|
|
|
end
|
|
|
|
|
|
|
|
# list all slots for the given training, with reservations info, relatives to the given user
|
|
|
|
def trainings(training_id, user)
|
|
|
|
# first, we get the already-made reservations
|
|
|
|
reservations = user.reservations.where("reservable_type = 'Training'")
|
|
|
|
reservations = reservations.where('reservable_id = :id', id: training_id.to_i) if training_id.is_number?
|
2020-01-20 16:15:28 +01:00
|
|
|
reservations = reservations.joins(:slots).where('slots.start_at > ?', @current_user.admin? ? 1.month.ago : DateTime.current)
|
2019-01-17 16:26:03 +01:00
|
|
|
|
|
|
|
# visible availabilities depends on multiple parameters
|
2019-01-17 16:55:25 +01:00
|
|
|
availabilities = training_availabilities(training_id, user)
|
2019-01-17 16:26:03 +01:00
|
|
|
|
|
|
|
# finally, we merge the availabilities with the reservations
|
|
|
|
availabilities.each do |a|
|
2019-01-17 16:55:25 +01:00
|
|
|
a = @service.training_event_reserved_status(a, reservations, user)
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def subscription_year?(user)
|
2019-12-02 11:57:25 +01:00
|
|
|
user.subscription && user.subscription.plan.interval == 'year' && user.subscription.expired_at >= DateTime.current
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# member must have validated at least 1 training and must have a valid yearly subscription.
|
|
|
|
def show_extended_slots?(user)
|
|
|
|
user.trainings.size.positive? && subscription_year?(user)
|
|
|
|
end
|
|
|
|
|
2020-01-16 14:50:38 +01:00
|
|
|
def reservations(reservable, user)
|
2019-01-17 16:26:03 +01:00
|
|
|
Reservation.where('reservable_type = ? and reservable_id = ?', reservable.class.name, reservable.id)
|
2019-06-04 16:50:23 +02:00
|
|
|
.includes(:slots, statistic_profile: [user: [:profile]])
|
2019-01-17 16:26:03 +01:00
|
|
|
.references(:slots, :user)
|
2020-01-16 14:50:38 +01:00
|
|
|
.where('slots.start_at > ?', user.admin? ? 1.month.ago : DateTime.current)
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def availabilities(reservable, type, user)
|
|
|
|
if user.admin?
|
|
|
|
reservable.availabilities
|
|
|
|
.includes(:tags)
|
2020-01-16 14:50:38 +01:00
|
|
|
.where('end_at > ? AND available_type = ?', 1.month.ago, type)
|
2019-01-17 16:26:03 +01:00
|
|
|
.where(lock: false)
|
|
|
|
else
|
|
|
|
end_at = @maximum_visibility[:other]
|
|
|
|
end_at = @maximum_visibility[:year] if subscription_year?(user)
|
|
|
|
reservable.availabilities
|
|
|
|
.includes(:tags)
|
2019-12-02 11:57:25 +01:00
|
|
|
.where('end_at > ? AND end_at < ? AND available_type = ?', DateTime.current, end_at, type)
|
2019-01-17 16:26:03 +01:00
|
|
|
.where('availability_tags.tag_id' => user.tag_ids.concat([nil]))
|
|
|
|
.where(lock: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def training_availabilities(training_id, user)
|
|
|
|
availabilities = if training_id.is_number? || (training_id.length.positive? && training_id != 'all')
|
2019-01-17 16:55:25 +01:00
|
|
|
Training.friendly.find(training_id).availabilities
|
2019-01-17 16:26:03 +01:00
|
|
|
else
|
|
|
|
Availability.trainings
|
|
|
|
end
|
|
|
|
|
|
|
|
# who made the request?
|
2020-01-21 11:16:49 +01:00
|
|
|
# 1) an admin (he can see all availabilities of 1 month ago and future)
|
2019-01-17 16:26:03 +01:00
|
|
|
if @current_user.admin?
|
|
|
|
availabilities.includes(:tags, :slots, trainings: [:machines])
|
2020-01-20 16:15:28 +01:00
|
|
|
.where('availabilities.start_at > ?', 1.month.ago)
|
2019-01-17 16:26:03 +01:00
|
|
|
.where(lock: false)
|
|
|
|
# 2) an user (he cannot see availabilities further than 1 (or 3) months)
|
|
|
|
else
|
|
|
|
end_at = @maximum_visibility[:other]
|
|
|
|
end_at = @maximum_visibility[:year] if show_extended_slots?(user)
|
|
|
|
availabilities.includes(:tags, :slots, :availability_tags, trainings: [:machines])
|
2019-12-02 11:57:25 +01:00
|
|
|
.where('availabilities.start_at > ? AND availabilities.start_at < ?', DateTime.current, end_at)
|
2019-01-17 16:26:03 +01:00
|
|
|
.where('availability_tags.tag_id' => user.tag_ids.concat([nil]))
|
|
|
|
.where(lock: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|