2019-01-17 16:26:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Availability
|
2016-03-23 18:39:41 +01:00
|
|
|
class API::AvailabilitiesController < API::ApiController
|
2017-02-15 15:41:25 +01:00
|
|
|
include FablabConfiguration
|
|
|
|
|
2016-06-29 17:37:22 +02:00
|
|
|
before_action :authenticate_user!, except: [:public]
|
2018-12-17 16:02:02 +01:00
|
|
|
before_action :set_availability, only: %i[show update destroy reservations lock]
|
|
|
|
before_action :define_max_visibility, only: %i[machine trainings spaces]
|
2016-03-23 18:39:41 +01:00
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
|
|
|
authorize Availability
|
2016-07-13 19:09:07 +02:00
|
|
|
start_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:start])
|
|
|
|
end_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:end]).end_of_day
|
2019-01-17 16:26:03 +01:00
|
|
|
@availabilities = Availability.includes(:machines, :tags, :trainings, :spaces)
|
|
|
|
.where.not(available_type: 'event')
|
2016-07-13 19:09:07 +02:00
|
|
|
.where('start_at >= ? AND end_at <= ?', start_date, end_date)
|
2017-02-15 15:41:25 +01:00
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
@availabilities = @availabilities.where.not(available_type: 'space') if fablab_spaces_deactivated?
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2016-07-13 19:12:16 +02:00
|
|
|
def public
|
|
|
|
start_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:start])
|
|
|
|
end_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:end]).end_of_day
|
2019-01-17 16:26:03 +01:00
|
|
|
@reservations = Reservation.includes(:slots, user: [:profile])
|
|
|
|
.references(:slots, :user)
|
|
|
|
.where('slots.start_at >= ? AND slots.end_at <= ?', start_date, end_date)
|
2017-02-28 11:59:48 +01:00
|
|
|
|
2016-07-14 18:36:52 +02:00
|
|
|
machine_ids = params[:m] || []
|
2019-01-17 16:55:25 +01:00
|
|
|
service = Availabilities::PublicAvailabilitiesService.new(current_user)
|
2019-01-17 16:26:03 +01:00
|
|
|
@availabilities = service.public_availabilities(
|
|
|
|
start_date,
|
|
|
|
end_date,
|
|
|
|
@reservations,
|
|
|
|
machines: machine_ids, spaces: params[:s]
|
|
|
|
)
|
|
|
|
|
|
|
|
@title_filter = { machine_ids: machine_ids.map(&:to_i) }
|
2016-07-14 18:36:52 +02:00
|
|
|
@availabilities = filter_availabilites(@availabilities)
|
2016-07-13 19:12:16 +02:00
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def show
|
|
|
|
authorize Availability
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Availability
|
|
|
|
@availability = Availability.new(availability_params)
|
|
|
|
if @availability.save
|
|
|
|
render :show, status: :created, location: @availability
|
|
|
|
else
|
|
|
|
render json: @availability.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Availability
|
|
|
|
if @availability.update(availability_params)
|
|
|
|
render :show, status: :ok, location: @availability
|
|
|
|
else
|
|
|
|
render json: @availability.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize Availability
|
|
|
|
if @availability.safe_destroy
|
|
|
|
head :no_content
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def machine
|
2019-01-14 12:57:31 +01:00
|
|
|
@current_user_role = current_user.admin? ? 'admin' : 'user'
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2019-01-17 16:55:25 +01:00
|
|
|
service = Availabilities::AvailabilitiesService.new(current_user, other: @visi_max_other, year: @visi_max_year)
|
2019-01-17 16:26:03 +01:00
|
|
|
@slots = service.machines(params[:machine_id], user)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def trainings
|
2019-01-17 16:55:25 +01:00
|
|
|
service = Availabilities::AvailabilitiesService.new(current_user, other: @visi_max_other, year: @visi_max_year)
|
|
|
|
@availabilities = service.trainings(params[:training_id], user)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2017-02-23 17:45:55 +01:00
|
|
|
def spaces
|
2019-01-14 12:57:31 +01:00
|
|
|
@current_user_role = current_user.admin? ? 'admin' : 'user'
|
2019-01-17 16:26:03 +01:00
|
|
|
|
2019-01-17 16:55:25 +01:00
|
|
|
service = Availabilities::AvailabilitiesService.new(current_user, other: @visi_max_other, year: @visi_max_year)
|
2019-01-17 16:26:03 +01:00
|
|
|
@slots = service.spaces(params[:space_id], user)
|
2017-02-23 17:45:55 +01:00
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def reservations
|
|
|
|
authorize Availability
|
2017-02-27 16:43:39 +01:00
|
|
|
@reservation_slots = @availability.slots.includes(reservations: [user: [:profile]]).order('slots.start_at ASC')
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2017-03-02 12:34:28 +01:00
|
|
|
def export_availabilities
|
|
|
|
authorize :export
|
|
|
|
|
2018-12-17 16:02:02 +01:00
|
|
|
export = Export.where(category: 'availabilities', export_type: 'index')
|
|
|
|
.where('created_at > ?', Availability.maximum('updated_at')).last
|
2017-03-02 12:34:28 +01:00
|
|
|
if export.nil? || !FileTest.exist?(export.file)
|
2018-12-17 16:02:02 +01:00
|
|
|
@export = Export.new(category: 'availabilities', export_type: 'index', user: current_user)
|
2017-03-02 12:34:28 +01:00
|
|
|
if @export.save
|
2019-01-17 16:26:03 +01:00
|
|
|
render json: { export_id: @export.id }, status: :ok
|
2017-03-02 12:34:28 +01:00
|
|
|
else
|
|
|
|
render json: @export.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
2018-12-17 16:02:02 +01:00
|
|
|
send_file File.join(Rails.root, export.file),
|
|
|
|
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
|
|
disposition: 'attachment'
|
2017-03-02 12:34:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-06 15:01:41 +02:00
|
|
|
def lock
|
|
|
|
authorize @availability
|
|
|
|
if @availability.update_attributes(lock: lock_params)
|
|
|
|
render :show, status: :ok, location: @availability
|
|
|
|
else
|
|
|
|
render json: @availability.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
def user
|
|
|
|
if params[:member_id]
|
|
|
|
User.find(params[:member_id])
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-17 16:02:02 +01:00
|
|
|
def set_availability
|
|
|
|
@availability = Availability.find(params[:id])
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-12-17 16:02:02 +01:00
|
|
|
def availability_params
|
|
|
|
params.require(:availability).permit(:start_at, :end_at, :available_type, :machine_ids, :training_ids, :nb_total_places,
|
|
|
|
machine_ids: [], training_ids: [], space_ids: [], tag_ids: [],
|
|
|
|
machines_attributes: %i[id _destroy])
|
|
|
|
end
|
2017-09-06 15:01:41 +02:00
|
|
|
|
2018-12-17 16:02:02 +01:00
|
|
|
def lock_params
|
|
|
|
params.require(:lock)
|
|
|
|
end
|
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
def filter_availabilites(availabilities)
|
|
|
|
availabilities_filtered = []
|
|
|
|
availabilities.to_ary.each do |a|
|
|
|
|
# machine slot
|
|
|
|
if !a.try(:available_type)
|
|
|
|
availabilities_filtered << a
|
|
|
|
else
|
|
|
|
availabilities_filtered << a if filter_training?(a)
|
|
|
|
availabilities_filtered << a if filter_space?(a)
|
|
|
|
availabilities_filtered << a if filter_machine?(a)
|
|
|
|
availabilities_filtered << a if filter_event?(a)
|
2017-02-23 17:45:55 +01:00
|
|
|
end
|
|
|
|
end
|
2019-01-17 16:26:03 +01:00
|
|
|
availabilities_filtered.delete_if(&method(:remove_completed?))
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|
2017-02-23 17:45:55 +01:00
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
def filter_training?(availability)
|
|
|
|
params[:t] && availability.available_type == 'training' && params[:t].include?(availability.trainings.first.id.to_s)
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
def filter_space?(availability)
|
|
|
|
params[:s] && availability.available_type == 'space' && params[:s].include?(availability.spaces.first.id.to_s)
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
def filter_machine?(availability)
|
|
|
|
params[:m] && availability.available_type == 'machines' && (params[:m].map(&:to_i) & availability.machine_ids).any?
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|
2016-06-28 18:58:44 +02:00
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
def filter_event?(availability)
|
|
|
|
params[:evt] && params[:evt] == 'true' && availability.available_type == 'event'
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|
2016-07-14 18:36:52 +02:00
|
|
|
|
2019-01-17 16:26:03 +01:00
|
|
|
def remove_completed?(availability)
|
2019-01-17 16:55:25 +01:00
|
|
|
params[:dispo] == 'false' && (availability.is_reserved || (availability.try(:completed?) && availability.completed?))
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|
2017-08-24 18:34:18 +02:00
|
|
|
|
|
|
|
def define_max_visibility
|
|
|
|
@visi_max_year = Setting.find_by(name: 'visibility_yearly').value.to_i.months.since
|
|
|
|
@visi_max_other = Setting.find_by(name: 'visibility_others').value.to_i.months.since
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|