mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Update CompleteOrdersWithBalanceQuery
This commit is contained in:
@@ -79,7 +79,7 @@ module Spree
|
||||
private
|
||||
|
||||
def orders_collection
|
||||
CompleteOrdersWithBalance.new(@user).query
|
||||
CompleteOrdersWithBalanceQuery.new(@user).call
|
||||
end
|
||||
|
||||
def load_object
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Fetches complete orders of the specified user including their balance as a computed column
|
||||
class CompleteOrdersWithBalance
|
||||
class CompleteOrdersWithBalanceQuery
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def query
|
||||
def call
|
||||
OutstandingBalance.new(sorted_finalized_orders).query
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
# Alternatively, you can get the SQL by calling #statement, which is suitable for more complex
|
||||
# cases.
|
||||
#
|
||||
# See CompleteOrdersWithBalance or CustomersWithBalance as examples.
|
||||
# See CompleteOrdersWithBalanceQuery or CustomersWithBalance as examples.
|
||||
#
|
||||
# Note this query object and `app/models/concerns/balance.rb` should implement the same behavior
|
||||
# until we find a better way. If you change one, please, change the other too.
|
||||
|
||||
@@ -9,7 +9,7 @@ module Api
|
||||
|
||||
has_many :payments, serializer: Api::PaymentSerializer
|
||||
|
||||
# This method relies on `balance_value` as a computed DB column. See `CompleteOrdersWithBalance`
|
||||
# This method relies on `balance_value` as a computed DB column. See `CompleteOrdersWithBalanceQuery`
|
||||
# for reference.
|
||||
def outstanding_balance
|
||||
-object.balance_value
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe CompleteOrdersWithBalance do
|
||||
let(:complete_orders_with_balance) { described_class.new(user) }
|
||||
describe CompleteOrdersWithBalanceQuery do
|
||||
let(:result) { described_class.new(user).call }
|
||||
|
||||
describe '#query' do
|
||||
describe '#call' do
|
||||
let(:user) { order.user }
|
||||
let(:outstanding_balance) { instance_double(OutstandingBalance) }
|
||||
|
||||
@@ -28,17 +28,16 @@ describe CompleteOrdersWithBalance do
|
||||
allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance)
|
||||
expect(outstanding_balance).to receive(:query)
|
||||
|
||||
complete_orders_with_balance.query
|
||||
result
|
||||
end
|
||||
|
||||
it 'returns complete orders including their balance' do
|
||||
order = complete_orders_with_balance.query.first
|
||||
order = result.first
|
||||
expect(order[:balance_value]).to eq(-1.0)
|
||||
end
|
||||
|
||||
it 'sorts them by their completed_at with the most recent first' do
|
||||
orders = complete_orders_with_balance.query
|
||||
expect(orders.pluck(:id)).to eq([other_order.id, order.id])
|
||||
expect(result.pluck(:id)).to eq([other_order.id, order.id])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -49,12 +48,11 @@ describe CompleteOrdersWithBalance do
|
||||
allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance)
|
||||
expect(outstanding_balance).to receive(:query)
|
||||
|
||||
complete_orders_with_balance.query
|
||||
result
|
||||
end
|
||||
|
||||
it 'returns an empty array' do
|
||||
order = complete_orders_with_balance.query
|
||||
expect(order).to be_empty
|
||||
expect(result).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user