Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/graphql/resolvers/payments_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PaymentsResolver < Resolvers::BaseResolver

description "Query payments of an organization"

argument :currency, Types::CurrencyEnum, required: false
argument :external_customer_id, ID, required: false
argument :invoice_id, ID, required: false
argument :limit, Integer, required: false
Expand All @@ -17,12 +18,13 @@ class PaymentsResolver < Resolvers::BaseResolver

type Types::Payments::Object.collection_type, null: false

def resolve(page: nil, limit: nil, invoice_id: nil, external_customer_id: nil, search_term: nil)
def resolve(currency: nil, page: nil, limit: nil, invoice_id: nil, external_customer_id: nil, search_term: nil)
result = PaymentsQuery.call(
organization: current_organization,
filters: {
invoice_id:,
external_customer_id:
external_customer_id:,
currency:
},
search_term:,
pagination: {
Expand Down
7 changes: 6 additions & 1 deletion app/queries/payments_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class PaymentsQuery < BaseQuery
Result = BaseResult[:payments]
Filters = BaseFilters[:invoice_id, :external_customer_id]
Filters = BaseFilters[:invoice_id, :external_customer_id, :currency]

def call
return result unless validate_filters.success?
Expand Down Expand Up @@ -82,6 +82,7 @@ def search_params
def apply_filters(scope)
scope = filter_by_invoice(scope) if filters.invoice_id.present?
scope = filter_by_customer(scope) if filters.external_customer_id.present?
scope = filter_by_currency(scope) if filters.currency.present?
scope
end

Expand All @@ -105,4 +106,8 @@ def filter_by_invoice(scope)
invoice_id:
)
end

def filter_by_currency(scope)
scope.where(amount_currency: filters.currency)
end
end
2 changes: 1 addition & 1 deletion schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions spec/graphql/resolvers/payments_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,33 @@
.to contain_exactly(invoice1.id, invoice2.id)
end
end

context "when currency is present" do
let(:query) do
<<~GQL
query($currency: CurrencyEnum!) {
payments(currency: $currency, limit: 5) {
collection { id }
metadata { currentPage, totalCount }
}
}
GQL
end

let(:usd_invoice) { create(:invoice, customer:, organization:, currency: "USD") }
let!(:usd_payment) { create(:payment, payable: usd_invoice, amount_currency: "USD") }

it "returns only payments matching the currency" do
result = execute_graphql(
current_user: membership.user,
current_organization: organization,
permissions: required_permission,
query:,
variables: {currency: "USD"}
)

ids = result["data"]["payments"]["collection"].map { |p| p["id"] }
expect(ids).to contain_exactly(usd_payment.id)
end
end
end
20 changes: 20 additions & 0 deletions spec/queries/payments_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,26 @@
end
end

context "when filtering by currency" do
let(:filters) { {currency: "USD"} }
let(:usd_invoice) { create(:invoice, organization:, currency: "USD") }
let!(:usd_payment) { create(:payment, payable: usd_invoice, amount_currency: "USD") }

it "returns only payments matching the currency" do
expect(result).to be_success
expect(returned_ids).to contain_exactly(usd_payment.id)
end
end

context "when filtering by a currency that matches no payments" do
let(:filters) { {currency: "GBP"} }

it "returns an empty result set" do
expect(result).to be_success
expect(returned_ids).to be_empty
end
end

context "when filtering with an invalid invoice_id" do
let(:filters) { {invoice_id: "invalid-uuid"} }

Expand Down
Loading