1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-15 00:24:09 +01:00
fab-manager/app/services/slots/title_service.rb

49 lines
1.6 KiB
Ruby
Raw Normal View History

2023-01-18 17:44:58 +01:00
# frozen_string_literal: true
# Build the title of the provided slot
class Slots::TitleService
def initialize(operator_role, user)
@user = user
@show_name = (%w[admin manager].include?(operator_role) || (operator_role && Setting.get('display_name_enable')))
end
# @param slot [Slot]
# @param reservables [Array<Machine, Space, Training, Event>]
def call(slot, reservables = nil)
reservables = all_reservables(slot) if reservables.nil?
2023-02-24 15:16:57 +01:00
is_reserved = reservables.map { |r| slot.reserved?(r) }.reduce(:|)
is_reserved_by_user = slot.reserved_by?(@user&.id, reservables)
2023-01-18 17:44:58 +01:00
name = reservables.map(&:name).join(', ')
2023-07-28 16:16:44 +02:00
if !slot.is_blocked
if !is_reserved && !is_reserved_by_user
name
elsif is_reserved && !is_reserved_by_user
"#{name} #{@show_name ? "- #{slot_users_names(slot, reservables)}" : ''}"
else
"#{name} - #{I18n.t('availabilities.i_ve_reserved')}"
end
2023-01-18 17:44:58 +01:00
else
2023-07-28 16:16:44 +02:00
"#{name} - #{I18n.t('availabilities.blocked')}"
2023-01-18 17:44:58 +01:00
end
end
private
# @param slot [Slot]
# @param reservables [Array<Machine, Space, Training, Event>]
# @return [String]
def slot_users_names(slot, reservables)
user_ids = slot.reserved_users(reservables)
User.where(id: user_ids).includes(:profile)
.map { |u| u&.profile&.full_name || I18n.t('availabilities.deleted_user') }
.join(', ')
end
# @param slot [Slot]
2023-02-24 15:16:57 +01:00
# @return [Array<Machine, Space, Training, Event>]
def all_reservables(slot)
slot.places.pluck('reservable_type', 'reservable_id').map { |r| r[0].classify.constantize.find(r[1]) }
end
2023-01-18 17:44:58 +01:00
end