mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
show availability with reservation state
This commit is contained in:
parent
a67320121c
commit
49ee5011c1
@ -1,5 +1,5 @@
|
||||
class API::AvailabilitiesController < API::ApiController
|
||||
before_action :authenticate_user!
|
||||
before_action :authenticate_user!, except: [:public]
|
||||
before_action :set_availability, only: [:show, :update, :destroy, :reservations]
|
||||
respond_to :json
|
||||
|
||||
@ -17,6 +17,7 @@ class API::AvailabilitiesController < API::ApiController
|
||||
def public
|
||||
start_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:start])
|
||||
end_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:end]).end_of_day
|
||||
@reservations = Reservation.includes(:slots, user: [:profile]).references(:slots, :user).where('slots.start_at >= ? AND slots.end_at <= ?', start_date, end_date)
|
||||
if in_same_day(start_date, end_date)
|
||||
@training_and_event_availabilities = Availability.includes(:tags, :trainings).where.not(available_type: 'machines')
|
||||
.where('start_at >= ? AND end_at <= ?', start_date, end_date)
|
||||
@ -27,14 +28,19 @@ class API::AvailabilitiesController < API::ApiController
|
||||
a.machines.each do |machine|
|
||||
((a.end_at - a.start_at)/SLOT_DURATION.minutes).to_i.times do |i|
|
||||
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: machine.name)
|
||||
slot = verify_machine_is_reserved(slot, @reservations, current_user, '')
|
||||
@machine_slots << slot
|
||||
end
|
||||
end
|
||||
end
|
||||
@availabilities = [].concat(@training_and_event_availabilities).concat(@machine_slots)
|
||||
else
|
||||
|
||||
@availabilities = Availability.includes(:tags, :machines, :trainings, :event)
|
||||
.where('start_at >= ? AND end_at <= ?', start_date, end_date)
|
||||
@availabilities.each do |a|
|
||||
a = verify_training_event_is_reserved(a, @reservations)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -133,7 +139,7 @@ class API::AvailabilitiesController < API::ApiController
|
||||
|
||||
# finally, we merge the availabilities with the reservations
|
||||
@availabilities.each do |a|
|
||||
a = verify_training_is_reserved(a, @reservations)
|
||||
a = verify_training_event_is_reserved(a, @reservations)
|
||||
end
|
||||
end
|
||||
|
||||
@ -165,31 +171,35 @@ class API::AvailabilitiesController < API::ApiController
|
||||
def verify_machine_is_reserved(slot, reservations, user, user_role)
|
||||
reservations.each do |r|
|
||||
r.slots.each do |s|
|
||||
if s.start_at == slot.start_at and s.canceled_at == nil
|
||||
slot.id = s.id
|
||||
slot.is_reserved = true
|
||||
slot.title = t('availabilities.not_available')
|
||||
slot.can_modify = true if user_role === 'admin'
|
||||
slot.reservation = r
|
||||
end
|
||||
if s.start_at == slot.start_at and r.user == user and s.canceled_at == nil
|
||||
slot.title = t('availabilities.i_ve_reserved')
|
||||
slot.can_modify = true
|
||||
slot.is_reserved_by_current_user = true
|
||||
if slot.machine.id == r.reservable_id
|
||||
if s.start_at == slot.start_at and s.canceled_at == nil
|
||||
slot.id = s.id
|
||||
slot.is_reserved = true
|
||||
slot.title = "#{slot.machine.name} - #{t('availabilities.not_available')}"
|
||||
slot.can_modify = true if user_role === 'admin'
|
||||
slot.reservation = r
|
||||
end
|
||||
if s.start_at == slot.start_at and r.user == user and s.canceled_at == nil
|
||||
slot.title = "#{slot.machine.name} - #{t('availabilities.i_ve_reserved')}"
|
||||
slot.can_modify = true
|
||||
slot.is_reserved_by_current_user = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
slot
|
||||
end
|
||||
|
||||
def verify_training_is_reserved(availability, reservations)
|
||||
def verify_training_event_is_reserved(availability, reservations)
|
||||
user = current_user
|
||||
reservations.each do |r|
|
||||
r.slots.each do |s|
|
||||
if s.start_at == availability.start_at and s.canceled_at == nil and availability.trainings.first.id == r.reservable_id
|
||||
if ((availability.available_type == 'training' and availability.trainings.first.id == r.reservable_id) or (availability.available_type == 'event' and availability.event.id == r.reservable_id)) and s.start_at == availability.start_at and s.canceled_at == nil
|
||||
availability.slot_id = s.id
|
||||
availability.is_reserved = true
|
||||
availability.can_modify = true if r.user == user
|
||||
if r.user == user
|
||||
availability.is_reserved = true
|
||||
availability.can_modify = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -4,7 +4,7 @@ module AvailabilityHelper
|
||||
EVENT_COLOR = '#dd7e6b'
|
||||
IS_RESERVED_BY_CURRENT_USER = '#b2e774'
|
||||
MACHINE_IS_RESERVED_BY_USER = '#1d98ec'
|
||||
TRAINING_IS_COMPLETED = '#eeeeee'
|
||||
IS_COMPLETED = '#eeeeee'
|
||||
|
||||
def availability_border_color(availability)
|
||||
if availability.available_type == 'machines'
|
||||
@ -17,16 +17,20 @@ module AvailabilityHelper
|
||||
end
|
||||
|
||||
def machines_slot_border_color(slot)
|
||||
slot.is_reserved ? (slot.is_reserved_by_current_user ? IS_RESERVED_BY_CURRENT_USER : MACHINE_IS_RESERVED_BY_USER) : MACHINE_COLOR
|
||||
slot.is_reserved ? (slot.is_reserved_by_current_user ? IS_RESERVED_BY_CURRENT_USER : IS_COMPLETED) : MACHINE_COLOR
|
||||
end
|
||||
|
||||
def trainings_border_color(availability)
|
||||
def trainings_events_border_color(availability)
|
||||
if availability.is_reserved
|
||||
IS_RESERVED_BY_CURRENT_USER
|
||||
elsif availability.is_completed
|
||||
TRAINING_IS_COMPLETED
|
||||
IS_COMPLETED
|
||||
else
|
||||
TRAINING_COLOR
|
||||
if availability.available_type == 'training'
|
||||
TRAINING_COLOR
|
||||
else
|
||||
EVENT_COLOR
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -53,15 +53,23 @@ 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.to_a.select {|s| s.canceled_at == nil }.size
|
||||
if available_type == 'training'
|
||||
nb_total_places <= slots.to_a.select {|s| s.canceled_at == nil }.size
|
||||
elsif available_type == 'event'
|
||||
event.nb_free_places == 0
|
||||
end
|
||||
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)
|
||||
else
|
||||
trainings.first.nb_total_places unless trainings.empty?
|
||||
if available_type == 'training'
|
||||
if read_attribute(:nb_total_places).present?
|
||||
read_attribute(:nb_total_places)
|
||||
else
|
||||
trainings.first.nb_total_places unless trainings.empty?
|
||||
end
|
||||
elsif available_type == 'event'
|
||||
event.nb_total_places
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -13,12 +13,25 @@ json.array!(@availabilities) do |availability|
|
||||
json.training_id availability.trainings.first.id
|
||||
end
|
||||
json.available_type availability.available_type
|
||||
json.borderColor availability_border_color(availability)
|
||||
json.tag_ids availability.tag_ids
|
||||
json.tags availability.tags do |t|
|
||||
json.id t.id
|
||||
json.name t.name
|
||||
end
|
||||
|
||||
if availability.available_type != 'machines'
|
||||
json.borderColor trainings_events_border_color(availability)
|
||||
if availability.is_reserved
|
||||
json.is_reserved true
|
||||
json.title "#{availability.title}' - #{t('trainings.i_ve_reserved')}"
|
||||
elsif availability.is_completed
|
||||
json.is_completed true
|
||||
json.title "#{availability.title} - #{t('trainings.completed')}"
|
||||
end
|
||||
else
|
||||
json.borderColor availability_border_color(availability)
|
||||
end
|
||||
|
||||
# machine slot object
|
||||
else
|
||||
json.borderColor machines_slot_border_color(availability)
|
||||
|
@ -10,7 +10,7 @@ json.array!(@availabilities) do |a|
|
||||
else
|
||||
json.title a.trainings[0].name
|
||||
end
|
||||
json.borderColor trainings_border_color(a)
|
||||
json.borderColor trainings_events_border_color(a)
|
||||
json.start a.start_at.iso8601
|
||||
json.end a.end_at.iso8601
|
||||
json.backgroundColor 'white'
|
||||
|
Loading…
x
Reference in New Issue
Block a user