mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-17 06:52:27 +01:00
refacto subscription extension with first training using a service
This commit is contained in:
parent
7a523a1cf8
commit
7649e953e4
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user