Update the Shop::OrderCyclesList service so it's clearer what kind of order cycles are being fetched

Before the Shop::OrderCyclesList service would return order cycles even if they are not ready for checkout and we had a check before calling the service in BaseController which would return OrderCycle.where('false'). It seems like this check should be part of the service too.
This commit is contained in:
Cillian O'Ruanaidh
2022-06-24 16:11:29 +01:00
committed by Filipe
parent f4a4f7c9ff
commit 747c88fb35
4 changed files with 80 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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