From 765ce68c119c8cd6e0432d6a609b677ee21391e0 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sat, 21 Jun 2025 16:15:31 +0500 Subject: [PATCH] Add order_id to order controller, variant autocomplete, and search parameters for improved order management --- .../controllers/order_controller.js.coffee | 1 + .../directives/variant_autocomplete.js.coffee | 1 + .../spree/admin/variants_controller.rb | 2 +- app/views/spree/admin/orders/edit.html.haml | 1 + .../scope_variants_for_search.rb | 13 +++++++++++-- .../scope_variants_for_search_spec.rb | 17 +++++++++++++---- 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/admin/orders/controllers/order_controller.js.coffee b/app/assets/javascripts/admin/orders/controllers/order_controller.js.coffee index e99cbf3bbd..2624f27ae2 100644 --- a/app/assets/javascripts/admin/orders/controllers/order_controller.js.coffee +++ b/app/assets/javascripts/admin/orders/controllers/order_controller.js.coffee @@ -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) diff --git a/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee b/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee index efc3b0042f..ea95e0c344 100644 --- a/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee +++ b/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee @@ -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 diff --git a/app/controllers/spree/admin/variants_controller.rb b/app/controllers/spree/admin/variants_controller.rb index d78cb60427..7ec309ea93 100644 --- a/app/controllers/spree/admin/variants_controller.rb +++ b/app/controllers/spree/admin/variants_controller.rb @@ -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 diff --git a/app/views/spree/admin/orders/edit.html.haml b/app/views/spree/admin/orders/edit.html.haml index 217eb6027c..a0e0d249c0 100644 --- a/app/views/spree/admin/orders/edit.html.haml +++ b/app/views/spree/admin/orders/edit.html.haml @@ -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) diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index 875b68ba5a..d8e96b2a69 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -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 diff --git a/spec/lib/open_food_network/scope_variants_for_search_spec.rb b/spec/lib/open_food_network/scope_variants_for_search_spec.rb index ccbed15dd0..bbed2af712 100644 --- a/spec/lib/open_food_network/scope_variants_for_search_spec.rb +++ b/spec/lib/open_food_network/scope_variants_for_search_spec.rb @@ -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