Feature: Expense summary endpoint #75
@ -8,16 +8,36 @@ module Expenses
|
||||
|
||||
def query
|
||||
<<~SQL
|
||||
SELECT count(amount) as fixed,
|
||||
0 as fixed_count,
|
||||
0 as variable,
|
||||
0 as variable_count,
|
||||
0 as total,
|
||||
0 as total_count,
|
||||
0 as max_projected,
|
||||
0 as per_person
|
||||
FROM EXPENSES
|
||||
LIMIT 1;
|
||||
WITH guest_count AS (#{guest_count_per_status}),
|
||||
expense_summary AS (#{expense_summary})
|
||||
SELECT expense_summary.fixed,
|
||||
expense_summary.fixed_count,
|
||||
expense_summary.variable,
|
||||
expense_summary.variable_count,
|
||||
expense_summary.total_count,
|
||||
expense_summary.fixed + expense_summary.variable * guest_count.confirmed as total,
|
||||
expense_summary.fixed + expense_summary.variable * guest_count.projected as max_projected,
|
||||
(expense_summary.fixed + expense_summary.variable * guest_count.confirmed) / guest_count.confirmed as per_person
|
||||
FROM guest_count, expense_summary;
|
||||
SQL
|
||||
end
|
||||
|
||||
def expense_summary
|
||||
<<~SQL
|
||||
SELECT coalesce(sum(amount) filter (where pricing_type = 'fixed'), 0) as fixed,
|
||||
coalesce(count(amount) filter (where pricing_type = 'fixed'), 0) as fixed_count,
|
||||
coalesce(sum(amount) filter (where pricing_type = 'per_person'), 0) as variable,
|
||||
coalesce(count(amount) filter (where pricing_type = 'per_person'), 0) as variable_count,
|
||||
count(*) as total_count
|
||||
FROM expenses
|
||||
SQL
|
||||
end
|
||||
|
||||
def guest_count_per_status
|
||||
<<~SQL
|
||||
SELECT COALESCE(count(*) filter(where status = #{Guest.statuses["confirmed"]}), 0) as confirmed,
|
||||
COALESCE(count(*) filter(where status IN (#{Guest.statuses.values_at("confirmed", "invited", "tentative").join(",")})), 0) as projected
|
||||
FROM guests
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
@ -32,7 +32,7 @@ module Expenses
|
||||
create(:expense, :fixed, amount: 200)
|
||||
end
|
||||
|
||||
it "returns the sum of fixed expenses" do
|
||||
it "returns the sum of fixed expenses", :aggregate_failures do
|
||||
expect(response["fixed"]).to eq(300)
|
||||
expect(response["fixed_count"]).to eq(2)
|
||||
expect(response["variable"]).to be_zero
|
||||
|
Loading…
x
Reference in New Issue
Block a user