2019-01-17 16:26:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-18 17:17:21 +02:00
|
|
|
# List all Availability's slots for the given resources
|
2019-01-17 16:26:03 +01:00
|
|
|
class Availabilities::AvailabilitiesService
|
|
|
|
|
2022-07-18 17:17:21 +02:00
|
|
|
def initialize(current_user, level = 'slot')
|
2019-01-17 16:26:03 +01:00
|
|
|
@current_user = current_user
|
2022-07-11 17:59:56 +02:00
|
|
|
@maximum_visibility = {
|
|
|
|
year: Setting.get('visibility_yearly').to_i.months.since,
|
|
|
|
other: Setting.get('visibility_others').to_i.months.since
|
|
|
|
}
|
|
|
|
@service = Availabilities::StatusService.new(current_user&.role)
|
2022-07-18 17:17:21 +02:00
|
|
|
@level = level
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
2022-07-11 17:59:56 +02:00
|
|
|
# list all slots for the given machine, with visibility relative to the given user
|
|
|
|
def machines(machine, user, window)
|
|
|
|
availabilities = availabilities(machine.availabilities, 'machines', user, window[:start], window[:end])
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2022-07-18 17:17:21 +02:00
|
|
|
if @level == 'slot'
|
|
|
|
availabilities.map(&:slots).flatten.map { |s| @service.slot_reserved_status(s, user, [machine]) }
|
|
|
|
else
|
|
|
|
availabilities.map { |a| @service.availability_reserved_status(a, user, [machine]) }
|
|
|
|
end
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
2022-07-11 17:59:56 +02:00
|
|
|
# list all slots for the given space, with visibility relative to the given user
|
|
|
|
def spaces(space, user, window)
|
|
|
|
availabilities = availabilities(space.availabilities, 'space', user, window[:start], window[:end])
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2022-07-18 17:17:21 +02:00
|
|
|
if @level == 'slot'
|
|
|
|
availabilities.map(&:slots).flatten.map { |s| @service.slot_reserved_status(s, user, [space]) }
|
|
|
|
else
|
|
|
|
availabilities.map { |a| @service.availability_reserved_status(a, user, [space]) }
|
|
|
|
end
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
2022-07-12 17:46:01 +02:00
|
|
|
# list all slots for the given training(s), with visibility relative to the given user
|
2022-07-11 17:59:56 +02:00
|
|
|
def trainings(trainings, user, window)
|
|
|
|
tr_availabilities = Availability.includes('trainings_availabilities')
|
|
|
|
.where('trainings_availabilities.training_id': trainings.map(&:id))
|
|
|
|
availabilities = availabilities(tr_availabilities, 'training', user, window[:start], window[:end])
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2022-07-18 17:17:21 +02:00
|
|
|
if @level == 'slot'
|
|
|
|
availabilities.map(&:slots).flatten.map { |s| @service.slot_reserved_status(s, user, s.availability.trainings) }
|
|
|
|
else
|
|
|
|
availabilities.map { |a| @service.availability_reserved_status(a, user, a.trainings) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# list all slots for the given event(s), with visibility relative to the given user
|
|
|
|
def events(events, user, window)
|
|
|
|
ev_availabilities = Availability.includes('event').where('events.id': events.map(&:id))
|
|
|
|
availabilities = availabilities(ev_availabilities, 'event', user, window[:start], window[:end])
|
|
|
|
|
|
|
|
if @level == 'slot'
|
|
|
|
availabilities.map(&:slots).flatten.map { |s| @service.slot_reserved_status(s, user, [s.availability.event]) }
|
|
|
|
else
|
|
|
|
availabilities.map { |a| @service.availability_reserved_status(a, user, [a.event]) }
|
|
|
|
end
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def subscription_year?(user)
|
2022-07-11 17:59:56 +02:00
|
|
|
user&.subscription && user.subscription.plan.interval == 'year' && user.subscription.expired_at >= DateTime.current
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
2022-07-11 17:59:56 +02:00
|
|
|
# members must have validated at least 1 training and must have a valid yearly subscription to view
|
|
|
|
# the trainings further in the futur. This is used to prevent users with a rolling subscription to take
|
|
|
|
# their first training in a very long delay.
|
|
|
|
def show_more_trainings?(user)
|
2022-07-18 17:17:21 +02:00
|
|
|
user&.trainings&.size&.positive? && subscription_year?(user)
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
2022-07-11 17:59:56 +02:00
|
|
|
def availabilities(availabilities, type, user, range_start, range_end)
|
2019-01-17 16:26:03 +01:00
|
|
|
# who made the request?
|
2022-07-11 17:59:56 +02:00
|
|
|
# 1) an admin (he can see all availabilities from 1 month ago to anytime in the future)
|
|
|
|
if @current_user&.admin? || @current_user&.manager?
|
|
|
|
window_start = [range_start, 1.month.ago].max
|
2022-07-18 17:17:21 +02:00
|
|
|
availabilities.includes(:tags, :plans, :slots)
|
|
|
|
.joins(:slots)
|
|
|
|
.where('availabilities.start_at <= ? AND availabilities.end_at >= ? AND available_type = ?', range_end, window_start, type)
|
|
|
|
.where('slots.start_at > ? AND slots.end_at < ?', window_start, range_end)
|
2019-01-17 16:26:03 +01:00
|
|
|
.where(lock: false)
|
2022-07-11 17:59:56 +02:00
|
|
|
# 2) an user (he cannot see past availabilities neither those further than 1 (or 3) months in the future)
|
2019-01-17 16:26:03 +01:00
|
|
|
else
|
|
|
|
end_at = @maximum_visibility[:other]
|
2022-07-11 17:59:56 +02:00
|
|
|
end_at = @maximum_visibility[:year] if subscription_year?(user) && type != 'training'
|
|
|
|
end_at = @maximum_visibility[:year] if show_more_trainings?(user) && type == 'training'
|
|
|
|
window_end = [end_at, range_end].min
|
|
|
|
window_start = [range_start, DateTime.current].max
|
2022-07-18 17:17:21 +02:00
|
|
|
availabilities.includes(:tags, :plans, :slots)
|
|
|
|
.joins(:slots)
|
|
|
|
.where('availabilities.start_at <= ? AND availabilities.end_at >= ? AND available_type = ?', window_end, window_start, type)
|
|
|
|
.where('slots.start_at > ? AND slots.end_at < ?', window_start, window_end)
|
|
|
|
.where('availability_tags.tag_id' => user&.tag_ids&.concat([nil]))
|
2019-01-17 16:26:03 +01:00
|
|
|
.where(lock: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|