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