2019-01-17 16:26:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides helper methods checking reservation status of any availabilities
|
|
|
|
class Availabilities::StatusService
|
|
|
|
def initialize(current_user_role)
|
|
|
|
@current_user_role = current_user_role
|
2022-07-19 11:32:12 +02:00
|
|
|
@show_name = (%w[admin manager].include?(@current_user_role) || (current_user_role && Setting.get('display_name_enable')))
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
2022-07-11 17:59:56 +02:00
|
|
|
# check that the provided slot is reserved for the given reservable (machine, training or space).
|
|
|
|
# Mark it accordingly for display in the calendar
|
|
|
|
def slot_reserved_status(slot, user, reservables)
|
2022-07-19 11:32:12 +02:00
|
|
|
if reservables.map(&:class).map(&:name).uniq.size > 1
|
2022-07-18 17:17:21 +02:00
|
|
|
raise TypeError('[Availabilities::StatusService#slot_reserved_status] reservables have differents types')
|
|
|
|
end
|
|
|
|
|
2019-06-06 16:34:53 +02:00
|
|
|
statistic_profile_id = user&.statistic_profile&.id
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2022-07-11 17:59:56 +02:00
|
|
|
slots_reservations = slot.slots_reservations
|
|
|
|
.includes(:reservation)
|
|
|
|
.where('reservations.reservable_type': reservables.map(&:class).map(&:name))
|
|
|
|
.where('reservations.reservable_id': reservables.map(&:id))
|
|
|
|
.where('slots_reservations.canceled_at': nil)
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2022-07-11 17:59:56 +02:00
|
|
|
user_slots_reservations = slots_reservations.where('reservations.statistic_profile_id': statistic_profile_id)
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2022-07-19 11:32:12 +02:00
|
|
|
slot.is_reserved = !user_slots_reservations.empty?
|
2022-07-11 17:59:56 +02:00
|
|
|
slot.title = slot_title(slots_reservations, user_slots_reservations, reservables)
|
|
|
|
slot.can_modify = true if %w[admin manager].include?(@current_user_role) || !user_slots_reservations.empty?
|
|
|
|
slot.current_user_slots_reservations_ids = user_slots_reservations.map(&:id)
|
2019-01-17 16:26:03 +01:00
|
|
|
|
|
|
|
slot
|
|
|
|
end
|
|
|
|
|
|
|
|
# check that the provided ability is reserved by the given user
|
2022-07-18 17:17:21 +02:00
|
|
|
def availability_reserved_status(availability, user, reservables)
|
2022-07-19 11:32:12 +02:00
|
|
|
if reservables.map(&:class).map(&:name).uniq.size > 1
|
2022-07-18 17:17:21 +02:00
|
|
|
raise TypeError('[Availabilities::StatusService#availability_reserved_status] reservables have differents types')
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
2022-07-18 17:17:21 +02:00
|
|
|
|
|
|
|
slots_reservations = availability.slots_reservations
|
|
|
|
.includes(:reservation)
|
|
|
|
.where('reservations.reservable_type': reservables.map(&:class).map(&:name))
|
|
|
|
.where('reservations.reservable_id': reservables.map(&:id))
|
|
|
|
.where('slots_reservations.canceled_at': nil)
|
|
|
|
|
|
|
|
user_slots_reservations = slots_reservations.where('reservations.statistic_profile_id': user&.statistic_profile&.id)
|
|
|
|
|
2022-07-19 11:32:12 +02:00
|
|
|
availability.is_reserved = !user_slots_reservations.empty?
|
2022-07-18 17:17:21 +02:00
|
|
|
availability.current_user_slots_reservations_ids = user_slots_reservations.map(&:id)
|
|
|
|
availability
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
2022-07-11 17:59:56 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def slot_title(slots_reservations, user_slots_reservations, reservables)
|
|
|
|
name = reservables.map(&:name).join(', ')
|
|
|
|
if user_slots_reservations.empty? && slots_reservations.empty?
|
|
|
|
name
|
|
|
|
elsif user_slots_reservations.empty? && !slots_reservations.empty?
|
|
|
|
user_names = slots_reservations.map(&:reservation)
|
|
|
|
.map(&:user)
|
|
|
|
.map { |u| u&.profile&.full_name || I18n.t('availabilities.deleted_user') }
|
|
|
|
.join(', ')
|
|
|
|
"#{name} - #{@show_name ? user_names : I18n.t('availabilities.not_available')}"
|
|
|
|
else
|
|
|
|
"#{name} - #{I18n.t('availabilities.i_ve_reserved')}"
|
|
|
|
end
|
|
|
|
end
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|