diff --git a/app/graphql/types/add_ons/object.rb b/app/graphql/types/add_ons/object.rb index ac9d028cb51..20b7d1fc462 100644 --- a/app/graphql/types/add_ons/object.rb +++ b/app/graphql/types/add_ons/object.rb @@ -25,7 +25,9 @@ class Object < Types::BaseObject field :taxes, [Types::Taxes::Object] - field :integration_mappings, [Types::IntegrationMappings::Object], null: true + field :integration_mappings, [Types::IntegrationMappings::Object], null: true do + argument :integration_id, ID, required: false + end def customers_count object.applied_add_ons.select(:customer_id).distinct.count @@ -34,6 +36,12 @@ def customers_count def applied_add_ons_count object.applied_add_ons.count end + + def integration_mappings(integration_id: nil) + mappings = object.integration_mappings + mappings = mappings.where(integration_id:) if integration_id + mappings + end end end end diff --git a/app/graphql/types/billable_metrics/object.rb b/app/graphql/types/billable_metrics/object.rb index a2fdbb00a99..e9dc61f615d 100644 --- a/app/graphql/types/billable_metrics/object.rb +++ b/app/graphql/types/billable_metrics/object.rb @@ -30,7 +30,9 @@ class Object < Types::BaseObject field :deleted_at, GraphQL::Types::ISO8601DateTime, null: true field :updated_at, GraphQL::Types::ISO8601DateTime, null: false - field :integration_mappings, [Types::IntegrationMappings::Object], null: true + field :integration_mappings, [Types::IntegrationMappings::Object], null: true do + argument :integration_id, ID, required: false + end def subscriptions_count object.plans.joins(:subscriptions).count @@ -52,6 +54,12 @@ def draft_invoices_count def plans_count object.plans.distinct.count end + + def integration_mappings(integration_id: nil) + mappings = object.integration_mappings + mappings = mappings.where(integration_id:) if integration_id + mappings + end end end end diff --git a/schema.graphql b/schema.graphql index 25b68062918..c2a71818294 100644 --- a/schema.graphql +++ b/schema.graphql @@ -62,7 +62,7 @@ type AddOn { deletedAt: ISO8601DateTime description: String id: ID! - integrationMappings: [Mapping!] + integrationMappings(integrationId: ID): [Mapping!] invoiceDisplayName: String name: String! organization: Organization @@ -178,7 +178,7 @@ type BillableMetric { fieldName: String filters: [BillableMetricFilter!] id: ID! - integrationMappings: [Mapping!] + integrationMappings(integrationId: ID): [Mapping!] name: String! organization: Organization plansCount: Int! diff --git a/schema.json b/schema.json index fcdd496df1d..ca6b9032052 100644 --- a/schema.json +++ b/schema.json @@ -464,7 +464,18 @@ "isDeprecated": false, "deprecationReason": null, "args": [ - + { + "name": "integrationId", + "description": null, + "type": { + "kind": "SCALAR", + "name": "ID", + "ofType": null + }, + "defaultValue": null, + "isDeprecated": false, + "deprecationReason": null + } ] }, { @@ -1677,7 +1688,18 @@ "isDeprecated": false, "deprecationReason": null, "args": [ - + { + "name": "integrationId", + "description": null, + "type": { + "kind": "SCALAR", + "name": "ID", + "ofType": null + }, + "defaultValue": null, + "isDeprecated": false, + "deprecationReason": null + } ] }, { diff --git a/spec/graphql/resolvers/add_ons_resolver_spec.rb b/spec/graphql/resolvers/add_ons_resolver_spec.rb index 2e2f99ec304..ae0f7ba726b 100644 --- a/spec/graphql/resolvers/add_ons_resolver_spec.rb +++ b/spec/graphql/resolvers/add_ons_resolver_spec.rb @@ -43,4 +43,50 @@ expect(add_ons_response['metadata']['totalCount']).to eq(1) end end + + context 'with integration mappings' do + let(:integration) { create(:netsuite_integration, organization:) } + let(:netsuite_mapping) { create(:netsuite_mapping, integration:, mappable_type: 'AddOn', mappable_id: add_on.id) } + let(:netsuite_mapping2) { create(:netsuite_mapping, external_name: 'Bla') } + let(:query) do + <<~GQL + query($integrationId: ID) { + addOns(limit: 5) { + collection { id name integrationMappings(integrationId: $integrationId) { externalId externalName } } + metadata { currentPage, totalCount } + } + } + GQL + end + + before do + integration + netsuite_mapping + netsuite_mapping2 + end + + it 'returns a list of add-ons' do + result = execute_graphql( + current_user: membership.user, + current_organization: organization, + permissions: required_permission, + query:, + variables: {integrationId: integration.id}, + ) + + add_ons_response = result['data']['addOns'] + + aggregate_failures do + expect(add_ons_response['collection'].first['id']).to eq(add_on.id) + expect(add_ons_response['collection'].first['name']).to eq(add_on.name) + + expect(add_ons_response['collection'].first['integrationMappings'].count).to eq(1) + expect(add_ons_response['collection'].first['integrationMappings'].first['externalName']) + .to eq('Credits and Discounts') + + expect(add_ons_response['metadata']['currentPage']).to eq(1) + expect(add_ons_response['metadata']['totalCount']).to eq(1) + end + end + end end