1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

[bug] most OpenAPI endpoints were dysfunctional

This commit is contained in:
Sylvain 2021-06-24 12:36:16 +02:00
parent a61a2e96d8
commit 522df1ccd7
12 changed files with 124 additions and 99 deletions

View File

@ -1,6 +1,7 @@
# Changelog Fab-manager
- Fix a bug: unable to export members list if no subscriptions was taken
- Fix a bug: most OpenAPI endpoints were dysfunctional
## v5.0.6 2021 June 21

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true
# module definition
module OpenAPI::V1; end
# Parameters for OpenAPI endpoints
class OpenAPI::V1::BaseController < ActionController::Base
protect_from_forgery with: :null_session

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# authorized 3rd party softwares can list the bookable machines through the OpenAPI
class OpenAPI::V1::BookableMachinesController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
expose_doc
@ -22,19 +25,15 @@ class OpenAPI::V1::BookableMachinesController < OpenAPI::V1::BaseController
if user.subscription
plan_id = user.subscription.plan_id
return unless user.subscription
@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
plan_id = user.subscription.plan_id
if credit
@hours_remaining[machine.id] = credit.hours - (users_credit.try(:hours_used) || 0)
else
@hours_remaining[machine.id] = 0
end
end
@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
@hours_remaining[machine.id] = credit ? credit.hours - (users_credit.try(:hours_used) || 0) : 0
end
end
end

View File

@ -1,32 +1,37 @@
# frozen_string_literal: true
# authorized 3rd party softwares can manage the events through the OpenAPI
class OpenAPI::V1::EventsController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
include Rails::Pagination
expose_doc
def index
if upcoming
@events = Event.includes(:event_image, :event_files, :availability, :category)
.where('availabilities.end_at >= ?', DateTime.current)
.order('availabilities.start_at ASC').references(:availabilities)
else
@events = Event.includes(:event_image, :event_files, :availability, :category).order(created_at: :desc)
end
@events = Event.includes(:event_image, :event_files, :availability, :category)
@events = if upcoming
@events.references(:availabilities)
.where('availabilities.end_at >= ?', DateTime.current)
.order('availabilities.start_at ASC')
else
@events.order(created_at: :desc)
end
if params[:id].present?
@events = @events.where(id: params[:id])
end
if params[:page].present?
@events = @events.page(params[:page]).per(per_page)
paginate @events, per_page: per_page
end
@events = @events.where(id: params[:id]) if params[:id].present?
return unless params[:page].present?
@events = @events.page(params[:page]).per(per_page)
paginate @events, per_page: per_page
end
private
def per_page
params[:per_page] || 20
end
def upcoming
params[:upcoming] || false
end
def per_page
params[:per_page] || 20
end
def upcoming
params[:upcoming] || false
end
end

View File

@ -1,12 +1,16 @@
# frozen_string_literal: true
# OpenAPI controller for the invoices
class OpenAPI::V1::InvoicesController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
include Rails::Pagination
expose_doc
def index
@invoices = Invoice.order(created_at: :desc)
.references(:invoicing_profiles)
@invoices = @invoices.where(user_id: params[:user_id]) if params[:user_id].present?
@invoices = @invoices.where('invoicing_profiles.user_id = ?', params[:user_id]) if params[:user_id].present?
return unless params[:page].present?

View File

@ -1,36 +1,33 @@
# frozen_string_literal: true
# public API controller for resources of type Reservation
class OpenAPI::V1::ReservationsController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
include Rails::Pagination
expose_doc
def index
@reservations = Reservation.order(created_at: :desc)
.includes(statistic_profile: :user)
.references(:statistic_profiles)
if params[:user_id].present?
@reservations = @reservations.where(user_id: params[:user_id])
else
@reservations = @reservations.includes(user: :profile)
end
@reservations = @reservations.where('statistic_profiles.user_id = ?', params[:user_id]) if params[:user_id].present?
@reservations = @reservations.where(reservable_type: format_type(params[:reservable_type])) if params[:reservable_type].present?
@reservations = @reservations.where(reservable_id: params[:reservable_id]) if params[:reservable_id].present?
if params[:reservable_type].present?
@reservations = @reservations.where(reservable_type: format_type(params[:reservable_type]))
end
return unless params[:page].present?
if params[:reservable_id].present?
@reservations = @reservations.where(reservable_id: params[:reservable_id])
end
if params[:page].present?
@reservations = @reservations.page(params[:page]).per(per_page)
paginate @reservations, per_page: per_page
end
@reservations = @reservations.page(params[:page]).per(per_page)
paginate @reservations, per_page: per_page
end
private
def format_type(type)
type.singularize.classify
end
def per_page
params[:per_page] || 20
end
def format_type(type)
type.singularize.classify
end
def per_page
params[:per_page] || 20
end
end

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# public API controller for resources of type Training
class OpenAPI::V1::TrainingsController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
expose_doc

View File

@ -1,30 +1,30 @@
# frozen_string_literal: true
# public API controller for user's trainings
class OpenAPI::V1::UserTrainingsController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
include Rails::Pagination
expose_doc
def index
@user_trainings = UserTraining.order(created_at: :desc)
@user_trainings = StatisticProfileTraining.includes(statistic_profile: :user)
.includes(:training)
.references(:statistic_profiles)
.order(created_at: :desc)
if params[:user_id].present?
@user_trainings = @user_trainings.where(user_id: params[:user_id])
else
@user_trainings = @user_trainings.includes(user: :profile)
end
if params[:training_id].present?
@user_trainings = @user_trainings.where(training_id: params[:training_id])
else
@user_trainings = @user_trainings.includes(:training)
end
@user_trainings = @user_trainings.where('statistic_profiles.user_id = ?', params[:user_id]) if params[:user_id].present?
@user_trainings = @user_trainings.where(training_id: params[:training_id]) if params[:training_id].present?
if params[:page].present?
@user_trainings = @user_trainings.page(params[:page]).per(per_page)
paginate @user_trainings, per_page: per_page
end
return unless params[:page].present?
@user_trainings = @user_trainings.page(params[:page]).per(per_page)
paginate @user_trainings, per_page: per_page
end
private
def per_page
params[:per_page] || 20
end
def per_page
params[:per_page] || 20
end
end

View File

@ -1,5 +1,9 @@
# frozen_string_literal: true
# public API controller for users
class OpenAPI::V1::UsersController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
include Rails::Pagination
expose_doc
def index
@ -9,19 +13,17 @@ class OpenAPI::V1::UsersController < OpenAPI::V1::BaseController
email_param = params[:email].is_a?(String) ? params[:email].downcase : params[:email].map(&:downcase)
@users = @users.where(email: email_param)
end
@users = @users.where(id: params[:user_id]) if params[:user_id].present?
if params[:user_id].present?
@users = @users.where(id: params[:user_id])
end
return unless params[:page].present?
if params[:page].present?
@users = @users.page(params[:page]).per(per_page)
paginate @users, per_page: per_page
end
@users = @users.page(params[:page]).per(per_page)
paginate @users, per_page: per_page
end
private
def per_page
params[:per_page] || 20
end
def per_page
params[:per_page] || 20
end
end

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true
json.invoices @invoices do |invoice|
json.extract! invoice, :id, :user_id, :reference, :total, :type, :description
json.extract! invoice, :id, :reference, :total, :type, :description
json.user_id invoice.statistic_profile.user_id
if invoice.payment_gateway_object
json.payment_gateway_object do
json.id invoice.payment_gateway_object.gateway_object_id

View File

@ -1,18 +1,23 @@
json.reservations @reservations do |reservation|
json.extract! reservation, :id, :user_id, :reservable_id, :reservable_type, :updated_at, :created_at
# frozen_string_literal: true
if reservation.association(:user).loaded?
json.user do
json.partial! 'open_api/v1/users/user', user: reservation.user
json.reservations @reservations do |reservation|
json.extract! reservation, :id, :reservable_id, :reservable_type, :updated_at, :created_at
if reservation.association(:statistic_profile).loaded?
json.user_id reservation.statistic_profile.user_id
unless reservation.statistic_profile.user.nil?
json.user do
json.partial! 'open_api/v1/users/user', user: reservation.statistic_profile.user
end
end
end
json.reservable do
if reservation.reservable_type == "Training"
if reservation.reservable_type == 'Training'
json.partial! 'open_api/v1/trainings/training', training: reservation.reservable
elsif reservation.reservable_type == "Machine"
elsif reservation.reservable_type == 'Machine'
json.partial! 'open_api/v1/machines/machine', machine: reservation.reservable
elsif reservation.reservable_type == "Event"
elsif reservation.reservable_type == 'Event'
json.partial! 'open_api/v1/events/event', event: reservation.reservable
end
end

View File

@ -1,9 +1,14 @@
json.user_trainings @user_trainings do |user_training|
json.extract! user_training, :id, :user_id, :training_id, :updated_at, :created_at
# frozen_string_literal: true
if user_training.association(:user).loaded?
json.user do
json.partial! 'open_api/v1/users/user', user: user_training.user
json.user_trainings @user_trainings do |user_training|
json.extract! user_training, :id, :training_id, :updated_at, :created_at
if user_training.association(:statistic_profile).loaded?
json.user_id user_training.statistic_profile.user_id
unless user_training.statistic_profile.user.nil?
json.user do
json.partial! 'open_api/v1/users/user', user: user_training.statistic_profile.user
end
end
end