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

91 lines
3.2 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) || Setting.get('display_name_enable') == 'true')
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
slot.title = "#{slot.machine.name} - #{@show_name ? r.user&.profile&.full_name : I18n.t('availabilities.not_available')}"
slot.can_modify = true if %w[admin manager].include?(@current_user_role)
2019-01-17 16:26:03 +01:00
slot.reservations.push r
next unless r.statistic_profile_id == statistic_profile_id
2019-01-17 16:26:03 +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?
slot.can_modify = true if %w[admin manager].include?(@current_user_role)
2019-01-17 16:26:03 +01:00
slot.reservations.push r
next unless r.statistic_profile_id == statistic_profile_id
2019-01-17 16:26:03 +01:00
slot.id = s.id
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
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
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