mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-03-21 12:29:03 +01:00
optimise machine/training availabilities query
This commit is contained in:
parent
e82372fb7b
commit
4dff74827b
@ -49,22 +49,22 @@ class API::AvailabilitiesController < API::ApiController
|
||||
else
|
||||
@user = current_user
|
||||
end
|
||||
current_user_role = current_user.is_admin? ? 'admin' : 'user'
|
||||
@current_user_role = current_user.is_admin? ? 'admin' : 'user'
|
||||
@machine = Machine.find(params[:machine_id])
|
||||
@slots = []
|
||||
@reservations = Reservation.where('reservable_type = ? and reservable_id = ?', @machine.class.to_s, @machine.id).includes(:slots, :user).references(:slots, :user).where('slots.start_at > ?', Time.now)
|
||||
@reservations = Reservation.where('reservable_type = ? and reservable_id = ?', @machine.class.to_s, @machine.id).includes(:slots, user: [:profile]).references(:slots, :user).where('slots.start_at > ?', Time.now)
|
||||
if @user.is_admin?
|
||||
@availabilities = @machine.availabilities.where("end_at > ? AND available_type = 'machines'", Time.now)
|
||||
@availabilities = @machine.availabilities.includes(:tags).where("end_at > ? AND available_type = 'machines'", Time.now)
|
||||
else
|
||||
end_at = 1.month.since
|
||||
end_at = 3.months.since if is_subscription_year(@user)
|
||||
@availabilities = @machine.availabilities.includes(:availability_tags).where("end_at > ? AND end_at < ? AND available_type = 'machines'", Time.now, end_at).where('availability_tags.tag_id' => @user.tag_ids.concat([nil]))
|
||||
@availabilities = @machine.availabilities.includes(:tags).where("end_at > ? AND end_at < ? AND available_type = 'machines'", Time.now, end_at).where('availability_tags.tag_id' => @user.tag_ids.concat([nil]))
|
||||
end
|
||||
@availabilities.each do |a|
|
||||
((a.end_at - a.start_at)/SLOT_DURATION.minutes).to_i.times do |i|
|
||||
if (a.start_at + (i * SLOT_DURATION).minutes) > Time.now
|
||||
slot = Slot.new(start_at: a.start_at + (i*SLOT_DURATION).minutes, end_at: a.start_at + (i*SLOT_DURATION).minutes + SLOT_DURATION.minutes, availability_id: a.id, availability: a, machine: @machine, title: '')
|
||||
slot = verify_machine_is_reserved(slot, @reservations, current_user, current_user_role)
|
||||
slot = verify_machine_is_reserved(slot, @reservations, current_user, @current_user_role)
|
||||
@slots << slot
|
||||
end
|
||||
end
|
||||
@ -78,13 +78,13 @@ class API::AvailabilitiesController < API::ApiController
|
||||
@user = current_user
|
||||
end
|
||||
@slots = []
|
||||
@reservations = @user.reservations.where("reservable_type = 'Training'").joins(:slots).where('slots.start_at > ?', Time.now)
|
||||
@reservations = @user.reservations.includes(:slots).references(:slots).where("reservable_type = 'Training' AND slots.start_at > ?", Time.now)
|
||||
if @user.is_admin?
|
||||
@availabilities = Availability.trainings.where('start_at > ?', Time.now)
|
||||
@availabilities = Availability.includes(:tags, :slots, trainings: [:machines]).trainings.where('availabilities.start_at > ?', Time.now)
|
||||
else
|
||||
end_at = 1.month.since
|
||||
end_at = 3.months.since if can_show_slot_plus_three_months(@user)
|
||||
@availabilities = Availability.trainings.includes(:availability_tags).where('start_at > ? AND start_at < ?', Time.now, end_at).where('availability_tags.tag_id' => @user.tag_ids.concat([nil]))
|
||||
@availabilities = Availability.includes(:tags, :slots, trainings: [:machines]).trainings.where('start_at > ? AND start_at < ?', Time.now, end_at).where('availability_tags.tag_id' => @user.tag_ids.concat([nil]))
|
||||
end
|
||||
@availabilities.each do |a|
|
||||
a = verify_training_is_reserved(a, @reservations)
|
||||
|
@ -16,7 +16,7 @@ class Availability < ActiveRecord::Base
|
||||
accepts_nested_attributes_for :tags, allow_destroy: true
|
||||
|
||||
scope :machines, -> { where(available_type: 'machines') }
|
||||
scope :trainings, -> { where(available_type: 'training') }
|
||||
scope :trainings, -> { includes(:trainings).where(available_type: 'training') }
|
||||
|
||||
attr_accessor :is_reserved, :slot_id, :can_modify
|
||||
|
||||
@ -51,9 +51,10 @@ class Availability < ActiveRecord::Base
|
||||
# if haven't defined a nb_total_places, places are unlimited
|
||||
def is_completed
|
||||
return false if nb_total_places.blank?
|
||||
nb_total_places <= slots.where(canceled_at: nil).size
|
||||
nb_total_places <= slots.to_a.select {|s| s.canceled_at == nil }.size
|
||||
end
|
||||
|
||||
# TODO: refactoring this function for avoid N+1 query
|
||||
def nb_total_places
|
||||
if read_attribute(:nb_total_places).present?
|
||||
read_attribute(:nb_total_places)
|
||||
|
@ -17,7 +17,7 @@ json.array!(@slots) do |slot|
|
||||
json.user do
|
||||
json.id slot.reservation.user.id
|
||||
json.name slot.reservation.user.profile.full_name
|
||||
end if slot.reservation # ... if the slot was reserved
|
||||
end if @current_user_role == 'admin' and slot.reservation # ... if the slot was reserved
|
||||
json.tag_ids slot.availability.tag_ids
|
||||
json.tags slot.availability.tags do |t|
|
||||
json.id t.id
|
||||
|
@ -2,26 +2,21 @@ json.array!(@availabilities) do |a|
|
||||
json.id a.id
|
||||
json.slot_id a.slot_id if a.slot_id
|
||||
if a.is_reserved
|
||||
json.is_reserved true
|
||||
json.title "#{a.trainings[0].name}' - #{t('trainings.i_ve_reserved')}"
|
||||
json.borderColor '#b2e774'
|
||||
elsif a.is_completed
|
||||
json.is_completed true
|
||||
json.title "#{a.trainings[0].name} - #{t('trainings.completed')}"
|
||||
json.borderColor '#eeeeee'
|
||||
else
|
||||
json.title a.trainings[0].name
|
||||
json.borderColor '#bd7ae9'
|
||||
end
|
||||
json.start a.start_at.iso8601
|
||||
json.end a.end_at.iso8601
|
||||
json.is_reserved a.is_reserved
|
||||
json.backgroundColor 'white'
|
||||
json.borderColor a.is_reserved ? '#b2e774' : '#bd7ae9'
|
||||
if a.is_reserved
|
||||
json.borderColor '#b2e774'
|
||||
elsif a.is_completed
|
||||
json.borderColor '#eeeeee'
|
||||
else
|
||||
json.borderColor '#bd7ae9'
|
||||
end
|
||||
json.can_modify a.can_modify
|
||||
json.is_completed a.is_completed
|
||||
json.nb_total_places a.nb_total_places
|
||||
|
||||
json.training do
|
||||
@ -32,7 +27,6 @@ json.array!(@availabilities) do |a|
|
||||
json.id m.id
|
||||
json.name m.name
|
||||
end
|
||||
json.amount a.trainings.first.amount_by_group(@user.group_id).amount_by_plan(nil)/100.0 if @user
|
||||
end
|
||||
json.tag_ids a.tag_ids
|
||||
json.tags a.tags do |t|
|
||||
|
Loading…
x
Reference in New Issue
Block a user