mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Whatever fee adjustments there are on other line items should be left alone (not recreated), and whatever fee adjustments are already on the order should just be updated.
51 lines
1.2 KiB
Ruby
51 lines
1.2 KiB
Ruby
class LineItemsController < BaseController
|
|
respond_to :json
|
|
|
|
before_action :load_line_item, only: :destroy
|
|
|
|
def bought
|
|
respond_with bought_items, each_serializer: Api::LineItemSerializer
|
|
end
|
|
|
|
def destroy
|
|
authorize! :destroy, @line_item
|
|
destroy_with_lock @line_item
|
|
respond_with(@line_item)
|
|
end
|
|
|
|
private
|
|
|
|
def load_line_item
|
|
@line_item = Spree::LineItem.find_by(id: params[:id])
|
|
not_found unless @line_item
|
|
end
|
|
|
|
# List all items the user already ordered in the current order cycle
|
|
def bought_items
|
|
return [] unless current_order_cycle && spree_current_user && current_distributor
|
|
|
|
current_order_cycle.items_bought_by_user(spree_current_user, current_distributor)
|
|
end
|
|
|
|
def unauthorized
|
|
status = spree_current_user ? 403 : 401
|
|
render(nothing: true, status: status) && return
|
|
end
|
|
|
|
def not_found
|
|
render(nothing: true, status: :not_found) && return
|
|
end
|
|
|
|
def destroy_with_lock(item)
|
|
order = item.order
|
|
order.with_lock do
|
|
item.destroy
|
|
order.update_shipping_fees!
|
|
order.update_payment_fees!
|
|
order.update_order_fees!
|
|
order.update!
|
|
order.create_tax_charge!
|
|
end
|
|
end
|
|
end
|