Skip to content

Commit

Permalink
Fix InvoicesQuery payment dispute lost filter
Browse files Browse the repository at this point in the history
Whenever `payment_dispute_lost` filter had a value other than `nil` it
was returing only inovoices with `payment_dispute_lost`,

hence, when the filter had a `false` value, it still returns only
invoices with a payment dispute lost.

This change fixes the filter by providing the proper scoping base on the
value of the filter.
  • Loading branch information
ancorcruz committed Jul 26, 2024
1 parent 1365c27 commit a3c0c72
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/queries/invoices_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def call(search_term: nil, status: nil, filters: {}, customer_id: nil, payment_s
invoices = with_issuing_date_range(invoices) if filters.issuing_date_from || filters.issuing_date_to
invoices = invoices.where(status:) if status.present?
invoices = invoices.where(payment_status:) if payment_status.present?
invoices = invoices.where.not(payment_dispute_lost_at: nil) unless payment_dispute_lost.nil?
invoices = invoices.where.not(payment_dispute_lost_at: nil) if payment_dispute_lost
invoices = invoices.where(payment_dispute_lost_at: nil) if payment_dispute_lost == false
invoices = invoices.where(payment_overdue:) if payment_overdue.present?
invoices = invoices.order(issuing_date: :desc, created_at: :desc)
invoices = paginate(invoices)
Expand Down
44 changes: 44 additions & 0 deletions spec/queries/invoices_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,50 @@
end
end

context 'when filtering by payment dispute lost false' do
it 'returns 1 invoices' do
result = invoice_query.call(
search_term: nil,
status: nil,
payment_dispute_lost: false
)

returned_ids = result.invoices.pluck(:id)

aggregate_failures do
expect(returned_ids.count).to eq(5)
expect(returned_ids).to include(invoice_first.id)
expect(returned_ids).to include(invoice_second.id)
expect(returned_ids).to include(invoice_third.id)
expect(returned_ids).to include(invoice_fourth.id)
expect(returned_ids).to include(invoice_fifth.id)
expect(returned_ids).not_to include(invoice_sixth.id)
end
end
end

context 'when filtering by payment dispute lost' do
it 'returns 1 invoices' do
result = invoice_query.call(
search_term: nil,
status: nil,
payment_dispute_lost: true
)

returned_ids = result.invoices.pluck(:id)

aggregate_failures do
expect(result.invoices.count).to eq(1)
expect(returned_ids).not_to include(invoice_first.id)
expect(returned_ids).not_to include(invoice_second.id)
expect(returned_ids).not_to include(invoice_third.id)
expect(returned_ids).not_to include(invoice_fourth.id)
expect(returned_ids).not_to include(invoice_fifth.id)
expect(returned_ids).to include(invoice_sixth.id)
end
end
end

context 'when filtering by payment overdue' do
it 'returns expected invoices' do
result = invoice_query.call(
Expand Down

0 comments on commit a3c0c72

Please sign in to comment.