1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-30 11:24:21 +01:00
fab-manager/app/controllers/api/availabilities_controller.rb

173 lines
5.9 KiB
Ruby
Raw Normal View History

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
before_action :authenticate_user!, except: [:public]
2019-11-18 17:29:51 +01:00
before_action :set_availability, only: %i[show update reservations lock]
2022-07-12 17:46:01 +02:00
before_action :set_operator_role, only: %i[machine spaces trainings]
before_action :set_customer, only: %i[machine spaces trainings]
2016-03-23 18:39:41 +01:00
respond_to :json
def index
authorize Availability
display_window = window
2019-01-17 16:26:03 +01:00
@availabilities = Availability.includes(:machines, :tags, :trainings, :spaces)
.where('start_at >= ? AND end_at <= ?', display_window[:start], display_window[:end])
@availabilities = @availabilities.where.not(available_type: 'event') unless Setting.get('events_in_calendar')
@availabilities = @availabilities.where.not(available_type: 'space') unless Setting.get('spaces_module')
2016-03-23 18:39:41 +01:00
end
def public
display_window = window
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(
display_window,
{ machines: machine_ids, spaces: params[:s], trainings: params[:t] },
(params[:evt] && params[:evt] == 'true')
2019-01-17 16:26:03 +01:00
)
@title_filter = { machine_ids: machine_ids.map(&:to_i) }
@availabilities = filter_availabilites(@availabilities)
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
service = Availabilities::CreateAvailabilitiesService.new
service.create(@availability, params[:availability][:occurrences])
2016-03-23 18:39:41 +01:00
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
2019-11-18 17:29:51 +01:00
service = Availabilities::DeleteAvailabilitiesService.new
res = service.delete(params[:id], params[:mode])
if res.all? { |r| r[:status] }
render json: { deleted: res.length, details: res }, status: :ok
2016-03-23 18:39:41 +01:00
else
2019-11-18 17:29:51 +01:00
render json: { total: res.length, deleted: res.select { |r| r[:status] }.length, details: res }, status: :unprocessable_entity
2016-03-23 18:39:41 +01:00
end
end
def machine
service = Availabilities::AvailabilitiesService.new(current_user)
@machine = Machine.friendly.find(params[:machine_id])
@slots = service.machines([@machine], @customer, window)
2016-03-23 18:39:41 +01:00
end
def trainings
service = Availabilities::AvailabilitiesService.new(current_user)
@trainings = if params[:training_id].is_number? || (params[:training_id].length.positive? && params[:training_id] != 'all')
[Training.friendly.find(params[:training_id])]
else
Training.all
end
@slots = service.trainings(@trainings, @customer, window)
2016-03-23 18:39:41 +01:00
end
2017-02-23 17:45:55 +01:00
def spaces
service = Availabilities::AvailabilitiesService.new(current_user)
@space = Space.friendly.find(params[:space_id])
@slots = service.spaces([@space], @customer, window)
2017-02-23 17:45:55 +01:00
end
2016-03-23 18:39:41 +01:00
def reservations
authorize Availability
2022-07-12 17:46:01 +02:00
@slots_reservations = @availability.slots_reservations
.includes(:slot, reservation: [statistic_profile: [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
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
def window
start_date = ActiveSupport::TimeZone[params[:timezone]]&.parse(params[:start])
end_date = ActiveSupport::TimeZone[params[:timezone]]&.parse(params[:end])&.end_of_day
{ start: start_date, end: end_date }
end
def set_customer
@customer = if params[:member_id]
User.find(params[:member_id])
else
current_user
end
end
def set_operator_role
2022-07-12 17:46:01 +02:00
@operator_role = current_user.role
2019-01-17 16:26:03 +01:00
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,
:is_recurrent, :period, :nb_periods, :end_date, :slot_duration,
machine_ids: [], training_ids: [], space_ids: [], tag_ids: [], plan_ids: [],
machines_attributes: %i[id _destroy], plans_attributes: %i[id _destroy])
2018-12-17 16:02:02 +01:00
end
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.delete_if(&method(:remove_full?))
2018-12-17 16:02:02 +01:00
end
2022-06-29 10:30:17 +02:00
def remove_full?(availability)
params[:dispo] == 'false' && (availability.is_reserved || (availability.try(:full?) && availability.full?))
2018-12-17 16:02:02 +01:00
end
2016-03-23 18:39:41 +01:00
end