Feature: Expense summary endpoint #75
| @ -8,16 +8,36 @@ module Expenses | |||||||
| 
 | 
 | ||||||
|     def query |     def query | ||||||
|       <<~SQL |       <<~SQL | ||||||
|         SELECT count(amount) as fixed, |         WITH guest_count AS (#{guest_count_per_status}), | ||||||
|                0 as fixed_count, |              expense_summary AS (#{expense_summary}) | ||||||
|                0 as variable, |         SELECT expense_summary.fixed, | ||||||
|                0 as variable_count, |                expense_summary.fixed_count, | ||||||
|                0 as total, |                expense_summary.variable, | ||||||
|                0 as total_count, |                expense_summary.variable_count, | ||||||
|                0 as max_projected, |                expense_summary.total_count, | ||||||
|                0 as per_person |                expense_summary.fixed + expense_summary.variable * guest_count.confirmed as total, | ||||||
|         FROM EXPENSES |                expense_summary.fixed + expense_summary.variable * guest_count.projected as max_projected, | ||||||
|         LIMIT 1; |                (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 |       SQL | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  | |||||||
| @ -32,7 +32,7 @@ module Expenses | |||||||
|           create(:expense, :fixed, amount: 200) |           create(:expense, :fixed, amount: 200) | ||||||
|         end |         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"]).to eq(300) | ||||||
|           expect(response["fixed_count"]).to eq(2) |           expect(response["fixed_count"]).to eq(2) | ||||||
|           expect(response["variable"]).to be_zero |           expect(response["variable"]).to be_zero | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user