1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-15 00:24:09 +01:00
fab-manager/app/controllers/open_api/v1/bookable_machines_controller.rb

34 lines
1.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# authorized 3rd party softwares can list the bookable machines through the OpenAPI
2016-05-04 18:17:50 +02:00
class OpenAPI::V1::BookableMachinesController < OpenAPI::V1::BaseController
2016-05-05 15:02:02 +02:00
extend OpenAPI::ApiDoc
expose_doc
2016-05-04 18:17:50 +02:00
def index
raise ActionController::ParameterMissing if params[:user_id].blank?
@machines = Machine.all
@machines = @machines.where(id: params[:machine_id]) if params[:machine_id].present?
@machines = @machines.to_a
user = User.find(params[:user_id])
@machines.delete_if do |machine|
(machine.trainings.count != 0) and !user.training_machine?(machine)
2016-05-04 18:17:50 +02:00
end
2022-12-07 10:15:15 +01:00
@hours_remaining = @machines.to_h { |m| [m.id, 0] }
2016-05-04 18:17:50 +02:00
return unless user.subscription
plan_id = user.subscription.plan_id
@machines.each do |machine|
credit = Credit.find_by(plan_id: plan_id, creditable: machine)
users_credit = user.users_credits.find_by(credit: credit) if credit
2016-05-04 18:17:50 +02:00
@hours_remaining[machine.id] = credit ? credit.hours - (users_credit.try(:hours_used) || 0) : 0
2016-05-04 18:17:50 +02:00
end
end
end