2019-01-17 16:26:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides helper methods for public calendar of Availability
|
|
|
|
class Availabilities::PublicAvailabilitiesService
|
|
|
|
def initialize(current_user)
|
|
|
|
@current_user = current_user
|
|
|
|
end
|
|
|
|
|
2022-12-27 10:59:36 +01:00
|
|
|
def public_availabilities(window, ids, events: false)
|
2022-07-18 17:17:21 +02:00
|
|
|
level = in_same_day(window[:start], window[:end]) ? 'slot' : 'availability'
|
|
|
|
service = Availabilities::AvailabilitiesService.new(@current_user, level)
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2023-01-18 17:44:58 +01:00
|
|
|
machines_slots = if Setting.get('machines_module')
|
2023-01-19 11:34:48 +01:00
|
|
|
service.machines(Machine.where(id: ids[:machines]), @current_user, window)
|
2023-01-18 17:44:58 +01:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2023-01-19 11:34:48 +01:00
|
|
|
spaces_slots = Setting.get('spaces_module') ? service.spaces(Space.where(id: ids[:spaces]), @current_user, window) : []
|
2023-01-18 17:44:58 +01:00
|
|
|
trainings_slots = if Setting.get('trainings_module')
|
2023-01-19 11:34:48 +01:00
|
|
|
service.trainings(Training.where(id: ids[:trainings]), @current_user, window)
|
2023-01-18 17:44:58 +01:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2024-04-29 11:14:12 +02:00
|
|
|
events_slots = events ? service.events(Event.where(deleted_at: nil), @current_user, window) : []
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2022-07-18 17:17:21 +02:00
|
|
|
[].concat(trainings_slots).concat(events_slots).concat(machines_slots).concat(spaces_slots)
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def in_same_day(start_date, end_date)
|
|
|
|
(end_date.to_date - start_date.to_date).to_i == 1
|
|
|
|
end
|
|
|
|
end
|