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
|
|
|
|
|
|
|
|
|
|
|
|
@hours_remaining = Hash[@machines.map { |m| [m.id, 0] }]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 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
|
|
|
|
|
|
|
|
if credit
|
|
|
|
@hours_remaining[machine.id] = credit.hours - (users_credit.try(:hours_used) || 0)
|
|
|
|
else
|
|
|
|
@hours_remaining[machine.id] = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|