From a61a2e96d85c42d8204dafcce03e54a124f2909a Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 21 Jun 2021 12:07:59 +0200 Subject: [PATCH 1/5] [bug] unable to export members list (#287) --- CHANGELOG.md | 2 ++ app/controllers/api/members_controller.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f550379d8..e63f44cb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog Fab-manager +- Fix a bug: unable to export members list if no subscriptions was taken + ## v5.0.6 2021 June 21 - Updated babel and its dependencies to 7.14.5 / 7.14.6 diff --git a/app/controllers/api/members_controller.rb b/app/controllers/api/members_controller.rb index 7bdd65947..621010089 100644 --- a/app/controllers/api/members_controller.rb +++ b/app/controllers/api/members_controller.rb @@ -120,7 +120,7 @@ class API::MembersController < API::ApiController Profile.where(user_id: User.members).maximum('updated_at'), InvoicingProfile.where(user_id: User.members).maximum('updated_at'), StatisticProfile.where(user_id: User.members).maximum('updated_at'), - Subscription.maximum('updated_at') + Subscription.maximum('updated_at') || DateTime.current ].max export = Export.where(category: 'users', export_type: 'members') From 522df1ccd77d6293055066f646caf6d0ec0b3d72 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 24 Jun 2021 12:36:16 +0200 Subject: [PATCH 2/5] [bug] most OpenAPI endpoints were dysfunctional --- CHANGELOG.md | 1 + .../open_api/v1/base_controller.rb | 3 ++ .../v1/bookable_machines_controller.rb | 21 ++++---- .../open_api/v1/events_controller.rb | 49 ++++++++++--------- .../open_api/v1/invoices_controller.rb | 6 ++- .../open_api/v1/reservations_controller.rb | 41 +++++++--------- .../open_api/v1/trainings_controller.rb | 3 ++ .../open_api/v1/user_trainings_controller.rb | 38 +++++++------- .../open_api/v1/users_controller.rb | 22 +++++---- .../open_api/v1/invoices/index.json.jbuilder | 3 +- .../v1/reservations/index.json.jbuilder | 21 +++++--- .../v1/user_trainings/index.json.jbuilder | 15 ++++-- 12 files changed, 124 insertions(+), 99 deletions(-) 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 From 4c313d180b522c3bd8f0e6744ea948a16b701768 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 24 Jun 2021 12:40:41 +0200 Subject: [PATCH 3/5] [bug] unable to open some modals when the logo was undefined --- CHANGELOG.md | 1 + app/frontend/src/javascript/components/base/fab-modal.tsx | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e883489b3..a6d93d416 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fix a bug: unable to export members list if no subscriptions was taken - Fix a bug: most OpenAPI endpoints were dysfunctional +- Fix a bug: unable to open some modals when the logo was undefined ## v5.0.6 2021 June 21 diff --git a/app/frontend/src/javascript/components/base/fab-modal.tsx b/app/frontend/src/javascript/components/base/fab-modal.tsx index 09370652b..4b2a17cff 100644 --- a/app/frontend/src/javascript/components/base/fab-modal.tsx +++ b/app/frontend/src/javascript/components/base/fab-modal.tsx @@ -74,8 +74,8 @@ export const FabModal: React.FC = ({ title, isOpen, toggleModal, onRequestClose={toggleModal}>
- {blackLogo.custom_asset_file_attributes.attachment}

{ title }

From 07017e4a4949127e147acf2e4d13266b3f7c5c59 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 24 Jun 2021 16:52:47 +0200 Subject: [PATCH 4/5] [bug] stripe subscription generation fails if the user already has a subscription --- CHANGELOG.md | 1 + app/pdfs/pdf/payment_schedule.rb | 2 +- app/services/cart_service.rb | 2 +- app/services/payment_schedule_service.rb | 6 +++--- lib/stripe/service.rb | 2 +- test/integration/reservations/create_test.rb | 4 ++-- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6d93d416..21a349657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Fix a bug: unable to export members list if no subscriptions was taken - Fix a bug: most OpenAPI endpoints were dysfunctional - Fix a bug: unable to open some modals when the logo was undefined +- Fix a bug: stripe subscription generation fails if the user already has a subscription ## v5.0.6 2021 June 21 diff --git a/app/pdfs/pdf/payment_schedule.rb b/app/pdfs/pdf/payment_schedule.rb index d89ccb350..061bd8065 100644 --- a/app/pdfs/pdf/payment_schedule.rb +++ b/app/pdfs/pdf/payment_schedule.rb @@ -63,7 +63,7 @@ class PDF::PaymentSchedule < Prawn::Document align: :right, inline_format: true name = full_name - subscription = payment_schedule.payment_schedule_objects.find(&:subscription).subscription + subscription = payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }.subscription # object move_down 25 diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index 60bf501ce..5b44d9ca4 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -38,7 +38,7 @@ class CartService def from_payment_schedule(payment_schedule) @customer = payment_schedule.user - plan = payment_schedule.payment_schedule_objects.find(&:subscription)&.subscription&.plan + plan = payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }&.subscription&.plan coupon = CartItem::Coupon.new(@customer, @operator, payment_schedule.coupon&.code) schedule = CartItem::PaymentSchedule.new(plan, coupon, true) diff --git a/app/services/payment_schedule_service.rb b/app/services/payment_schedule_service.rb index 379c0e651..978f06f4e 100644 --- a/app/services/payment_schedule_service.rb +++ b/app/services/payment_schedule_service.rb @@ -162,7 +162,7 @@ class PaymentScheduleService item.update_attributes(state: 'canceled') end # cancel subscription - subscription = payment_schedule.payment_schedule_objects.find(&:subscription).subscription + subscription = payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }.subscription subscription.expire(DateTime.current) subscription.canceled_at @@ -181,7 +181,7 @@ class PaymentScheduleService } # the subscription and reservation items - subscription = payment_schedule_item.payment_schedule.payment_schedule_objects.find(&:subscription).subscription + subscription = payment_schedule_item.payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }.subscription if payment_schedule_item.payment_schedule.main_object.object_type == Reservation.name details[:reservation] = payment_schedule_item.details['other_items'] reservation = payment_schedule_item.payment_schedule.main_object.reservation @@ -200,7 +200,7 @@ class PaymentScheduleService ## def complete_next_invoice(payment_schedule_item, invoice) # the subscription item - subscription = payment_schedule_item.payment_schedule.payment_schedule_objects.find(&:subscription).subscription + subscription = payment_schedule_item.payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }.subscription # sub-price for the subscription details = { subscription: payment_schedule_item.details['recurring'] } diff --git a/lib/stripe/service.rb b/lib/stripe/service.rb index f9d1624d6..afb5c4a3d 100644 --- a/lib/stripe/service.rb +++ b/lib/stripe/service.rb @@ -14,7 +14,7 @@ class Stripe::Service < Payment::Service case payment_schedule.main_object.object_type when Reservation.name - subscription = payment_schedule.payment_schedule_objects.find(&:subscription).object + subscription = payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }.object reservable_stp_id = payment_schedule.main_object.object.reservable&.payment_gateway_object&.gateway_object_id when Subscription.name subscription = payment_schedule.main_object.object diff --git a/test/integration/reservations/create_test.rb b/test/integration/reservations/create_test.rb index 701c11062..d09b7cf05 100644 --- a/test/integration/reservations/create_test.rb +++ b/test/integration/reservations/create_test.rb @@ -787,7 +787,7 @@ class Reservations::CreateTest < ActionDispatch::IntegrationTest # Check the answer result = json_response(response.body) assert_equal payment_schedule.id, result[:id], 'payment schedule id does not match' - subscription = payment_schedule.payment_schedule_objects.find(&:subscription).object + subscription = payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }.object assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match' end @@ -909,7 +909,7 @@ class Reservations::CreateTest < ActionDispatch::IntegrationTest # Check the answer result = json_response(response.body) assert_equal payment_schedule.id, result[:id], 'payment schedule id does not match' - subscription = payment_schedule.payment_schedule_objects.find(&:subscription).object + subscription = payment_schedule.payment_schedule_objects.find { |pso| pso.object_type == Subscription.name }.object assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match' end end From 55991a0b3bc466c1ddcb087d3a4e7291bb8a9b5f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 24 Jun 2021 16:55:48 +0200 Subject: [PATCH 5/5] Version 5.0.7 --- CHANGELOG.md | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21a349657..5a3df5c60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog Fab-manager +## v5.0.7 2021 June 24 + - Fix a bug: unable to export members list if no subscriptions was taken - Fix a bug: most OpenAPI endpoints were dysfunctional - Fix a bug: unable to open some modals when the logo was undefined diff --git a/package.json b/package.json index b334f030f..b36fba43f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fab-manager", - "version": "5.0.6", + "version": "5.0.7", "description": "Fab-manager is the FabLab management solution. It provides a comprehensive, web-based, open-source tool to simplify your administrative tasks and your marker's projects.", "keywords": [ "fablab",