2021-06-24 12:36:16 +02:00
|
|
|
# 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|
|
2018-12-03 15:10:04 +01:00
|
|
|
(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
|
|
|
|
2021-06-24 12:36:16 +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
|
|
|
|
2021-06-24 12:36:16 +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
|