mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-29 10:24:20 +01:00
84 lines
3.0 KiB
Ruby
84 lines
3.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Provides methods for working with cart items
|
|
class CartService
|
|
def initialize(operator)
|
|
@operator = operator
|
|
end
|
|
|
|
def from_hash(cart_items)
|
|
items = []
|
|
plan_info = plan(cart_items)
|
|
|
|
@customer = User.find(cart_items[:customer_id])
|
|
|
|
items.push(CartItem::Subscription.new(plan_info[:plan])) if cart_items[:subscription]
|
|
items.push(reservable_from_hash(cart_items[:reservation], plan_info)) if cart_items[:reservation]
|
|
|
|
coupon = CartItem::Coupon.new(@customer, @operator, cart_items[:coupon_code])
|
|
schedule = CartItem::PaymentSchedule.new(plan_info[:plan], coupon, cart_items[:payment_schedule])
|
|
|
|
ShoppingCart.new(
|
|
@customer,
|
|
coupon,
|
|
cart_items[:payment_method],
|
|
items: items,
|
|
payment_schedule: schedule
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
def plan(cart_items)
|
|
plan = if @customer.subscribed_plan
|
|
new_plan_being_bought = false
|
|
@customer.subscribed_plan
|
|
elsif cart_items[:subscription]
|
|
new_plan_being_bought = true
|
|
Plan.find(cart_items[:subscription][:plan_id])
|
|
else
|
|
new_plan_being_bought = false
|
|
nil
|
|
end
|
|
{ plan: plan, new_subscription: new_plan_being_bought }
|
|
end
|
|
|
|
def reservable_from_hash(cart_item, plan_info)
|
|
return nil if cart_item[:reservable_id].blank?
|
|
|
|
reservable = cart_item[:reservable_type].constantize.find(cart_item[:reservable_id])
|
|
case reservable
|
|
when Machine
|
|
CartItem::MachineReservation.new(@customer,
|
|
@operator,
|
|
reservable,
|
|
cart_item[:slots_attributes],
|
|
plan: plan_info[:plan],
|
|
new_subscription: plan_info[:new_subscription])
|
|
when Training
|
|
CartItem::TrainingReservation.new(@customer,
|
|
@operator,
|
|
reservable,
|
|
cart_item[:slots_attributes],
|
|
plan: plan_info[:plan],
|
|
new_subscription: plan_info[:new_subscription])
|
|
when Event
|
|
CartItem::EventReservation.new(@customer,
|
|
@operator,
|
|
reservable,
|
|
cart_item[:slots_attributes],
|
|
normal_tickets: cart_item[:nb_reserve_places],
|
|
other_tickets: cart_item[:tickets_attributes])
|
|
when Space
|
|
CartItem::SpaceReservation.new(@customer,
|
|
@operator,
|
|
reservable,
|
|
cart_item[:slots_attributes],
|
|
plan: plan_info[:plan],
|
|
new_subscription: plan_info[:new_subscription])
|
|
else
|
|
raise NotImplementedError
|
|
end
|
|
end
|
|
end
|