1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/services/availabilities/status_service.rb

71 lines
3.3 KiB
Ruby
Raw Normal View History

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
@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
# 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)
if reservables.map(&:class).map(&:name).uniq.size > 1
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
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
user_slots_reservations = slots_reservations.where('reservations.statistic_profile_id': statistic_profile_id)
2019-01-17 16:26:03 +01:00
slot.is_reserved = !slots_reservations.empty?
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
def availability_reserved_status(availability, user, reservables)
if reservables.map(&:class).map(&:name).uniq.size > 1
raise TypeError('[Availabilities::StatusService#availability_reserved_status] reservables have differents types')
2019-01-17 16:26:03 +01:00
end
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)
availability.is_reserved = !slots_reservations.empty?
availability.current_user_slots_reservations_ids = user_slots_reservations.map(&:id)
availability
2019-01-17 16:26:03 +01:00
end
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}" : ''}"
else
"#{name} - #{I18n.t('availabilities.i_ve_reserved')}"
end
end
2019-01-17 16:26:03 +01:00
end