Skip to content

Commit

Permalink
fix(invoice-filtering): Wrong total count when filter invoices by met…
Browse files Browse the repository at this point in the history
…adata (#3097)

## Context

Recently added invoice filtering by metadata incorrectly counts
`total_count` in pagination metadata.

## Description

Fix pagination `total_count` when filter invoices by metadata.
  • Loading branch information
floganz authored Jan 23, 2025
1 parent 88b480b commit 033e72e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/queries/invoices_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def with_amount_range(scope)
end

def with_metadata(scope)
base_scope = scope.left_joins(:metadata)
base_scope = scope.left_joins(:metadata).limit(nil).offset(nil)
subquery = base_scope

presence_filters = filters.metadata.select { |_k, v| v.present? }
Expand Down
6 changes: 4 additions & 2 deletions spec/queries/invoices_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,8 @@
}
end

let!(:matching_invoices) { create_pair(:invoice, organization:) }
let(:pagination) { {page: 1, limit: 2} }
let!(:matching_invoices) { create_list(:invoice, 3, organization:) }

before do
matching_invoices.each do |invoice|
Expand All @@ -692,7 +693,8 @@

it "returns invoices with matching metadata filters" do
expect(result).to be_success
expect(result.invoices.pluck(:id)).to match_array matching_invoices.pluck(:id)
expect(result.invoices.pluck(:id)).to match_array matching_invoices[1..].pluck(:id)
expect(result.invoices.total_count).to eq matching_invoices.count
end
end
end
Expand Down

0 comments on commit 033e72e

Please sign in to comment.