diff --git a/app/controllers/api/reservations_controller.rb b/app/controllers/api/reservations_controller.rb index 08cee38a5..f7332d0aa 100644 --- a/app/controllers/api/reservations_controller.rb +++ b/app/controllers/api/reservations_controller.rb @@ -28,10 +28,8 @@ class API::ReservationsController < API::ApiController is_reserve = @reservation.save_with_payment end if is_reserve - reservation_user = @reservation.user - if @reservation.reservable_type == 'Training' and is_first_training_and_active_subscription(reservation_user) - reservation_user.subscription.update_expired_date_with_first_training(@reservation.slots.first.start_at) - end + SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible + render :show, status: :created, location: @reservation else render json: @reservation.errors, status: :unprocessable_entity @@ -57,8 +55,4 @@ class API::ReservationsController < API::ApiController :nb_reserve_places, :nb_reserve_reduced_places, slots_attributes: [:id, :start_at, :end_at, :availability_id, :offered]) end - - def is_first_training_and_active_subscription(user) - user.reservations.where(reservable_type: 'Training').size == 1 and user.subscription and !user.subscription.is_expired? - end end diff --git a/app/controllers/api/slots_controller.rb b/app/controllers/api/slots_controller.rb index 5e5179afe..1d315cd24 100644 --- a/app/controllers/api/slots_controller.rb +++ b/app/controllers/api/slots_controller.rb @@ -6,10 +6,7 @@ class API::SlotsController < API::ApiController def update authorize @slot if @slot.update(slot_params) - reservation_user = @slot.reservation.user - if @slot.reservation.reservable_type == 'Training' and is_first_training_and_active_subscription(reservation_user) - reservation_user.subscription.update_expired_date_with_first_training(@slot.start_at) - end + SubscriptionExtensionAfterReservation.new(@slot.reservation).extend_subscription_if_eligible render :show, status: :created, location: @slot else render json: @slot.errors, status: :unprocessable_entity @@ -29,8 +26,4 @@ class API::SlotsController < API::ApiController def slot_params params.require(:slot).permit(:start_at, :end_at, :availability_id) end - - def is_first_training_and_active_subscription(user) - user.reservations.where(reservable_type: 'Training').size == 1 and user.subscription and !user.subscription.is_expired? - end end diff --git a/app/models/subscription.rb b/app/models/subscription.rb index dfd0bd5e2..1d9ccd240 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -102,12 +102,6 @@ class Subscription < ActiveRecord::Base invoice.save end - def update_expired_date_with_first_training(training_start_at) - if plan.is_rolling? - update_columns(expired_at: training_start_at + plan.duration) - end - end - def cancel if stp_subscription_id.present? stp_subscription = stripe_subscription