diff --git a/CHANGELOG.md b/CHANGELOG.md index e63f44cb4..e883489b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/controllers/open_api/v1/base_controller.rb b/app/controllers/open_api/v1/base_controller.rb index 45cce71f8..3d96c8d8b 100644 --- a/app/controllers/open_api/v1/base_controller.rb +++ b/app/controllers/open_api/v1/base_controller.rb @@ -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 diff --git a/app/controllers/open_api/v1/bookable_machines_controller.rb b/app/controllers/open_api/v1/bookable_machines_controller.rb index d7a981d76..efbd40cae 100644 --- a/app/controllers/open_api/v1/bookable_machines_controller.rb +++ b/app/controllers/open_api/v1/bookable_machines_controller.rb @@ -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 diff --git a/app/controllers/open_api/v1/events_controller.rb b/app/controllers/open_api/v1/events_controller.rb index 169c367ef..920bf6ce8 100644 --- a/app/controllers/open_api/v1/events_controller.rb +++ b/app/controllers/open_api/v1/events_controller.rb @@ -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 diff --git a/app/controllers/open_api/v1/invoices_controller.rb b/app/controllers/open_api/v1/invoices_controller.rb index 985f01ba4..373e18910 100644 --- a/app/controllers/open_api/v1/invoices_controller.rb +++ b/app/controllers/open_api/v1/invoices_controller.rb @@ -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? diff --git a/app/controllers/open_api/v1/reservations_controller.rb b/app/controllers/open_api/v1/reservations_controller.rb index b1218d992..807716793 100644 --- a/app/controllers/open_api/v1/reservations_controller.rb +++ b/app/controllers/open_api/v1/reservations_controller.rb @@ -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 diff --git a/app/controllers/open_api/v1/trainings_controller.rb b/app/controllers/open_api/v1/trainings_controller.rb index ded339d63..277c99626 100644 --- a/app/controllers/open_api/v1/trainings_controller.rb +++ b/app/controllers/open_api/v1/trainings_controller.rb @@ -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 diff --git a/app/controllers/open_api/v1/user_trainings_controller.rb b/app/controllers/open_api/v1/user_trainings_controller.rb index a8a978391..38f0eab18 100644 --- a/app/controllers/open_api/v1/user_trainings_controller.rb +++ b/app/controllers/open_api/v1/user_trainings_controller.rb @@ -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 diff --git a/app/controllers/open_api/v1/users_controller.rb b/app/controllers/open_api/v1/users_controller.rb index 46a9bf433..00ac3b6ca 100644 --- a/app/controllers/open_api/v1/users_controller.rb +++ b/app/controllers/open_api/v1/users_controller.rb @@ -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 diff --git a/app/views/open_api/v1/invoices/index.json.jbuilder b/app/views/open_api/v1/invoices/index.json.jbuilder index 1ded6c5ce..e4def16bd 100644 --- a/app/views/open_api/v1/invoices/index.json.jbuilder +++ b/app/views/open_api/v1/invoices/index.json.jbuilder @@ -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 diff --git a/app/views/open_api/v1/reservations/index.json.jbuilder b/app/views/open_api/v1/reservations/index.json.jbuilder index 04d20bd80..dc4b24e5b 100644 --- a/app/views/open_api/v1/reservations/index.json.jbuilder +++ b/app/views/open_api/v1/reservations/index.json.jbuilder @@ -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 diff --git a/app/views/open_api/v1/user_trainings/index.json.jbuilder b/app/views/open_api/v1/user_trainings/index.json.jbuilder index 868d73752..2991feb4a 100644 --- a/app/views/open_api/v1/user_trainings/index.json.jbuilder +++ b/app/views/open_api/v1/user_trainings/index.json.jbuilder @@ -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