mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-05 02:41:33 +00:00
check stock before doing anything related to admin payments
This commit is contained in:
@@ -9,6 +9,7 @@ module Spree
|
||||
before_action :load_payment, only: [:fire, :show]
|
||||
before_action :load_data
|
||||
before_action :can_transition_to_payment
|
||||
before_action :ensure_sufficient_stock_lines
|
||||
|
||||
respond_to :html
|
||||
|
||||
@@ -142,6 +143,17 @@ module Spree
|
||||
redirect_to spree.edit_admin_order_customer_url(@order)
|
||||
end
|
||||
|
||||
def ensure_sufficient_stock_lines
|
||||
return if @order.insufficient_stock_lines.blank? || !@order.payment?
|
||||
|
||||
out_of_stock_items = @order.insufficient_stock_lines.map do |line_item|
|
||||
line_item.variant.name
|
||||
end.join(", ")
|
||||
flash[:error] = I18n.t("spree.orders.line_item.insufficient_stock",
|
||||
on_hand: "0 #{out_of_stock_items}")
|
||||
redirect_to spree.edit_admin_order_url(@order)
|
||||
end
|
||||
|
||||
def load_order
|
||||
@order = Order.find_by!(number: params[:order_id])
|
||||
authorize! action, @order
|
||||
|
||||
@@ -309,5 +309,19 @@ describe Spree::Admin::PaymentsController, type: :controller do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "the order contains an item that is out of stock" do
|
||||
let!(:order) { create(:order, distributor: shop, state: 'payment') }
|
||||
|
||||
before do
|
||||
order.line_items.first.variant.update_attribute(:on_hand, 0)
|
||||
end
|
||||
|
||||
it "redirects to the order details page" do
|
||||
spree_get :index, order_id: order.number
|
||||
expect(response.status).to eq 302
|
||||
expect(response.location).to eq spree.edit_admin_order_url(order)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user