Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow producer to edit their products on hubs' orders #13113

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/services/search_orders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
end

def search_query
base_query = ::Permissions::Order.new(current_user).editable_orders.not_empty
.or(::Permissions::Order.new(current_user).editable_orders.finalized)
base_query = ::Permissions::Order.new(current_user).editable_orders.not_empty.or(::Permissions::Order.new(current_user).editable_orders.finalized)

Check warning on line 31 in app/services/search_orders.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [150/100] Raw Output: app/services/search_orders.rb:31:101: C: Layout/LineLength: Line is too long. [150/100]

return base_query if params[:shipping_method_id].blank?

Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/orders/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

- content_for :minimal_js, true

- if can?(:create, Spree::Order)
- if can?(:create, Spree::Order) && spree_current_user.can_manage_orders?
- content_for :page_actions do
%li
= button_link_to t('.new_order'), spree.new_admin_order_url, icon: 'icon-plus', id: 'admin_new_order'
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/open_food_network/scope_variants_for_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@
to eq(["Product 1", "Product a", "Product b", "Product c"])
end
end

context "when search is done by the producer allowing to edit orders" do
let(:params) { { q: "product" } }
let(:producer) { create(:supplier_enterprise) }
let(:spree_current_user) { instance_double('Spree::User', enterprises: Enterprise.where(id: producer.id), can_manage_line_items_in_orders_only?: true) }

Check warning on line 197 in spec/lib/open_food_network/scope_variants_for_search_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [158/100] Raw Output: spec/lib/open_food_network/scope_variants_for_search_spec.rb:197:101: C: Layout/LineLength: Line is too long. [158/100]

it "returns all products distributed through distributors allowing producers to edit orders" do

Check warning on line 199 in spec/lib/open_food_network/scope_variants_for_search_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [101/100] Raw Output: spec/lib/open_food_network/scope_variants_for_search_spec.rb:199:101: C: Layout/LineLength: Line is too long. [101/100]
v1.supplier_id = producer.id
v2.supplier_id = producer.id
v1.save!
v2.save!

expect(result).to include v1, v2
expect(result).not_to include v3, v4
end
end
end

private
Expand Down
23 changes: 23 additions & 0 deletions spec/models/enterprise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,29 @@ def should_have_enterprise_relationship(opts = {})
expect(expected).to include(sender)
end
end

describe "#is_producer" do
context "when enterprise is_primary_producer and sells none" do
it "returns true" do
enterprise = build(:supplier_enterprise)
expect(enterprise.is_producer).to be true
end
end

context "when enterprise is_primary_producer and sells any" do
it "returns false" do
enterprise = build(:enterprise, is_primary_producer: true, sells: "any")
expect(enterprise.is_producer).to be false
end
end

context "when enterprise is_primary_producer and sells own" do
it "returns false" do
enterprise = build(:enterprise, is_primary_producer: true, sells: "own")
expect(enterprise.is_producer).to be false
end
end
end
end

def enterprise_name_error(owner_email)
Expand Down
35 changes: 35 additions & 0 deletions spec/models/spree/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,39 @@
end
end
end

describe "#can_manage_line_items_in_orders_only?" do
let(:producer) { create(:supplier_enterprise) }
let(:order) { create(:order, distributor: distributor) }

Check warning on line 304 in spec/models/spree/user_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Omit the hash value. Raw Output: spec/models/spree/user_spec.rb:304:47: C: Style/HashSyntax: Omit the hash value.

subject { user.can_manage_line_items_in_orders_only? }

context "when user has producer" do
let(:user) { create(:user, enterprises: [producer]) }

context "order containing their product" do
before do
order.line_items << create(:line_item, product: create(:product, supplier_id: producer.id))

Check warning on line 313 in spec/models/spree/user_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [101/100] Raw Output: spec/models/spree/user_spec.rb:313:101: C: Layout/LineLength: Line is too long. [101/100]
end
context "order distributor allow producer to edit orders" do
let(:distributor) do
create(:distributor_enterprise, enable_producers_to_edit_orders: true)
end

it { is_expected.to be_truthy }
end

context "order distributor doesn't allow producer to edit orders" do
let(:distributor) { create(:distributor_enterprise) }
it { is_expected.to be_falsey }
end
end
end

context "no order containing their product" do
let(:user) { create(:user, enterprises: [create(:distributor_enterprise)]) }

it { is_expected.to be_falsey }
end
end
end
Loading
Loading