mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-26 20:54:21 +01:00
(quality) improved calendar loading time
This commit is contained in:
parent
a77bf46bcd
commit
e5b199f8ed
@ -22,9 +22,11 @@ module AvailabilityHelper
|
||||
end
|
||||
end
|
||||
|
||||
# @param slot [Slot]
|
||||
# @param customer [User]
|
||||
def machines_slot_border_color(slot, customer = nil)
|
||||
if slot.reserved?
|
||||
slot.reserved_users.include?(customer&.id) ? IS_RESERVED_BY_CURRENT_USER : IS_FULL
|
||||
slot.reserved_by?(customer&.id) ? IS_RESERVED_BY_CURRENT_USER : IS_FULL
|
||||
else
|
||||
MACHINE_COLOR
|
||||
end
|
||||
|
@ -128,6 +128,12 @@ class Availability < ApplicationRecord
|
||||
slots.map(&:reserved_users).flatten
|
||||
end
|
||||
|
||||
# @param user [User]
|
||||
# @return [Boolean]
|
||||
def reserved_by?(user_id)
|
||||
reserved_users.include?(user_id)
|
||||
end
|
||||
|
||||
def reserved?
|
||||
slots.map(&:reserved?).reduce(:&)
|
||||
end
|
||||
|
@ -37,6 +37,13 @@ class Slot < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
# @param user_id [Integer,NilClass]
|
||||
# @param reservables [Array<Machine,Space,Training,Event>,NilClass]
|
||||
# @return [Boolean]
|
||||
def reserved_by?(user_id, reservables = nil)
|
||||
reserved_users(reservables).include?(user_id)
|
||||
end
|
||||
|
||||
# @param reservable [Machine, Space, Training, Event, NilClass]
|
||||
# @return [Boolean] enough reservation to fill the whole slot?
|
||||
def full?(reservable = nil)
|
||||
@ -63,7 +70,7 @@ class Slot < ApplicationRecord
|
||||
# @param reservable [Machine,Space,Training,Event,NilClass]
|
||||
# @return [Boolean] the reservation on this slot can be modified?
|
||||
def modifiable?(operator_role, user_id, reservable = nil)
|
||||
%w[admin manager].include?(operator_role) || reserved_users([reservable]).include?(user_id)
|
||||
%w[admin manager].include?(operator_role) || reserved_by?(user_id, [reservable])
|
||||
end
|
||||
|
||||
def duration
|
||||
|
@ -74,11 +74,13 @@ class Slots::ReservationsService
|
||||
|
||||
# @param slot [Slot]
|
||||
# @param user [User]
|
||||
# @param reservable_type [String] 'Machine' | 'Space' | 'Training' | 'Event'
|
||||
# @param reservable_id [Integer]
|
||||
# @return [Hash{Symbol=>ActiveRecord::Relation<SlotsReservation>,ActiveRecord::Relation<CartItem::ReservationSlot>}]
|
||||
def user_reservations(slot, user, reservable_type, reservable_id)
|
||||
return { reservations: [], pending: [] } if user.nil?
|
||||
# @param reservable [Machine,Space,Training,Event']
|
||||
# @return [Hash{Symbol=>ActiveRecord::Relation<SlotsReservation>,ActiveRecord::Relation<CartItem::ReservationSlot>,Array}]
|
||||
def user_reservations(slot, user, reservable)
|
||||
return { reservations: [], pending: [] } if user.nil? || !slot.reserved_by?(user.id, [reservable])
|
||||
|
||||
reservable_type = reservable&.class&.name
|
||||
reservable_id = reservable&.id
|
||||
|
||||
reservations = SlotsReservation.includes(:reservation)
|
||||
.where(slot_id: slot.id, reservations: {
|
||||
|
@ -12,7 +12,7 @@ class Slots::TitleService
|
||||
def call(slot, reservables = nil)
|
||||
reservables = all_reservables(slot) if reservables.nil?
|
||||
is_reserved = slot.reserved?
|
||||
is_reserved_by_user = slot.reserved_users(reservables).include?(@user&.id)
|
||||
is_reserved_by_user = slot.reserved_by?(@user&.id, reservables)
|
||||
|
||||
name = reservables.map(&:name).join(', ')
|
||||
if !is_reserved && !is_reserved_by_user
|
||||
|
@ -10,7 +10,7 @@ json.is_completed slot.full?(reservable)
|
||||
json.backgroundColor 'white'
|
||||
|
||||
json.availability_id slot.availability_id
|
||||
json.slots_reservations_ids Slots::ReservationsService.user_reservations(slot, @user, reservable&.class&.name, reservable&.id)[:reservations]
|
||||
json.slots_reservations_ids Slots::ReservationsService.user_reservations(slot, @user, reservable)[:reservations]
|
||||
|
||||
json.tag_ids slot.availability.tag_ids
|
||||
json.tags slot.availability.tags do |t|
|
||||
|
@ -23,7 +23,7 @@ json.array!(@availabilities) do |availability|
|
||||
json.is_completed availability.full?
|
||||
json.is_reserved availability.reserved?
|
||||
json.borderColor availability_border_color(availability)
|
||||
if availability.reserved? && !@user.nil? && availability.reserved_users.include?(@user.id)
|
||||
if availability.reserved? && !@user.nil? && availability.reserved_by?(@user.id)
|
||||
json.title "#{availability.title}' - #{t('trainings.i_ve_reserved')}"
|
||||
elsif availability.full?
|
||||
json.title "#{availability.title} - #{t('trainings.completed')}"
|
||||
@ -33,7 +33,7 @@ json.array!(@availabilities) do |availability|
|
||||
# slot object ( here => availability = slot ), for daily view
|
||||
elsif availability.instance_of? Slot
|
||||
slot = availability
|
||||
json.title Slots::TitleService.new(@user.role, @user).call(slot)
|
||||
json.title Slots::TitleService.new(@user&.role, @user).call(slot)
|
||||
json.tag_ids slot.availability.tag_ids
|
||||
json.tags slot.availability.tags do |t|
|
||||
json.id t.id
|
||||
|
Loading…
x
Reference in New Issue
Block a user