diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index a04725416a..952c66afee 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -16,12 +16,7 @@ class BaseController < ApplicationController private def set_order_cycles - if !@distributor.ready_for_checkout? - @order_cycles = OrderCycle.where('false') - return - end - - @order_cycles = Shop::OrderCyclesList.new(@distributor, current_customer).call + @order_cycles = Shop::OrderCyclesList.ready_for_checkout_for(@distributor, current_customer) set_order_cycle end diff --git a/app/services/order_cart_reset.rb b/app/services/order_cart_reset.rb index 4faec18a30..9401d60c4c 100644 --- a/app/services/order_cart_reset.rb +++ b/app/services/order_cart_reset.rb @@ -34,7 +34,7 @@ class OrderCartReset end def reset_order_cycle(current_customer) - listed_order_cycles = Shop::OrderCyclesList.new(distributor, current_customer).call + listed_order_cycles = Shop::OrderCyclesList.active_for(distributor, current_customer) if order_cycle_not_listed?(order.order_cycle, listed_order_cycles) order.order_cycle = nil diff --git a/app/services/shop/order_cycles_list.rb b/app/services/shop/order_cycles_list.rb index 1afed9acd8..3a453aa7ac 100644 --- a/app/services/shop/order_cycles_list.rb +++ b/app/services/shop/order_cycles_list.rb @@ -3,12 +3,22 @@ # Lists available order cycles for a given customer in a given distributor module Shop class OrderCyclesList + def self.active_for(distributor, customer) + new(distributor, customer).call(ready_for_checkout: false) + end + + def self.ready_for_checkout_for(distributor, customer) + new(distributor, customer).call(ready_for_checkout: true) + end + def initialize(distributor, customer) @distributor = distributor @customer = customer end - def call + def call(ready_for_checkout:) + return OrderCycle.none if ready_for_checkout && !@distributor.ready_for_checkout? + order_cycles = OrderCycle.with_distributor(@distributor).active .order(@distributor.preferred_shopfront_order_cycle_order).to_a diff --git a/spec/services/shop/order_cycles_list_spec.rb b/spec/services/shop/order_cycles_list_spec.rb new file mode 100644 index 0000000000..3b0e5fe045 --- /dev/null +++ b/spec/services/shop/order_cycles_list_spec.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Shop::OrderCyclesList do + describe ".active_for" do + let(:customer) { nil } + + context "when the order cycle is open and the distributor belongs to the order cycle" do + context "and the distributor is ready for checkout" do + let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } + + it "returns the order cycle" do + open_order_cycle = create(:open_order_cycle, distributors: [distributor]) + + expect(Shop::OrderCyclesList.active_for(distributor, customer)).to eq [open_order_cycle] + end + end + + context "and the distributor is not ready for checkout" do + let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: false) } + + it "returns the order cycle" do + open_order_cycle = create(:open_order_cycle, distributors: [distributor]) + + expect(Shop::OrderCyclesList.active_for(distributor, customer)).to eq [open_order_cycle] + end + end + end + + it "doesn't returns closed order cycles or ones belonging to other distributors" do + distributor = create(:distributor_enterprise) + closed_order_cycle = create(:closed_order_cycle, distributors: [distributor]) + other_distributor_order_cycle = create(:open_order_cycle) + + expect(Shop::OrderCyclesList.active_for(distributor, customer)).to be_empty + end + end + + describe ".ready_for_checkout_for" do + let(:customer) { nil } + + context "when the order cycle is open and belongs to the distributor" do + context "and the distributor is ready for checkout" do + let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } + + it "returns the order cycle" do + open_order_cycle = create(:open_order_cycle, distributors: [distributor]) + + expect(Shop::OrderCyclesList.ready_for_checkout_for(distributor, customer)).to eq [ + open_order_cycle + ] + end + end + + context "but the distributor not is ready for checkout" do + let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: false) } + + it "doesn't return the order cycle" do + open_order_cycle = create(:open_order_cycle, distributors: [distributor]) + + expect(Shop::OrderCyclesList.ready_for_checkout_for(distributor, customer)).to be_empty + end + end + end + end +end