From 0ac99e3b6b8004ae316a340fd4477c8341a700f4 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 14 Mar 2023 11:54:41 +0100 Subject: [PATCH] (quality) improved error message --- app/controllers/api/local_payment_controller.rb | 2 ++ app/controllers/api/payzen_controller.rb | 2 +- app/controllers/api/stripe_controller.rb | 4 ++-- .../src/javascript/components/plans/plan-limit-form.tsx | 4 ++-- app/models/cart_item/reservation.rb | 5 ++++- app/models/shopping_cart.rb | 1 + app/services/cart_service.rb | 3 +-- config/locales/en.yml | 2 +- 8 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/local_payment_controller.rb b/app/controllers/api/local_payment_controller.rb index 126510dd4..a5c18200f 100644 --- a/app/controllers/api/local_payment_controller.rb +++ b/app/controllers/api/local_payment_controller.rb @@ -8,6 +8,8 @@ class API::LocalPaymentController < API::PaymentsController authorize LocalPaymentContext.new(cart, price[:amount]) + render json: cart.errors, status: :unprocessable_entity and return unless cart.valid? + render on_payment_success(nil, nil, cart) end diff --git a/app/controllers/api/payzen_controller.rb b/app/controllers/api/payzen_controller.rb index 69fbaa70d..14eaf310b 100644 --- a/app/controllers/api/payzen_controller.rb +++ b/app/controllers/api/payzen_controller.rb @@ -55,7 +55,7 @@ class API::PayzenController < API::PaymentsController def check_cart cart = shopping_cart - render json: { error: cart.errors }, status: :unprocessable_entity and return unless cart.valid? + render json: cart.errors, status: :unprocessable_entity and return unless cart.valid? render json: { cart: 'ok' }, status: :ok end diff --git a/app/controllers/api/stripe_controller.rb b/app/controllers/api/stripe_controller.rb index 05b3a6e2b..e885c6fd4 100644 --- a/app/controllers/api/stripe_controller.rb +++ b/app/controllers/api/stripe_controller.rb @@ -19,7 +19,7 @@ class API::StripeController < API::PaymentsController res = nil # json of the API answer cart = shopping_cart - render json: { error: cart.errors }, status: :unprocessable_entity and return unless cart.valid? + render json: cart.errors, status: :unprocessable_entity and return unless cart.valid? begin amount = debit_amount(cart) # will contains the amount and the details of each invoice lines @@ -73,7 +73,7 @@ class API::StripeController < API::PaymentsController def setup_subscription cart = shopping_cart - render json: { error: cart.errors }, status: :unprocessable_entity and return unless cart.valid? + render json: cart.errors, status: :unprocessable_entity and return unless cart.valid? service = Stripe::Service.new method = service.attach_method_as_default( diff --git a/app/frontend/src/javascript/components/plans/plan-limit-form.tsx b/app/frontend/src/javascript/components/plans/plan-limit-form.tsx index 2c1888934..acf66d416 100644 --- a/app/frontend/src/javascript/components/plans/plan-limit-form.tsx +++ b/app/frontend/src/javascript/components/plans/plan-limit-form.tsx @@ -69,7 +69,7 @@ export const PlanLimitForm = ({ register, control, for update(edited.index, { ...limitation, id }); setEdited(null); } else { - append({ ...limitation }); + append({ ...limitation, id }); } }; @@ -97,7 +97,7 @@ export const PlanLimitForm = ({ register, control, for remove(index); // This have a little drowback: remove(index) will set the form as "dirty", and trigger the "unsaved form alert", even if clicking on save or not // won't change anything to the deleted item. To improve this we could do the following: do not destroy the limitation through the API and instead - // set {_destroy: true} and destroy the limitation when saving the form but we need some UI for items about to be deleted + // set {_destroy: true} and destroy the limitation when saving the form, but we need some UI for items about to be deleted // update(index, { ...getValues(`plan_limitations_attributes.${index}`), _destroy: true }); }; }; diff --git a/app/models/cart_item/reservation.rb b/app/models/cart_item/reservation.rb index e3a136361..4a8a80b36 100644 --- a/app/models/cart_item/reservation.rb +++ b/app/models/cart_item/reservation.rb @@ -67,7 +67,10 @@ class CartItem::Reservation < CartItem::BaseItem reservation_deadline = reservation_deadline_minutes.minutes.since unless ReservationLimitService.authorized?(plan, customer, self, all_items) - errors.add(:reservation, I18n.t('cart_item_validation.limit_reached', { HOURS: ReservationLimitService.limit(plan, reservable) })) + errors.add(:reservation, I18n.t('cart_item_validation.limit_reached', { + HOURS: ReservationLimitService.limit(plan, reservable), + RESERVABLE: reservable.name + })) return false end diff --git a/app/models/shopping_cart.rb b/app/models/shopping_cart.rb index 01b2c6046..31ed97e43 100644 --- a/app/models/shopping_cart.rb +++ b/app/models/shopping_cart.rb @@ -160,6 +160,7 @@ class ShoppingCart # Check if the current cart needs the user to have been validated, and if the condition is satisfied. # Return an array of errors, if any; false otherwise + # @return [Array,FalseClass] def check_user_validation(items) user_validation_required = Setting.get('user_validation_required') user_validation_required_list = Setting.get('user_validation_required_list') diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index aea1c834d..6c6588e4b 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -6,10 +6,9 @@ class CartService @operator = operator end - ## # For details about the expected hash format # @see app/frontend/src/javascript/models/payment.ts > interface ShoppingCart - ## + # @return [ShoppingCart] def from_hash(cart_items) cart_items.permit! if cart_items.is_a? ActionController::Parameters diff --git a/config/locales/en.yml b/config/locales/en.yml index 1f3360329..d8a3b9064 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -519,7 +519,7 @@ en: availability: "The availaility doesn't exist" full: "The slot is already fully reserved" deadline: "You can't reserve a slot %{MINUTES} minutes prior to its start" - limit_reached: "You have reached the booking limit of %{HOURS}H per day for your current subscription" + limit_reached: "You have reached the booking limit of %{HOURS}H per day for the %{RESERVABLE}, for your current subscription" restricted: "This availability is restricted for subscribers" plan: "This subscription plan is disabled" plan_group: "This subscription plan is reserved for members of group %{GROUP}"