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
|
2020-05-25 16:47:06 +02:00
|
|
|
@show_name = (%w[admin manager].include?(@current_user_role) || Setting.get('display_name_enable'))
|
2019-01-17 16:26:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# check that the provided machine slot is reserved or not and modify it accordingly
|
|
|
|
def machine_reserved_status(slot, reservations, user)
|
2019-06-06 16:34:53 +02:00
|
|
|
statistic_profile_id = user&.statistic_profile&.id
|
2019-01-17 16:26:03 +01:00
|
|
|
reservations.each do |r|
|
|
|
|
r.slots.each do |s|
|
|
|
|
next unless slot.machine.id == r.reservable_id
|
|
|
|
|
|
|
|
next unless s.start_at == slot.start_at && s.canceled_at.nil?
|
|
|
|
|
|
|
|
slot.id = s.id
|
|
|
|
slot.is_reserved = true
|
2022-04-05 16:44:55 +02:00
|
|
|
user_name = r.user ? r.user&.profile&.full_name : I18n.t('availabilities.deleted_user');
|
|
|
|
slot.title = "#{slot.machine.name} - #{@show_name ? user_name : I18n.t('availabilities.deleted_user')}"
|
2020-05-25 16:35:21 +02:00
|
|
|
slot.can_modify = true if %w[admin manager].include?(@current_user_role)
|
2019-01-17 16:26:03 +01:00
|
|
|
slot.reservations.push r
|
|
|
|
|
2019-06-06 12:00:21 +02:00
|
|
|
next unless r.statistic_profile_id == statistic_profile_id
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2019-01-29 11:29:14 +01:00
|
|
|
slot.title = "#{slot.machine.name} - #{I18n.t('availabilities.i_ve_reserved')}"
|
2019-01-17 16:26:03 +01:00
|
|
|
slot.can_modify = true
|
|
|
|
slot.is_reserved_by_current_user = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
slot
|
|
|
|
end
|
|
|
|
|
|
|
|
# check that the provided space slot is reserved or not and modify it accordingly
|
|
|
|
def space_reserved_status(slot, reservations, user)
|
2019-06-06 16:34:53 +02:00
|
|
|
statistic_profile_id = user&.statistic_profile&.id
|
2019-01-17 16:26:03 +01:00
|
|
|
reservations.each do |r|
|
|
|
|
r.slots.each do |s|
|
|
|
|
next unless slot.space.id == r.reservable_id
|
|
|
|
|
|
|
|
next unless s.start_at == slot.start_at && s.canceled_at.nil?
|
|
|
|
|
2020-05-25 16:35:21 +02:00
|
|
|
slot.can_modify = true if %w[admin manager].include?(@current_user_role)
|
2019-01-17 16:26:03 +01:00
|
|
|
slot.reservations.push r
|
|
|
|
|
2019-06-06 12:00:21 +02:00
|
|
|
next unless r.statistic_profile_id == statistic_profile_id
|
2019-01-17 16:26:03 +01:00
|
|
|
|
|
|
|
slot.id = s.id
|
2019-01-29 11:29:14 +01:00
|
|
|
slot.title = I18n.t('availabilities.i_ve_reserved')
|
2019-01-17 16:26:03 +01:00
|
|
|
slot.can_modify = true
|
|
|
|
slot.is_reserved = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
slot
|
|
|
|
end
|
|
|
|
|
|
|
|
# check that the provided availability (training or event) is reserved or not and modify it accordingly
|
|
|
|
def training_event_reserved_status(availability, reservations, user)
|
2019-06-06 16:34:53 +02:00
|
|
|
statistic_profile_id = user&.statistic_profile&.id
|
2019-01-17 16:26:03 +01:00
|
|
|
reservations.each do |r|
|
|
|
|
r.slots.each do |s|
|
|
|
|
next unless (
|
|
|
|
(availability.available_type == 'training' && availability.trainings.first.id == r.reservable_id) ||
|
|
|
|
(availability.available_type == 'event' && availability.event.id == r.reservable_id)
|
|
|
|
) && s.start_at == availability.start_at && s.canceled_at.nil?
|
|
|
|
|
|
|
|
availability.slot_id = s.id
|
2019-06-06 12:00:21 +02:00
|
|
|
if r.statistic_profile_id == statistic_profile_id
|
2019-01-17 16:26:03 +01:00
|
|
|
availability.is_reserved = true
|
|
|
|
availability.can_modify = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
availability
|
|
|
|
end
|
|
|
|
|
|
|
|
# check that the provided ability is reserved by the given user
|
|
|
|
def reserved_availability?(availability, user)
|
|
|
|
if user
|
|
|
|
reserved_slots = []
|
|
|
|
availability.slots.each do |s|
|
|
|
|
reserved_slots << s if s.canceled_at.nil?
|
|
|
|
end
|
2019-10-14 10:38:13 +02:00
|
|
|
reserved_slots.map(&:reservations).flatten.map(&:statistic_profile_id).include? user.statistic_profile&.id
|
2019-01-17 16:26:03 +01:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|