mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Add order_id to order controller, variant autocomplete, and search parameters for improved order management
This commit is contained in:
@@ -6,6 +6,7 @@ angular.module("admin.orders").controller "orderCtrl", ($scope, shops, orderCycl
|
||||
$scope.distributor_id = parseInt($attrs.ofnDistributorId)
|
||||
$scope.order_cycle_id = parseInt($attrs.ofnOrderCycleId)
|
||||
$scope.search_variants_as = $attrs.ofnSearchVariantsAs
|
||||
$scope.order_id = $attrs.ofnOrderId
|
||||
|
||||
$scope.validOrderCycle = (oc) ->
|
||||
$scope.orderCycleHasDistributor oc, parseInt($scope.distributor_id)
|
||||
|
||||
@@ -27,6 +27,7 @@ angular.module("admin.utils").directive "variantAutocomplete", ($timeout) ->
|
||||
eligible_for_subscriptions: scope.eligible_for_subscriptions
|
||||
include_out_of_stock: scope.include_out_of_stock
|
||||
search_variants_as: scope.search_variants_as
|
||||
order_id: scope.order_id
|
||||
results: (data, page) ->
|
||||
window.variants = data # this is how spree auto complete JS code picks up variants
|
||||
results: data
|
||||
|
||||
@@ -117,7 +117,7 @@ module Spree
|
||||
def variant_search_params
|
||||
params.permit(
|
||||
:q, :distributor_id, :order_cycle_id, :schedule_id, :eligible_for_subscriptions,
|
||||
:include_out_of_stock, :search_variants_as,
|
||||
:include_out_of_stock, :search_variants_as, :order_id
|
||||
).to_h.with_indifferent_access
|
||||
end
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
"ofn-distributor-id" => @order.distributor_id,
|
||||
"ofn-order-cycle-id" => @order.order_cycle_id,
|
||||
"ofn-search-variants-as" => (can?(:manage_order_sections, @order) ? 'hub' : 'supplier'),
|
||||
"ofn-order-id" => @order.id,
|
||||
}
|
||||
|
||||
= render :partial => 'add_product' if can?(:update, @order)
|
||||
|
||||
@@ -12,6 +12,7 @@ module OpenFoodNetwork
|
||||
def initialize(params, spree_current_user)
|
||||
@params = params
|
||||
@spree_current_user = spree_current_user
|
||||
@ability = Spree::Ability.new(spree_current_user)
|
||||
end
|
||||
|
||||
def search
|
||||
@@ -21,14 +22,14 @@ module OpenFoodNetwork
|
||||
scope_to_schedule if params[:schedule_id]
|
||||
scope_to_order_cycle if params[:order_cycle_id]
|
||||
scope_to_distributor if params[:distributor_id]
|
||||
scope_to_supplier if params[:search_variants_as] == 'supplier'
|
||||
scope_to_supplier if scope_to_supplier?
|
||||
|
||||
@variants
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :params, :spree_current_user
|
||||
attr_reader :params, :spree_current_user, :ability
|
||||
|
||||
def search_params
|
||||
{ product_name_cont: params[:q], sku_cont: params[:q], product_sku_cont: params[:q] }
|
||||
@@ -47,6 +48,10 @@ module OpenFoodNetwork
|
||||
@distributor ||= Enterprise.find params[:distributor_id]
|
||||
end
|
||||
|
||||
def order
|
||||
@order ||= Spree::Order.find(params[:order_id])
|
||||
end
|
||||
|
||||
def scope_to_schedule
|
||||
@variants = @variants.in_schedule(params[:schedule_id])
|
||||
end
|
||||
@@ -102,5 +107,9 @@ module OpenFoodNetwork
|
||||
def scope_to_supplier
|
||||
@variants = @variants.where(supplier_id: spree_current_user.enterprises.ids)
|
||||
end
|
||||
|
||||
def scope_to_supplier?
|
||||
params[:search_variants_as] == 'supplier' && ability.can?(:edit_as_producer_only, order)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -67,9 +67,12 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do
|
||||
|
||||
it "returns all products distributed through that distributor" do
|
||||
expect{ result }.to query_database [
|
||||
"Enterprise Load",
|
||||
"EnterpriseGroup Load",
|
||||
"OrderCycle Exists?",
|
||||
"Enterprise Load",
|
||||
"VariantOverride Load",
|
||||
"SQL",
|
||||
"SQL"
|
||||
]
|
||||
|
||||
expect(result).to include v4
|
||||
@@ -183,13 +186,19 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do
|
||||
end
|
||||
|
||||
context "when search is done by the producer allowing to edit orders" do
|
||||
let(:params) { { q: "product", search_variants_as: 'supplier' } }
|
||||
let(:order) { create(:order) }
|
||||
let(:params) { { q: "product", search_variants_as: 'supplier', order_id: order.id } }
|
||||
let(:producer) { create(:supplier_enterprise) }
|
||||
let(:ability) { instance_double('Spree::Ability', can?: true) }
|
||||
let!(:spree_current_user) {
|
||||
instance_double('Spree::User', enterprises: Enterprise.where(id: producer.id),
|
||||
can_manage_line_items_in_orders_only?: true)
|
||||
instance_double('Spree::User', enterprises: Enterprise.where(id: producer.id))
|
||||
}
|
||||
|
||||
before do
|
||||
allow(Spree::Ability).to receive(:new).with(spree_current_user).and_return(ability)
|
||||
allow(ability).to receive(:can?).with(:edit_as_producer_only, order).and_return(true)
|
||||
end
|
||||
|
||||
it "returns products distributed by distributors allowing producers to edit orders" do
|
||||
v1.supplier_id = producer.id
|
||||
v2.supplier_id = producer.id
|
||||
|
||||
Reference in New Issue
Block a user