2021-05-31 15:39:56 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-07 17:55:36 +02:00
|
|
|
class SubscriptionExtensionAfterReservation
|
|
|
|
attr_accessor :user, :reservation
|
|
|
|
|
|
|
|
def initialize(reservation)
|
2018-12-06 18:26:01 +01:00
|
|
|
@user = reservation.user
|
|
|
|
@reservation = reservation
|
2016-04-07 17:55:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def extend_subscription_if_eligible
|
|
|
|
extend_subscription if eligible_to_extension?
|
|
|
|
end
|
|
|
|
|
|
|
|
def eligible_to_extension?
|
|
|
|
return false unless reservation.reservable_type == 'Training'
|
|
|
|
return false if user.reservations.where(reservable_type: 'Training').count != 1
|
|
|
|
return false unless user.subscription
|
2018-12-06 18:26:01 +01:00
|
|
|
return false if user.subscription.expired?
|
2016-04-07 17:55:36 +02:00
|
|
|
return false unless user.subscribed_plan.is_rolling
|
2018-12-06 18:26:01 +01:00
|
|
|
|
|
|
|
true
|
2016-04-07 17:55:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def extend_subscription
|
|
|
|
user.subscription.update_columns(
|
2018-12-10 17:20:23 +01:00
|
|
|
expiration_date: reservation.slots.first.start_at + user.subscribed_plan.duration
|
2016-04-07 17:55:36 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|