mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-13 04:00:21 +00:00
Add new 'Checkout options' step to the edit order cycle form so people can attach shipping methods
This commit is contained in:
committed by
Filipe
parent
1e817af5aa
commit
855ec1a708
@@ -677,6 +677,24 @@ describe OrderCycle do
|
||||
end
|
||||
end
|
||||
|
||||
describe "#attachable_shipping_methods" do
|
||||
it "includes shipping methods from the distributors on the order cycle" do
|
||||
shipping_method = create(:shipping_method)
|
||||
enterprise = create(:enterprise, shipping_methods: [shipping_method])
|
||||
oc = create(:simple_order_cycle, distributors: [enterprise])
|
||||
|
||||
expect(oc.attachable_shipping_methods).to eq([shipping_method])
|
||||
end
|
||||
|
||||
it "does not include backoffice only shipping methods" do
|
||||
shipping_method = create(:shipping_method, display_on: "back_end")
|
||||
enterprise = create(:enterprise, shipping_methods: [shipping_method])
|
||||
oc = create(:simple_order_cycle, distributors: [enterprise])
|
||||
|
||||
expect(oc.attachable_shipping_methods).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe "#simple?" do
|
||||
it "returns true if the coordinator sells their own products i.e. shops" do
|
||||
order_cycle = build(:simple_order_cycle, coordinator: build(:enterprise, sells: "own"))
|
||||
|
||||
269
spec/services/order_available_shipping_methods_spec.rb
Normal file
269
spec/services/order_available_shipping_methods_spec.rb
Normal file
@@ -0,0 +1,269 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe OrderAvailableShippingMethods do
|
||||
context "when the order has no current_distributor" do
|
||||
it "returns an empty array" do
|
||||
order_cycle = create(:sells_own_order_cycle)
|
||||
order = build(:order, distributor: nil, order_cycle: order_cycle)
|
||||
|
||||
expect(OrderAvailableShippingMethods.new(order).to_a).to eq []
|
||||
end
|
||||
end
|
||||
|
||||
it "does not return 'back office only' shipping method" do
|
||||
distributor = create(:distributor_enterprise)
|
||||
frontend_shipping_method = create(:shipping_method, distributors: [distributor])
|
||||
backoffice_only_shipping_method = create(:shipping_method,
|
||||
distributors: [distributor], display_on: 'back_end')
|
||||
order_cycle = create(:sells_own_order_cycle)
|
||||
order = build(:order, distributor: distributor, order_cycle: order_cycle)
|
||||
|
||||
available_shipping_methods = OrderAvailableShippingMethods.new(order).to_a
|
||||
|
||||
expect(available_shipping_methods).to eq [frontend_shipping_method]
|
||||
end
|
||||
|
||||
context "when no tag rules are in effect" do
|
||||
context "order cycle selling own produce only i.e. shipping methods cannot be customised" do
|
||||
it "returns all shipping methods belonging to the enterprise" do
|
||||
order_cycle = create(:sells_own_order_cycle)
|
||||
enterprise = order_cycle.coordinator
|
||||
shipping_method = create(:shipping_method, distributors: [enterprise])
|
||||
other_enterprise = create(:enterprise)
|
||||
other_enterprise_shipping_method = create(:shipping_method,
|
||||
distributors: [other_enterprise])
|
||||
order = build(:order, distributor: enterprise, order_cycle: order_cycle)
|
||||
|
||||
available_shipping_methods = OrderAvailableShippingMethods.new(order).to_a
|
||||
|
||||
expect(order_cycle.shipping_methods).to be_empty
|
||||
expect(available_shipping_methods).to eq [shipping_method]
|
||||
end
|
||||
end
|
||||
|
||||
context "distributor order cycle" do
|
||||
it "only returns shipping methods which belong to the order distributor
|
||||
and have been added to the order cycle" do
|
||||
distributor = create(:distributor_enterprise)
|
||||
shipping_method_i = create(:shipping_method, distributors: [distributor])
|
||||
shipping_method_ii = create(:shipping_method, distributors: [distributor])
|
||||
order_cycle = create(:simple_order_cycle,
|
||||
distributors: [distributor], shipping_methods: [shipping_method_i])
|
||||
order = build(:order, distributor: distributor, order_cycle: order_cycle)
|
||||
|
||||
available_shipping_methods = OrderAvailableShippingMethods.new(order).to_a
|
||||
|
||||
expect(available_shipping_methods).to eq order_cycle.shipping_methods
|
||||
expect(available_shipping_methods).to eq [shipping_method_i]
|
||||
end
|
||||
|
||||
it "doesn't return shipping methods which have been added to the order cycle
|
||||
when they don't belong to the order distributor" do
|
||||
distributor_i = create(:distributor_enterprise)
|
||||
distributor_ii = create(:distributor_enterprise)
|
||||
shipping_method_i = create(:shipping_method, distributors: [distributor_i])
|
||||
shipping_method_ii = create(:shipping_method, distributors: [distributor_ii])
|
||||
order_cycle = create(:simple_order_cycle,
|
||||
distributors: [distributor_i, distributor_ii],
|
||||
shipping_methods: [shipping_method_i, shipping_method_ii])
|
||||
order = build(:order, distributor: distributor_ii, order_cycle: order_cycle)
|
||||
|
||||
available_shipping_methods = OrderAvailableShippingMethods.new(order).to_a
|
||||
|
||||
expect(available_shipping_methods).not_to eq order_cycle.shipping_methods
|
||||
expect(available_shipping_methods).to eq [shipping_method_ii]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when FilterShippingMethods tag rules are in effect" do
|
||||
let(:user) { create(:user) }
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let(:other_distributor) { create(:distributor_enterprise) }
|
||||
let!(:distributor_shipping_method) { create(:shipping_method, distributors: [distributor]) }
|
||||
let!(:other_distributor_shipping_method) do
|
||||
create(:shipping_method, distributors: [other_distributor])
|
||||
end
|
||||
let(:customer) { create(:customer, user: user, enterprise: distributor) }
|
||||
let!(:tag_rule) {
|
||||
create(:filter_shipping_methods_tag_rule,
|
||||
enterprise: distributor,
|
||||
preferred_customer_tags: "local",
|
||||
preferred_shipping_method_tags: "local-delivery")
|
||||
}
|
||||
let!(:default_tag_rule) {
|
||||
create(:filter_shipping_methods_tag_rule,
|
||||
enterprise: distributor,
|
||||
is_default: true,
|
||||
preferred_shipping_method_tags: "local-delivery")
|
||||
}
|
||||
let!(:tagged_sm) { distributor_shipping_method }
|
||||
let!(:untagged_sm) { other_distributor_shipping_method }
|
||||
|
||||
before do
|
||||
tagged_sm.update_attribute(:tag_list, 'local-delivery')
|
||||
distributor.shipping_methods = [tagged_sm, untagged_sm]
|
||||
end
|
||||
|
||||
context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do
|
||||
before {
|
||||
tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility, 'visible')
|
||||
}
|
||||
before {
|
||||
default_tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility,
|
||||
'hidden')
|
||||
}
|
||||
|
||||
context "order cycle selling own produce only" do
|
||||
let(:order_cycle) { create(:sells_own_order_cycle) }
|
||||
let(:order) { build(:order, distributor: distributor, order_cycle: order_cycle) }
|
||||
|
||||
context "when the customer is nil" do
|
||||
let(:available_shipping_methods) { OrderAvailableShippingMethods.new(order).to_a }
|
||||
|
||||
it "applies default action (hide)" do
|
||||
expect(available_shipping_methods).to include untagged_sm
|
||||
expect(available_shipping_methods).to_not include tagged_sm
|
||||
end
|
||||
end
|
||||
|
||||
context "when a customer is present" do
|
||||
let(:available_shipping_methods) { OrderAvailableShippingMethods.new(order, customer).to_a }
|
||||
|
||||
context "and the customer's tags match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'local')
|
||||
end
|
||||
|
||||
it "applies the action (show)" do
|
||||
expect(available_shipping_methods).to include tagged_sm, untagged_sm
|
||||
end
|
||||
end
|
||||
|
||||
context "and the customer's tags don't match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'something')
|
||||
end
|
||||
|
||||
it "applies the default action (hide)" do
|
||||
expect(available_shipping_methods).to include untagged_sm
|
||||
expect(available_shipping_methods).to_not include tagged_sm
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "distributor order cycle" do
|
||||
context "when the shipping method without the tag rule is attached to the order cycle
|
||||
and the shipping method with the tag rule is not" do
|
||||
let(:order_cycle) do
|
||||
create(:distributor_order_cycle,
|
||||
distributors: [distributor], shipping_methods: [untagged_sm])
|
||||
end
|
||||
let(:order) { build(:order, distributor: distributor, order_cycle: order_cycle) }
|
||||
let(:available_shipping_methods) { OrderAvailableShippingMethods.new(order, customer).to_a }
|
||||
|
||||
context "when the customer's tags match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'local')
|
||||
end
|
||||
|
||||
it "doesn't display the shipping method with the prefered visibility 'visible' tag
|
||||
even though the customer's tags match
|
||||
because it hasn't been attached to the order cycle" do
|
||||
expect(available_shipping_methods).to include untagged_sm
|
||||
expect(available_shipping_methods).to_not include tagged_sm
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do
|
||||
before {
|
||||
tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility, 'hidden')
|
||||
}
|
||||
before {
|
||||
default_tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility,
|
||||
'visible')
|
||||
}
|
||||
|
||||
context "order cycle selling own produce only" do
|
||||
let(:order_cycle) { create(:sells_own_order_cycle) }
|
||||
let(:order) { build(:order, distributor: distributor, order_cycle: order_cycle) }
|
||||
|
||||
context "when the customer is nil" do
|
||||
let(:available_shipping_methods) { OrderAvailableShippingMethods.new(order).to_a }
|
||||
|
||||
it "applies default action (show)" do
|
||||
expect(available_shipping_methods).to include tagged_sm, untagged_sm
|
||||
end
|
||||
end
|
||||
|
||||
context "when a customer is present" do
|
||||
let(:available_shipping_methods) { OrderAvailableShippingMethods.new(order, customer).to_a }
|
||||
|
||||
context "and the customer's tags match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'local')
|
||||
end
|
||||
|
||||
it "applies the action (hide)" do
|
||||
expect(available_shipping_methods).to include untagged_sm
|
||||
expect(available_shipping_methods).to_not include tagged_sm
|
||||
end
|
||||
end
|
||||
|
||||
context "and the customer's tags don't match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'something')
|
||||
end
|
||||
|
||||
it "applies the default action (show)" do
|
||||
expect(available_shipping_methods).to include tagged_sm, untagged_sm
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "distributor order cycle" do
|
||||
context "when the shipping method without the tag rule is attached to the order cycle
|
||||
and the shipping method with the tag rule is not" do
|
||||
let(:order_cycle) do
|
||||
create(:distributor_order_cycle,
|
||||
distributors: [distributor], shipping_methods: [untagged_sm])
|
||||
end
|
||||
let(:order) { build(:order, distributor: distributor, order_cycle: order_cycle) }
|
||||
|
||||
context "when the customer is nil" do
|
||||
let(:available_shipping_methods) { OrderAvailableShippingMethods.new(order).to_a }
|
||||
|
||||
it "doesn't display the shipping method tagged to be visible by default
|
||||
because it is not attached to the order cycle" do
|
||||
expect(available_shipping_methods).to include untagged_sm
|
||||
expect(available_shipping_methods).to_not include tagged_sm
|
||||
end
|
||||
end
|
||||
|
||||
context "when a customer is present" do
|
||||
let(:available_shipping_methods) { OrderAvailableShippingMethods.new(order, customer).to_a }
|
||||
|
||||
context "when the customer's tags don't match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'something')
|
||||
end
|
||||
|
||||
it "doesn't display the shipping method tagged to be visible by default
|
||||
because it is not attached to the order cycle" do
|
||||
expect(available_shipping_methods).to include untagged_sm
|
||||
expect(available_shipping_methods).to_not include tagged_sm
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -22,6 +22,8 @@ describe '
|
||||
v2 = create(:variant, product: product)
|
||||
distributor = create(:distributor_enterprise, name: 'My distributor',
|
||||
with_payment_and_shipping: true)
|
||||
shipping_method_i = distributor.shipping_methods.first
|
||||
shipping_method_ii = create(:shipping_method, distributors: [distributor])
|
||||
|
||||
# Relationships required for interface to work
|
||||
create(:enterprise_relationship, parent: supplier, child: coordinator,
|
||||
@@ -129,6 +131,12 @@ describe '
|
||||
select 'Distributor fee',
|
||||
from: 'order_cycle_outgoing_exchange_0_enterprise_fees_0_enterprise_fee_id'
|
||||
|
||||
click_button 'Save and Next'
|
||||
|
||||
# And I select preferred shipping methods
|
||||
check "order_cycle_preferred_shipping_method_ids_#{shipping_method_i.id}"
|
||||
uncheck "order_cycle_preferred_shipping_method_ids_#{shipping_method_ii.id}"
|
||||
|
||||
click_button 'Save and Back to List'
|
||||
|
||||
oc = OrderCycle.last
|
||||
@@ -161,5 +169,8 @@ describe '
|
||||
expect(exchange.pickup_time).to eq('pickup time')
|
||||
expect(exchange.pickup_instructions).to eq('pickup instructions')
|
||||
expect(exchange.tag_list).to eq(['wholesale'])
|
||||
|
||||
# And the shipping method should be attached
|
||||
expect(oc.shipping_methods).to eq([shipping_method_i])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user