2021-05-31 15:39:56 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-03 16:37:14 +01:00
|
|
|
# Services around subscriptions
|
|
|
|
module Subscriptions; end
|
|
|
|
|
2022-07-12 17:46:01 +02:00
|
|
|
# Extend the user's current subscription after his first training reservation if
|
|
|
|
# he subscribed to a rolling plan
|
2023-02-17 15:35:06 +01:00
|
|
|
class Subscriptions::ExtensionAfterReservation
|
2016-04-07 17:55:36 +02:00
|
|
|
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
|
2023-02-17 15:35:06 +01:00
|
|
|
user.subscription.update_columns( # rubocop:disable Rails/SkipsModelValidations
|
2022-07-13 16:28:43 +02:00
|
|
|
expiration_date: reservation.slots_reservations.first.slot.start_at + user.subscribed_plan.duration
|
2016-04-07 17:55:36 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|