diff --git a/app/graphql/resolvers/payments_resolver.rb b/app/graphql/resolvers/payments_resolver.rb index 06203cb42d3..04eac25124d 100644 --- a/app/graphql/resolvers/payments_resolver.rb +++ b/app/graphql/resolvers/payments_resolver.rb @@ -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 @@ -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: { diff --git a/app/queries/payments_query.rb b/app/queries/payments_query.rb index 518dc0498b9..536426c0b1c 100644 --- a/app/queries/payments_query.rb +++ b/app/queries/payments_query.rb @@ -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? @@ -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 @@ -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 diff --git a/schema.graphql b/schema.graphql index 7906139fd67..5643e9d0e28 100644 --- a/schema.graphql +++ b/schema.graphql @@ -10482,7 +10482,7 @@ type Query { """ Query payments of an organization """ - payments(externalCustomerId: ID, invoiceId: ID, limit: Int, page: Int, searchTerm: String): PaymentCollection! + payments(currency: CurrencyEnum, externalCustomerId: ID, invoiceId: ID, limit: Int, page: Int, searchTerm: String): PaymentCollection! """ Query a single plan of an organization diff --git a/schema.json b/schema.json index 3953882f2f8..7996c473ee9 100644 --- a/schema.json +++ b/schema.json @@ -56167,6 +56167,18 @@ "name": "payments", "description": "Query payments of an organization", "args": [ + { + "name": "currency", + "description": null, + "type": { + "kind": "ENUM", + "name": "CurrencyEnum", + "ofType": null + }, + "defaultValue": null, + "isDeprecated": false, + "deprecationReason": null + }, { "name": "externalCustomerId", "description": null, diff --git a/spec/graphql/resolvers/payments_resolver_spec.rb b/spec/graphql/resolvers/payments_resolver_spec.rb index 1b363776817..cecab9fec95 100644 --- a/spec/graphql/resolvers/payments_resolver_spec.rb +++ b/spec/graphql/resolvers/payments_resolver_spec.rb @@ -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 diff --git a/spec/queries/payments_query_spec.rb b/spec/queries/payments_query_spec.rb index 6b206d009f2..cad6fbc1339 100644 --- a/spec/queries/payments_query_spec.rb +++ b/spec/queries/payments_query_spec.rb @@ -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"} }