Update OutstandingBalanceQuery

This commit is contained in:
Feruz Oripov
2024-02-26 23:37:11 +05:00
parent 81f40a99d9
commit ff6830f954
9 changed files with 39 additions and 38 deletions

View File

@@ -7,7 +7,7 @@ class CompleteOrdersWithBalanceQuery
end
def call
OutstandingBalance.new(sorted_finalized_orders).query
OutstandingBalanceQuery.new(sorted_finalized_orders).call
end
private

View File

@@ -32,6 +32,6 @@ class CustomersWithBalanceQuery
end
def outstanding_balance_sum
"SUM(#{OutstandingBalance.new.statement})::float"
"SUM(#{OutstandingBalanceQuery.new.statement})::float"
end
end

View File

@@ -11,7 +11,7 @@
#
# 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.
class OutstandingBalance
class OutstandingBalanceQuery
# 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
@@ -22,7 +22,7 @@ class OutstandingBalance
@relation = relation
end
def query
def call
relation.select("#{statement} AS balance_value")
end

View File

@@ -29,7 +29,7 @@ module Reporting
def orders
search_result = search.result.order(:completed_at)
orders = OutstandingBalance.new(search_result).query.select('spree_orders.*')
orders = OutstandingBalanceQuery.new(search_result).call.select('spree_orders.*')
filter(orders)
end

View File

@@ -23,7 +23,7 @@ describe Spree::UsersController, type: :controller do
let(:orders) { assigns(:orders) }
let(:shops) { Enterprise.where(id: orders.pluck(:distributor_id)) }
let(:outstanding_balance) { instance_double(OutstandingBalance) }
let(:outstanding_balance_query) { instance_double(OutstandingBalanceQuery) }
before do
allow(controller).to receive(:spree_current_user) { u1 }
@@ -47,9 +47,9 @@ describe Spree::UsersController, type: :controller do
expect(orders).not_to include d1o3
end
it 'calls OutstandingBalance' do
allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:query) { Spree::Order.none }
it 'calls OutstandingBalanceQuery' do
allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance_query)
expect(outstanding_balance_query).to receive(:call) { Spree::Order.none }
spree_get :show
end

View File

@@ -17,9 +17,9 @@ module Reporting
end
describe "fetching orders" do
it 'calls the OutstandingBalance query object' do
outstanding_balance = instance_double(OutstandingBalance, query: Spree::Order.none)
expect(OutstandingBalance).to receive(:new).and_return(outstanding_balance)
it 'calls the OutstandingBalanceQuery query object' do
outstanding_balance = instance_double(OutstandingBalanceQuery, call: Spree::Order.none)
expect(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance)
subject.orders
end

View File

@@ -7,7 +7,7 @@ describe CompleteOrdersWithBalanceQuery do
describe '#call' do
let(:user) { order.user }
let(:outstanding_balance) { instance_double(OutstandingBalance) }
let(:outstanding_balance) { instance_double(OutstandingBalanceQuery) }
context 'when the user has complete orders' do
let(:order) do
@@ -24,9 +24,9 @@ describe CompleteOrdersWithBalanceQuery do
)
end
it 'calls OutstandingBalance#query' do
allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:query)
it 'calls OutstandingBalanceQuery#call' do
allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:call)
result
end
@@ -44,9 +44,9 @@ describe CompleteOrdersWithBalanceQuery do
context 'when the user has no complete orders' do
let(:order) { create(:order) }
it 'calls OutstandingBalance' do
allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:query)
it 'calls OutstandingBalanceQuery' do
allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:call)
result
end

View File

@@ -9,10 +9,10 @@ describe CustomersWithBalanceQuery do
let(:customers) { create(:customer) }
let(:total) { 200.00 }
let(:order_total) { 100.00 }
let(:outstanding_balance) { instance_double(OutstandingBalance) }
let(:outstanding_balance) { instance_double(OutstandingBalanceQuery) }
it 'calls OutstandingBalance#statement' do
allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance)
it 'calls OutstandingBalanceQuery#statement' do
allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:statement)
result

View File

@@ -2,14 +2,15 @@
require 'spec_helper'
describe OutstandingBalance do
subject(:outstanding_balance) { described_class.new(relation) }
describe OutstandingBalanceQuery do
subject { described_class.new(relation) }
let(:result) { subject.call }
describe '#statement' do
let(:relation) { Spree::Order.none }
it 'returns the CASE statement necessary to compute the order balance' do
normalized_sql_statement = normalize(outstanding_balance.statement)
normalized_sql_statement = normalize(subject.statement)
expect(normalized_sql_statement).to eq(normalize(<<-SQL))
CASE WHEN "spree_orders"."state" IN ('canceled', 'returned') THEN "spree_orders"."payment_total"
@@ -23,7 +24,7 @@ describe OutstandingBalance do
end
end
describe '#query' do
describe '#call' do
let(:relation) { Spree::Order.all }
let(:total) { 200.00 }
let(:order_total) { 100.00 }
@@ -35,7 +36,7 @@ describe OutstandingBalance do
end
it 'returns the order balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(-order_total)
end
end
@@ -47,7 +48,7 @@ describe OutstandingBalance do
end
it 'returns the order balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(-order_total)
end
end
@@ -59,7 +60,7 @@ describe OutstandingBalance do
end
it 'returns the order balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(-order_total)
end
end
@@ -71,7 +72,7 @@ describe OutstandingBalance do
end
it 'returns the order balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(-order_total)
end
end
@@ -85,7 +86,7 @@ describe OutstandingBalance do
end
it 'returns the customer balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(-order_total)
end
end
@@ -101,7 +102,7 @@ describe OutstandingBalance do
end
it 'returns the customer balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end
@@ -117,7 +118,7 @@ describe OutstandingBalance do
end
it 'returns the customer balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(payment_total)
end
end
@@ -133,7 +134,7 @@ describe OutstandingBalance do
end
it 'returns the customer balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end
@@ -149,7 +150,7 @@ describe OutstandingBalance do
end
it 'returns the customer balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end
@@ -165,7 +166,7 @@ describe OutstandingBalance do
end
it 'returns the customer balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end
@@ -182,14 +183,14 @@ describe OutstandingBalance do
end
it 'returns the customer balance' do
order = outstanding_balance.query.first
order = result.first
expect(order.balance_value).to eq(payment_total)
end
end
context 'when there are no orders' do
it 'returns the order balance' do
orders = outstanding_balance.query
orders = result
expect(orders).to be_empty
end
end