mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Replace double negation with proper list of states
We rely now on the exhaustive list of states an order can be in after checkout. What made this all a bit more messy is that I made up the "checkout" order state, likely mixing it from the payment model states. This simplifies things quite a bit and gives meaningful names to things.
This commit is contained in:
@@ -130,13 +130,10 @@ module Spree
|
||||
where("state != ?", state)
|
||||
}
|
||||
|
||||
# All the states an order can be in before completing the checkout
|
||||
PRIOR_TO_COMPLETION_STATES = %w(cart address delivery payment).freeze
|
||||
# All the states of a complete order but that shouldn't count towards the balance. Those that
|
||||
# the customer won't enjoy.
|
||||
NON_FULFILLED_STATES = %w(canceled returned).freeze
|
||||
# All the states an order can be in after completing the checkout
|
||||
FINALIZED_STATES = %w(complete canceled resumed awaiting_return returned).freeze
|
||||
|
||||
scope :in_incomplete_state, -> { where(state: PRIOR_TO_COMPLETION_STATES) }
|
||||
scope :finalized, -> { where(state: FINALIZED_STATES) }
|
||||
|
||||
def self.by_number(number)
|
||||
where(number: number)
|
||||
|
||||
@@ -7,14 +7,14 @@ class CompleteOrdersWithBalance
|
||||
end
|
||||
|
||||
def query
|
||||
OutstandingBalance.new(sorted_complete_orders).query
|
||||
OutstandingBalance.new(sorted_finalized_orders).query
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sorted_complete_orders
|
||||
def sorted_finalized_orders
|
||||
@user.orders
|
||||
.where.not(Spree::Order.in_incomplete_state.where_values_hash)
|
||||
.finalized
|
||||
.select('spree_orders.*')
|
||||
.order(completed_at: :desc)
|
||||
end
|
||||
|
||||
@@ -28,13 +28,12 @@ class CustomersWithBalance
|
||||
def left_join_complete_orders
|
||||
<<-SQL.strip_heredoc
|
||||
LEFT JOIN spree_orders ON spree_orders.customer_id = customers.id
|
||||
AND #{complete_orders.to_sql}
|
||||
AND #{finalized_states.to_sql}
|
||||
SQL
|
||||
end
|
||||
|
||||
def complete_orders
|
||||
states = Spree::Order::PRIOR_TO_COMPLETION_STATES
|
||||
.map { |state| Arel::Nodes.build_quoted(state) }
|
||||
Arel::Nodes::NotIn.new(Spree::Order.arel_table[:state], states)
|
||||
def finalized_states
|
||||
states = Spree::Order::FINALIZED_STATES.map { |state| Arel::Nodes.build_quoted(state) }
|
||||
Arel::Nodes::In.new(Spree::Order.arel_table[:state], states)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,6 +9,10 @@
|
||||
#
|
||||
# See CompleteOrdersWithBalance or CustomersWithBalance as examples.
|
||||
class OutstandingBalance
|
||||
# All the states of a finished order but that shouldn't count towards the balance (the customer
|
||||
# didn't get the order for whatever reason). Note it does not include complete
|
||||
FINALIZED_NON_SUCCESSFUL_STATES = %w(canceled returned).freeze
|
||||
|
||||
# The relation must be an ActiveRecord::Relation object with `spree_orders` in the SQL statement
|
||||
# FROM for #statement to work.
|
||||
def initialize(relation = nil)
|
||||
@@ -34,7 +38,7 @@ class OutstandingBalance
|
||||
attr_reader :relation
|
||||
|
||||
def non_fulfilled_states_group
|
||||
states = Spree::Order::NON_FULFILLED_STATES.map { |state| Arel::Nodes.build_quoted(state) }
|
||||
states = FINALIZED_NON_SUCCESSFUL_STATES.map { |state| Arel::Nodes.build_quoted(state) }
|
||||
Arel::Nodes::Grouping.new(states)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -69,9 +69,9 @@ describe CustomersWithBalance do
|
||||
context 'when no orders where paid' do
|
||||
before do
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
@@ -85,9 +85,9 @@ describe CustomersWithBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
@@ -102,7 +102,7 @@ describe CustomersWithBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
create(
|
||||
:order,
|
||||
customer: customer,
|
||||
@@ -123,7 +123,7 @@ describe CustomersWithBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'resumed')
|
||||
end
|
||||
@@ -139,7 +139,7 @@ describe CustomersWithBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'payment')
|
||||
end
|
||||
@@ -155,7 +155,7 @@ describe CustomersWithBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'awaiting_return')
|
||||
end
|
||||
@@ -172,7 +172,7 @@ describe CustomersWithBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, customer: customer, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'returned')
|
||||
end
|
||||
|
||||
@@ -79,9 +79,9 @@ describe OutstandingBalance do
|
||||
context 'when no orders where paid' do
|
||||
before do
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
@@ -95,9 +95,9 @@ describe OutstandingBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
@@ -113,7 +113,7 @@ describe OutstandingBalance do
|
||||
before do
|
||||
create(:order, total: order_total, payment_total: order_total, state: 'canceled')
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
@@ -127,7 +127,7 @@ describe OutstandingBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'resumed')
|
||||
end
|
||||
@@ -143,7 +143,7 @@ describe OutstandingBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'payment')
|
||||
end
|
||||
@@ -159,7 +159,7 @@ describe OutstandingBalance do
|
||||
|
||||
before do
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
order = create(:order, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'awaiting_return')
|
||||
end
|
||||
@@ -178,7 +178,7 @@ describe OutstandingBalance do
|
||||
order = create(:order, total: order_total, payment_total: payment_total)
|
||||
order.update_attribute(:state, 'returned')
|
||||
order = create(:order, total: order_total, payment_total: 0)
|
||||
order.update_attribute(:state, 'checkout')
|
||||
order.update_attribute(:state, 'complete')
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
|
||||
Reference in New Issue
Block a user