mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-10 21:24:20 +01:00
b68e47a0ea
Previsouly, the reservation was expecting parameters like: slots_attributes: [{start_at: date, end_at: date, availability_id: number}] Now, the reservation is expecting simpler parameters like: slots_reservations_attributes:[{slot_id: number}]
33 lines
946 B
Ruby
33 lines
946 B
Ruby
# frozen_string_literal: true
|
|
|
|
# Extend the user's current subscription after his first training reservation if
|
|
# he subscribed to a rolling plan
|
|
class SubscriptionExtensionAfterReservation
|
|
attr_accessor :user, :reservation
|
|
|
|
def initialize(reservation)
|
|
@user = reservation.user
|
|
@reservation = reservation
|
|
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
|
|
return false if user.subscription.expired?
|
|
return false unless user.subscribed_plan.is_rolling
|
|
|
|
true
|
|
end
|
|
|
|
def extend_subscription
|
|
user.subscription.update_columns(
|
|
expiration_date: reservation.slots_reservations.first.slot.start_at + user.subscribed_plan.duration
|
|
)
|
|
end
|
|
end
|