2022-08-19 19:59:13 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-30 15:29:52 +01:00
|
|
|
# API Controller to manage user's cart
|
2022-08-19 19:59:13 +02:00
|
|
|
class API::CartController < API::ApiController
|
2022-08-21 19:08:10 +02:00
|
|
|
include API::OrderConcern
|
|
|
|
|
2022-08-20 20:49:51 +02:00
|
|
|
before_action :current_order, except: %i[create]
|
2022-08-19 19:59:13 +02:00
|
|
|
before_action :ensure_order, except: %i[create]
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize :cart, :create?
|
2022-09-05 15:24:08 +02:00
|
|
|
@order ||= Cart::FindOrCreateService.new(current_user).call(order_token)
|
2022-08-19 19:59:13 +02:00
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
2022-12-30 15:29:52 +01:00
|
|
|
def create_item
|
|
|
|
authorize @current_order, policy_class: CartPolicy
|
|
|
|
service = Cart::CreateCartItemService.new(@current_order)
|
|
|
|
@item = service.create(params)
|
|
|
|
if @item.save({ context: @current_order.order_items })
|
|
|
|
render 'api/orders/item', status: :created
|
|
|
|
else
|
|
|
|
render json: @item.errors.full_messages, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-19 19:59:13 +02:00
|
|
|
def add_item
|
|
|
|
authorize @current_order, policy_class: CartPolicy
|
|
|
|
@order = Cart::AddItemService.new.call(@current_order, orderable, cart_params[:quantity])
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_item
|
2022-08-20 20:49:51 +02:00
|
|
|
authorize @current_order, policy_class: CartPolicy
|
2022-08-19 19:59:13 +02:00
|
|
|
@order = Cart::RemoveItemService.new.call(@current_order, orderable)
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_quantity
|
2022-08-20 20:49:51 +02:00
|
|
|
authorize @current_order, policy_class: CartPolicy
|
2022-08-19 19:59:13 +02:00
|
|
|
@order = Cart::SetQuantityService.new.call(@current_order, orderable, cart_params[:quantity])
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
2022-09-08 15:10:56 +02:00
|
|
|
def set_offer
|
2022-11-04 12:13:53 +01:00
|
|
|
authorize CartContext.new(params[:customer_id], cart_params[:is_offered])
|
2022-09-08 15:10:56 +02:00
|
|
|
@order = Cart::SetOfferService.new.call(@current_order, orderable, cart_params[:is_offered])
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
2022-09-27 19:44:39 +02:00
|
|
|
def refresh_item
|
|
|
|
authorize @current_order, policy_class: CartPolicy
|
|
|
|
@order = Cart::RefreshItemService.new.call(@current_order, orderable)
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
authorize @current_order, policy_class: CartPolicy
|
|
|
|
@order_errors = Cart::CheckCartService.new.call(@current_order)
|
|
|
|
render json: @order_errors
|
|
|
|
end
|
|
|
|
|
2022-08-19 19:59:13 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def orderable
|
2022-12-28 17:51:27 +01:00
|
|
|
params[:orderable_type].classify.constantize.find(cart_params[:orderable_id])
|
2022-08-19 19:59:13 +02:00
|
|
|
end
|
|
|
|
end
|