2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for working with cart items
|
|
|
|
class CartService
|
|
|
|
def initialize(operator)
|
|
|
|
@operator = operator
|
|
|
|
end
|
|
|
|
|
2021-04-23 12:52:06 +02:00
|
|
|
##
|
|
|
|
# For details about the expected hash format
|
2021-05-21 18:25:18 +02:00
|
|
|
# @see app/frontend/src/javascript/models/payment.ts > interface ShoppingCart
|
2021-04-23 12:52:06 +02:00
|
|
|
##
|
2021-04-22 19:24:08 +02:00
|
|
|
def from_hash(cart_items)
|
2021-04-23 17:54:59 +02:00
|
|
|
@customer = customer(cart_items)
|
2021-04-22 19:24:08 +02:00
|
|
|
plan_info = plan(cart_items)
|
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
items = []
|
2021-05-19 18:12:52 +02:00
|
|
|
cart_items[:items].each do |item|
|
|
|
|
if item.keys.first == 'subscription'
|
2021-05-21 18:25:18 +02:00
|
|
|
items.push(CartItem::Subscription.new(plan_info[:plan], @customer)) if plan_info[:new_subscription]
|
2021-05-19 18:12:52 +02:00
|
|
|
elsif item.keys.first == 'reservation'
|
|
|
|
items.push(reservable_from_hash(item[:reservation], plan_info))
|
|
|
|
end
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
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,
|
2021-05-21 18:25:18 +02:00
|
|
|
@operator,
|
2021-04-22 19:24:08 +02:00
|
|
|
coupon,
|
2021-04-23 12:52:06 +02:00
|
|
|
schedule,
|
2021-04-22 19:24:08 +02:00
|
|
|
cart_items[:payment_method],
|
2021-04-23 12:52:06 +02:00
|
|
|
items: items
|
2021-04-22 19:24:08 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def plan(cart_items)
|
2021-05-19 18:12:52 +02:00
|
|
|
new_plan_being_bought = false
|
|
|
|
plan = if cart_items[:items].any? { |item| item.keys.first == 'subscription' }
|
|
|
|
index = cart_items[:items].index { |item| item.keys.first == 'subscription' }
|
|
|
|
if cart_items[:items][index][:subscription][:plan_id]
|
|
|
|
new_plan_being_bought = true
|
|
|
|
Plan.find(cart_items[:items][index][:subscription][:plan_id])
|
|
|
|
end
|
2021-04-26 11:40:26 +02:00
|
|
|
elsif @customer.subscribed_plan
|
|
|
|
@customer.subscribed_plan
|
2021-04-22 19:24:08 +02:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
{ plan: plan, new_subscription: new_plan_being_bought }
|
|
|
|
end
|
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
def customer(cart_items)
|
|
|
|
if @operator.admin? || (@operator.manager? && @operator.id != cart_items[:customer_id])
|
|
|
|
User.find(cart_items[:customer_id])
|
|
|
|
else
|
|
|
|
@operator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
def reservable_from_hash(cart_item, plan_info)
|
2021-04-28 16:22:22 +02:00
|
|
|
reservable = cart_item[:reservable_type]&.constantize&.find(cart_item[:reservable_id])
|
2021-04-22 19:24:08 +02:00
|
|
|
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
|
2021-04-28 16:22:22 +02:00
|
|
|
STDERR.puts "WARNING: the reservable #{reservable} is not implemented"
|
2021-04-22 19:24:08 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|