From e9e27663cd594ed3fc0663148d5f3928fe9c95a1 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 9 Sep 2019 18:04:31 +0200 Subject: [PATCH] quick coding rules checking with rubocop --- app/assets/javascripts/directives/cart.js.erb | 18 +++----- app/controllers/api/payments_controller.rb | 44 +++++++++---------- .../api/reservations_controller.rb | 4 +- app/models/reservation.rb | 2 +- config/locales/app.shared.en.yml | 2 +- config/locales/app.shared.es.yml | 2 +- config/locales/app.shared.fr.yml | 2 +- config/locales/app.shared.pt.yml | 2 +- 8 files changed, 34 insertions(+), 42 deletions(-) diff --git a/app/assets/javascripts/directives/cart.js.erb b/app/assets/javascripts/directives/cart.js.erb index 1890d6a0c..a558bd319 100644 --- a/app/assets/javascripts/directives/cart.js.erb +++ b/app/assets/javascripts/directives/cart.js.erb @@ -350,11 +350,7 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs', if ($scope.isAdmin()) { return true; } const slotStart = moment(slot.start); const now = moment(); - if (slot.can_modify && $scope.enableBookingMove && (slotStart.diff(now, 'hours') >= $scope.moveBookingDelay)) { - return true; - } else { - return false; - } + return (slot.can_modify && $scope.enableBookingMove && (slotStart.diff(now, 'hours') >= $scope.moveBookingDelay)); }; /** @@ -365,11 +361,7 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs', if ($scope.isAdmin()) { return true; } const slotStart = moment(slot.start); const now = moment(); - if (slot.can_modify && $scope.enableBookingCancel && (slotStart.diff(now, 'hours') >= $scope.cancelBookingDelay)) { - return true; - } else { - return false; - } + return (slot.can_modify && $scope.enableBookingCancel && (slotStart.diff(now, 'hours') >= $scope.cancelBookingDelay)); }; /** @@ -448,7 +440,7 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs', slots_attributes: [], plan_id: ((plan ? plan.id : undefined)) }; - angular.forEach(slots, function (slot, key) { + angular.forEach(slots, function (slot) { reservation.slots_attributes.push({ start_at: slot.start, end_at: slot.end, @@ -608,7 +600,7 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs', } , function (response) { $scope.alerts = []; - $scope.alerts.push({ msg: _t('cart.a_problem_occured_during_the_payment_process_please_try_again_later'), type: 'danger' }); + $scope.alerts.push({ msg: _t('cart.a_problem_occurred_during_the_payment_process_please_try_again_later'), type: 'danger' }); return $scope.attempting = false; }); }; @@ -618,7 +610,7 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs', }).result['finally'](null).then(function (reservation) { afterPayment(reservation); }); }; /** - * Actions to run after the payment was successfull + * Actions to run after the payment was successful */ var afterPayment = function (reservation) { // we set the cart content as 'paid' to display a summary of the transaction diff --git a/app/controllers/api/payments_controller.rb b/app/controllers/api/payments_controller.rb index 2898af73e..a2e0e6a48 100644 --- a/app/controllers/api/payments_controller.rb +++ b/app/controllers/api/payments_controller.rb @@ -5,51 +5,51 @@ class API::PaymentsController < API::ApiController before_action :authenticate_user! def confirm_payment - begin if params[:payment_method_id].present? # Create the PaymentIntent # TODO the client has to provide the reservation details. Then, we use Price.compute - user.walletAmount to get the amount # currency is set in Rails.secrets - + reservable = cart_items_params[:reservable_type].constantize.find(cart_items_params[:reservable_id]) price_details = Price.compute(false, - current_user, - reservable, - cart_items_params[:slots_attributes] || [], - cart_items_params[:plan_id], - cart_items_params[:nb_reserve_places], - cart_items_params[:tickets_attributes], - coupon_params[:coupon_code]) + current_user, + reservable, + cart_items_params[:slots_attributes] || [], + cart_items_params[:plan_id], + cart_items_params[:nb_reserve_places], + cart_items_params[:tickets_attributes], + coupon_params[:coupon_code]) intent = Stripe::PaymentIntent.create( payment_method: params[:payment_method_id], amount: price_details[:total], - currency: 'eur', + currency: 'eur', confirmation_method: 'manual', confirm: true ) elsif params[:payment_intent_id].present? intent = Stripe::PaymentIntent.confirm(params[:payment_intent_id]) - + end rescue Stripe::CardError => e # Display error on client render status: 200, json: { error: e.message } end - if intent.status == 'succeeded' + # TODO extract in subroutine + if intent.status == 'succeeded' begin - user_id = params[:cart_items][:reservation][:user_id] + user_id = params[:cart_items][:reservation][:user_id] @reservation = Reservation.new(reservation_params) is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) .pay_and_save(@reservation, :stripe, coupon_params[:coupon_code]) - + if is_reserve SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible - + render('api/reservations/show', status: :created, location: @reservation) and return else render(json: @reservation.errors, status: :unprocessable_entity) and return @@ -75,7 +75,7 @@ class API::PaymentsController < API::ApiController } } elsif intent.status == 'succeeded' - # The payment didn’t need any additional actions and is completed! + # The payment didn't need any additional actions and is completed! # Handle post-payment fulfillment { status: 200, json: { success: true } } else @@ -86,17 +86,17 @@ class API::PaymentsController < API::ApiController def reservation_params params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :nb_reserve_places, - tickets_attributes: %i[event_price_category_id booked], - slots_attributes: %i[id start_at end_at availability_id offered]) + tickets_attributes: %i[event_price_category_id booked], + slots_attributes: %i[id start_at end_at availability_id offered]) end - + def cart_items_params params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :user_id, :nb_reserve_places, - tickets_attributes: %i[event_price_category_id booked], - slots_attributes: %i[id start_at end_at availability_id offered]) + tickets_attributes: %i[event_price_category_id booked], + slots_attributes: %i[id start_at end_at availability_id offered]) end def coupon_params params.require(:cart_items).permit(:coupon_code) end -end \ No newline at end of file +end diff --git a/app/controllers/api/reservations_controller.rb b/app/controllers/api/reservations_controller.rb index af3dcf9e0..f720c1245 100644 --- a/app/controllers/api/reservations_controller.rb +++ b/app/controllers/api/reservations_controller.rb @@ -23,8 +23,8 @@ class API::ReservationsController < API::ApiController def create authorize Reservation - - user_id = params[:reservation][:user_id] + + user_id = params[:reservation][:user_id] @reservation = Reservation.new(reservation_params) is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 301846ee5..0821fba74 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -203,7 +203,7 @@ class Reservation < ActiveRecord::Base # else # error handling - + # errors[:card] << subscription.errors[:card].join # errors[:payment] << subscription.errors[:payment].join if subscription.errors[:payment] diff --git a/config/locales/app.shared.en.yml b/config/locales/app.shared.en.yml index 0fa871c7b..117dd8d90 100644 --- a/config/locales/app.shared.en.yml +++ b/config/locales/app.shared.en.yml @@ -461,5 +461,5 @@ en: reservation_was_cancelled_successfully: "Reservation was cancelled successfully." cancellation_failed: "Cancellation failed." confirm_payment_of_html: "{ROLE, select, admin{Payment on site} other{Pay}}: {AMOUNT}" # messageFormat interpolation (context: confirm my payment of $20.00) - a_problem_occured_during_the_payment_process_please_try_again_later: "A problem occurred during the payment process. Please try again later." + a_problem_occurred_during_the_payment_process_please_try_again_later: "A problem occurred during the payment process. Please try again later." none: "None" diff --git a/config/locales/app.shared.es.yml b/config/locales/app.shared.es.yml index a02f5db6e..d6f538749 100644 --- a/config/locales/app.shared.es.yml +++ b/config/locales/app.shared.es.yml @@ -461,5 +461,5 @@ es: reservation_was_cancelled_successfully: "La reserva se ha cancelado con éxito." cancellation_failed: "Cancelación fallida." confirm_payment_of_html: "{ROLE, select, admin{Payment on site} other{Pay}}: {AMOUNT}" # messageFormat interpolation (context: confirm my payment of $20.00) - a_problem_occured_during_the_payment_process_please_try_again_later: "A problem occurred during the payment process. Please try again later." + a_problem_occurred_during_the_payment_process_please_try_again_later: "A problem occurred during the payment process. Please try again later." none: "Ninguno" diff --git a/config/locales/app.shared.fr.yml b/config/locales/app.shared.fr.yml index 83de9cdd3..238d3b61f 100644 --- a/config/locales/app.shared.fr.yml +++ b/config/locales/app.shared.fr.yml @@ -461,5 +461,5 @@ fr: reservation_was_cancelled_successfully: "La réservation a bien été annulée." cancellation_failed: "L'annulation a échouée." confirm_payment_of_html: "{ROLE, select, admin{Paiement sur place} other{Payer}} : {AMOUNT}" # messageFormat interpolation (contexte : valider mon paiement de 20,00 €) - a_problem_occured_during_the_payment_process_please_try_again_later: "Il y a eu un problème lors de la procédure de paiement. Veuillez réessayer plus tard." + a_problem_occurred_during_the_payment_process_please_try_again_later: "Il y a eu un problème lors de la procédure de paiement. Veuillez réessayer plus tard." none: "Aucune" diff --git a/config/locales/app.shared.pt.yml b/config/locales/app.shared.pt.yml index 46c7c484f..cc36398c2 100755 --- a/config/locales/app.shared.pt.yml +++ b/config/locales/app.shared.pt.yml @@ -461,5 +461,5 @@ pt: reservation_was_cancelled_successfully: "Reserva a foi cancelada com sucesso." cancellation_failed: "Cancelamento falhou." confirm_payment_of_html: "{ROLE, select, admin{Pagamento pelo site} other{Pagar}}: {AMOUNT}" # messageFormat interpolation (context: confirm my payment of $20.00) - a_problem_occured_during_the_payment_process_please_try_again_later: "Um problema ocorreu durante o processo de pagamento. Por favor tente novamente mais tarde." + a_problem_occurred_during_the_payment_process_please_try_again_later: "Um problema ocorreu durante o processo de pagamento. Por favor tente novamente mais tarde." none: "Vazio"