From d2ff043159dc651dbf8c9ea6b09e03b21b3ef0e8 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Fri, 19 Jul 2024 20:48:15 +0100 Subject: [PATCH] fix(export-invoices): allow no pagination for Query objects (#2307) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Roadmap Task 👉 https://getlago.canny.io/feature-requests/p/ability-to-export-data-from-the-user-interface ## Context When exporting data (eg: invoices) we use query objects to fetch the records to export. However, the current implementation of query objects paginates to first page and limit results to 25 or 100 (depends on implementation) when no pagination information is provided. We need the query object to allow no pagination and fetch all resources matching the search / filter criteria when pagination args are not provided. ## Description This change allows client code to call Query object with `pagination: nil` arg to avoid pagination defaults (page: 1, limit: 25). Also, it makes `BaseQuery::Pagination` internal knowledge of the query objects. As query objects expect pagination as a hash, for example: `{page: 1, limit: 50}`. The same will be applied to `BaseQuery::Filters` in a coming PR. Updates the Query objects implementing the latest approach and updates `InvoicesQuery` object to allow no pagination and fix the CSV data export issues. --- .../api/v1/applied_coupons_controller.rb | 4 +- .../api/v1/customers/usage_controller.rb | 4 +- app/controllers/api/v1/fees_controller.rb | 4 +- app/controllers/api/v1/invoices_controller.rb | 10 +- .../api/v1/subscriptions_controller.rb | 4 +- .../customer_portal/invoices_resolver.rb | 8 +- .../resolvers/customers/invoices_resolver.rb | 4 +- ...ntegration_collection_mappings_resolver.rb | 2 +- .../integration_mappings_resolver.rb | 2 +- app/graphql/resolvers/invoices_resolver.rb | 4 +- .../resolvers/subscriptions_resolver.rb | 2 +- app/queries/base_query.rb | 26 +++-- app/queries/invoices_query.rb | 5 +- app/queries/past_usage_query.rb | 12 +- app/services/data_exports/csv/invoices.rb | 4 +- spec/queries/applied_coupons_query_spec.rb | 86 +++++++------- spec/queries/fees_query_spec.rb | 4 +- ...egration_collection_mappings_query_spec.rb | 2 +- .../integration_mappings_query_spec.rb | 2 +- spec/queries/invoices_query_spec.rb | 105 +++++++----------- spec/queries/past_usage_query_spec.rb | 28 ++++- spec/queries/subscriptions_query_spec.rb | 4 +- .../data_exports/csv/invoice_fees_spec.rb | 4 +- .../data_exports/csv/invoices_spec.rb | 4 +- 24 files changed, 167 insertions(+), 167 deletions(-) diff --git a/app/controllers/api/v1/applied_coupons_controller.rb b/app/controllers/api/v1/applied_coupons_controller.rb index 7da0d612f4d..6029a40d137 100644 --- a/app/controllers/api/v1/applied_coupons_controller.rb +++ b/app/controllers/api/v1/applied_coupons_controller.rb @@ -31,10 +31,10 @@ def create def index result = AppliedCouponsQuery.call( organization: current_organization, - pagination: BaseQuery::Pagination.new( + pagination: { page: params[:page], limit: params[:per_page] || PER_PAGE - ), + }, filters: BaseQuery::Filters.new(index_filters) ) diff --git a/app/controllers/api/v1/customers/usage_controller.rb b/app/controllers/api/v1/customers/usage_controller.rb index ecacea78b58..d9c2ebf0f36 100644 --- a/app/controllers/api/v1/customers/usage_controller.rb +++ b/app/controllers/api/v1/customers/usage_controller.rb @@ -29,10 +29,10 @@ def current def past result = PastUsageQuery.call( organization: current_organization, - pagination: BaseQuery::Pagination.new( + pagination: { page: params[:page], limit: params[:per_page] || PER_PAGE - ), + }, filters: BaseQuery::Filters.new(past_usage_filters) ) diff --git a/app/controllers/api/v1/fees_controller.rb b/app/controllers/api/v1/fees_controller.rb index 3c7f630a40a..2a67b8ca015 100644 --- a/app/controllers/api/v1/fees_controller.rb +++ b/app/controllers/api/v1/fees_controller.rb @@ -27,10 +27,10 @@ def update def index result = FeesQuery.call( organization: current_organization, - pagination: BaseQuery::Pagination.new( + pagination: { page: params[:page], limit: params[:per_page] || PER_PAGE - ), + }, filters: BaseQuery::Filters.new(index_filters) ) diff --git a/app/controllers/api/v1/invoices_controller.rb b/app/controllers/api/v1/invoices_controller.rb index 31832dc129e..f91607ea343 100644 --- a/app/controllers/api/v1/invoices_controller.rb +++ b/app/controllers/api/v1/invoices_controller.rb @@ -43,9 +43,13 @@ def show end def index - result = InvoicesQuery.new(organization: current_organization).call( - page: params[:page], - limit: params[:per_page] || PER_PAGE, + result = InvoicesQuery.new( + organization: current_organization, + pagination: { + page: params[:page], + limit: params[:per_page] || PER_PAGE + } + ).call( search_term: params[:search_term], payment_status: (params[:payment_status] if valid_payment_status?(params[:payment_status])), payment_dispute_lost: params[:payment_dispute_lost], diff --git a/app/controllers/api/v1/subscriptions_controller.rb b/app/controllers/api/v1/subscriptions_controller.rb index da97a5072b7..3dbfcc6f653 100644 --- a/app/controllers/api/v1/subscriptions_controller.rb +++ b/app/controllers/api/v1/subscriptions_controller.rb @@ -84,10 +84,10 @@ def show def index result = SubscriptionsQuery.call( organization: current_organization, - pagination: BaseQuery::Pagination.new( + pagination: { page: params[:page], limit: params[:per_page] || PER_PAGE - ), + }, filters: BaseQuery::Filters.new(index_filters) ) diff --git a/app/graphql/resolvers/customer_portal/invoices_resolver.rb b/app/graphql/resolvers/customer_portal/invoices_resolver.rb index 666221633f2..711c55ff780 100644 --- a/app/graphql/resolvers/customer_portal/invoices_resolver.rb +++ b/app/graphql/resolvers/customer_portal/invoices_resolver.rb @@ -15,12 +15,14 @@ class InvoicesResolver < Resolvers::BaseResolver type Types::Invoices::Object.collection_type, null: false def resolve(status: nil, page: nil, limit: nil, search_term: nil) - query = InvoicesQuery.new(organization: context[:customer_portal_user].organization) + query = InvoicesQuery.new( + organization: context[:customer_portal_user], + pagination: {page:, limit:} + ) + result = query.call( customer_id: context[:customer_portal_user].id, search_term:, - page:, - limit:, status: ) diff --git a/app/graphql/resolvers/customers/invoices_resolver.rb b/app/graphql/resolvers/customers/invoices_resolver.rb index 1be3d9c00a3..36fad0da4fd 100644 --- a/app/graphql/resolvers/customers/invoices_resolver.rb +++ b/app/graphql/resolvers/customers/invoices_resolver.rb @@ -19,12 +19,10 @@ class InvoicesResolver < Resolvers::BaseResolver type Types::Invoices::Object.collection_type, null: false def resolve(customer_id: nil, status: nil, page: nil, limit: nil, search_term: nil) - query = InvoicesQuery.new(organization: current_organization) + query = InvoicesQuery.new(organization: current_organization, pagination: {page:, limit:}) result = query.call( search_term:, customer_id:, - page:, - limit:, status: ) diff --git a/app/graphql/resolvers/integration_collection_mappings_resolver.rb b/app/graphql/resolvers/integration_collection_mappings_resolver.rb index 55ff83f47da..21833ba4ef0 100644 --- a/app/graphql/resolvers/integration_collection_mappings_resolver.rb +++ b/app/graphql/resolvers/integration_collection_mappings_resolver.rb @@ -19,7 +19,7 @@ class IntegrationCollectionMappingsResolver < Resolvers::BaseResolver def resolve(page: nil, limit: nil, integration_id: nil, mapping_type: nil) result = ::IntegrationCollectionMappingsQuery.call( organization: current_organization, - pagination: BaseQuery::Pagination.new(page:, limit:), + pagination: {page:, limit:}, filters: BaseQuery::Filters.new({integration_id:, mapping_type:}) ) diff --git a/app/graphql/resolvers/integration_mappings_resolver.rb b/app/graphql/resolvers/integration_mappings_resolver.rb index 8a240efcf3d..3f132f0a7a1 100644 --- a/app/graphql/resolvers/integration_mappings_resolver.rb +++ b/app/graphql/resolvers/integration_mappings_resolver.rb @@ -19,7 +19,7 @@ class IntegrationMappingsResolver < Resolvers::BaseResolver def resolve(page: nil, limit: nil, integration_id: nil, mappable_type: nil) result = ::IntegrationMappingsQuery.call( organization: current_organization, - pagination: BaseQuery::Pagination.new(page:, limit:), + pagination: {page:, limit:}, filters: BaseQuery::Filters.new({integration_id:, mappable_type:}) ) diff --git a/app/graphql/resolvers/invoices_resolver.rb b/app/graphql/resolvers/invoices_resolver.rb index f63d965fb03..4f6686a6997 100644 --- a/app/graphql/resolvers/invoices_resolver.rb +++ b/app/graphql/resolvers/invoices_resolver.rb @@ -38,11 +38,9 @@ def resolve( # rubocop:disable Metrics/ParameterLists payment_dispute_lost: nil, payment_overdue: nil ) - query = InvoicesQuery.new(organization: current_organization) + query = InvoicesQuery.new(organization: current_organization, pagination: {page:, limit:}) result = query.call( search_term:, - page:, - limit:, payment_status:, payment_dispute_lost:, payment_overdue:, diff --git a/app/graphql/resolvers/subscriptions_resolver.rb b/app/graphql/resolvers/subscriptions_resolver.rb index 77e91a1c8d6..5c53298a55c 100644 --- a/app/graphql/resolvers/subscriptions_resolver.rb +++ b/app/graphql/resolvers/subscriptions_resolver.rb @@ -19,7 +19,7 @@ class SubscriptionsResolver < Resolvers::BaseResolver def resolve(page: nil, limit: nil, plan_code: nil, status: nil) result = SubscriptionsQuery.call( organization: current_organization, - pagination: BaseQuery::Pagination.new(page:, limit:), + pagination: {page:, limit:}, filters: BaseQuery::Filters.new({plan_code:, status:}) ) diff --git a/app/queries/base_query.rb b/app/queries/base_query.rb index b40d1dbc68d..2b3244d5242 100644 --- a/app/queries/base_query.rb +++ b/app/queries/base_query.rb @@ -1,19 +1,16 @@ # frozen_string_literal: true class BaseQuery < BaseService - PER_PAGE = 100 + # nil values force Kaminari to apply its default values for page and limit. + DEFAULT_PAGINATION_PARAMS = {page: nil, limit: nil} - Pagination = Struct.new(:page, :limit, keyword_init: true) do - def initialize(page: 0, limit: PER_PAGE) - super - end - end + Pagination = Struct.new(:page, :limit, keyword_init: true) class Filters < OpenStruct; end - def initialize(organization:, pagination: Pagination.new, filters: Filters.new) + def initialize(organization:, pagination: DEFAULT_PAGINATION_PARAMS, filters: Filters.new) @organization = organization - @pagination = pagination + @pagination_params = pagination @filters = filters super @@ -21,9 +18,20 @@ def initialize(organization:, pagination: Pagination.new, filters: Filters.new) private - attr_reader :organization, :pagination, :filters + attr_reader :organization, :pagination_params, :filters + + def pagination + return if pagination_params.blank? + + @pagination ||= Pagination.new( + page: pagination_params[:page], + limit: pagination_params[:limit] + ) + end def paginate(scope) + return scope unless pagination + scope.page(pagination.page).per(pagination.limit) end diff --git a/app/queries/invoices_query.rb b/app/queries/invoices_query.rb index 9027d3fd77e..7a271abcbd6 100644 --- a/app/queries/invoices_query.rb +++ b/app/queries/invoices_query.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class InvoicesQuery < BaseQuery - def call(search_term:, status:, page:, limit:, filters: {}, customer_id: nil, payment_status: nil, payment_dispute_lost: nil, payment_overdue: nil) # rubocop:disable Metrics/ParameterLists + def call(search_term: nil, status: nil, filters: {}, customer_id: nil, payment_status: nil, payment_dispute_lost: nil, payment_overdue: nil) # rubocop:disable Metrics/ParameterLists @search_term = search_term @customer_id = customer_id @filters = filters @@ -16,7 +16,8 @@ def call(search_term:, status:, page:, limit:, filters: {}, customer_id: nil, pa 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(payment_overdue:) if payment_overdue.present? - invoices = invoices.order(issuing_date: :desc, created_at: :desc).page(page).per(limit) + invoices = invoices.order(issuing_date: :desc, created_at: :desc) + invoices = paginate(invoices) result.invoices = invoices result diff --git a/app/queries/past_usage_query.rb b/app/queries/past_usage_query.rb index 314e9305055..1b4045965db 100644 --- a/app/queries/past_usage_query.rb +++ b/app/queries/past_usage_query.rb @@ -14,11 +14,13 @@ def call end # NOTE: Pagination attributes - result.current_page = query_result.current_page - result.next_page = query_result.next_page - result.prev_page = query_result.prev_page - result.total_pages = query_result.total_pages - result.total_count = query_result.total_count + if pagination + result.current_page = query_result.current_page + result.next_page = query_result.next_page + result.prev_page = query_result.prev_page + result.total_pages = query_result.total_pages + result.total_count = query_result.total_count + end result end diff --git a/app/services/data_exports/csv/invoices.rb b/app/services/data_exports/csv/invoices.rb index 7b22bd37844..c95d66392c9 100644 --- a/app/services/data_exports/csv/invoices.rb +++ b/app/services/data_exports/csv/invoices.rb @@ -86,11 +86,9 @@ def query status = resource_query["status"] filters = resource_query.except("search_term", "status") - InvoicesQuery.new(organization: organization).call( + InvoicesQuery.new(organization: organization, pagination: nil).call( search_term:, status:, - page: nil, - limit: nil, filters: ) end diff --git a/spec/queries/applied_coupons_query_spec.rb b/spec/queries/applied_coupons_query_spec.rb index cf0fd1400ba..20e99612c9d 100644 --- a/spec/queries/applied_coupons_query_spec.rb +++ b/spec/queries/applied_coupons_query_spec.rb @@ -3,10 +3,12 @@ require 'rails_helper' RSpec.describe AppliedCouponsQuery, type: :query do - subject(:applied_coupons_query) { described_class.new(organization:, pagination:, filters:) } + subject(:result) do + described_class.call(organization:, pagination:, filters:) + end let(:organization) { create(:organization) } - let(:pagination) { BaseQuery::Pagination.new } + let(:pagination) { nil } let(:filters) { BaseQuery::Filters.new(query_filters) } let(:query_filters) { {} } @@ -18,67 +20,55 @@ before { applied_coupon } - describe 'call' do - it 'returns a list of applied_coupons' do - result = applied_coupons_query.call - - aggregate_failures do - expect(result).to be_success - expect(result.applied_coupons.count).to eq(1) - expect(result.applied_coupons).to eq([applied_coupon]) - end + it 'returns a list of applied_coupons' do + aggregate_failures do + expect(result).to be_success + expect(result.applied_coupons.count).to eq(1) + expect(result.applied_coupons).to eq([applied_coupon]) end + end - context 'when customer is deleted' do - let(:customer) { create(:customer, :deleted, organization:) } - - it 'filters the applied_coupons' do - result = applied_coupons_query.call + context 'when customer is deleted' do + let(:customer) { create(:customer, :deleted, organization:) } - aggregate_failures do - expect(result).to be_success - expect(result.applied_coupons.count).to eq(0) - end + it 'filters the applied_coupons' do + aggregate_failures do + expect(result).to be_success + expect(result.applied_coupons.count).to eq(0) end end + end - context 'with pagination' do - let(:pagination) { BaseQuery::Pagination.new(page: 2, limit: 10) } - - it 'applies the pagination' do - result = applied_coupons_query.call + context 'with pagination' do + let(:pagination) { {page: 2, limit: 10} } - aggregate_failures do - expect(result).to be_success - expect(result.applied_coupons.count).to eq(0) - expect(result.applied_coupons.current_page).to eq(2) - end + it 'applies the pagination' do + aggregate_failures do + expect(result).to be_success + expect(result.applied_coupons.count).to eq(0) + expect(result.applied_coupons.current_page).to eq(2) end end + end - context 'with customer filter' do - let(:query_filters) { {external_customer_id: customer.external_id} } - - it 'applies the filter' do - result = applied_coupons_query.call + context 'with customer filter' do + let(:query_filters) { {external_customer_id: customer.external_id} } - aggregate_failures do - expect(result).to be_success - expect(result.applied_coupons.count).to eq(1) - end + it 'applies the filter' do + aggregate_failures do + expect(result).to be_success + expect(result.applied_coupons.count).to eq(1) end end + end - context 'with status filter' do - let(:query_filters) { {status: 'terminated'} } - - it 'applies the filter' do - result = applied_coupons_query.call + context 'with status filter' do + let(:query_filters) { {status: 'terminated'} } - aggregate_failures do - expect(result).to be_success - expect(result.applied_coupons.count).to eq(0) - end + it 'applies the filter' do + aggregate_failures do + expect(result).to be_success + expect(result.applied_coupons.count).to eq(0) end end end diff --git a/spec/queries/fees_query_spec.rb b/spec/queries/fees_query_spec.rb index c74609ebafb..062c2e1d986 100644 --- a/spec/queries/fees_query_spec.rb +++ b/spec/queries/fees_query_spec.rb @@ -6,7 +6,7 @@ subject(:fees_query) { described_class.new(organization:, pagination:, filters:) } let(:organization) { create(:organization) } - let(:pagination) { BaseQuery::Pagination.new } + let(:pagination) { nil } let(:filters) { BaseQuery::Filters.new(query_filters) } let(:query_filters) { {} } @@ -29,7 +29,7 @@ end context 'with pagination' do - let(:pagination) { BaseQuery::Pagination.new(page: 2, limit: 10) } + let(:pagination) { {page: 2, limit: 10} } it 'applies the pagination' do result = fees_query.call diff --git a/spec/queries/integration_collection_mappings_query_spec.rb b/spec/queries/integration_collection_mappings_query_spec.rb index 1c131899b95..4838775ed48 100644 --- a/spec/queries/integration_collection_mappings_query_spec.rb +++ b/spec/queries/integration_collection_mappings_query_spec.rb @@ -5,7 +5,7 @@ RSpec.describe IntegrationCollectionMappingsQuery, type: :query do subject(:collection_mappings_query) { described_class.new(organization:, pagination:, filters:) } - let(:pagination) { BaseQuery::Pagination.new } + let(:pagination) { nil } let(:filters) { BaseQuery::Filters.new(query_filters) } let(:query_filters) { {} } let(:membership) { create(:membership) } diff --git a/spec/queries/integration_mappings_query_spec.rb b/spec/queries/integration_mappings_query_spec.rb index 38c9c33d2d5..74d252355de 100644 --- a/spec/queries/integration_mappings_query_spec.rb +++ b/spec/queries/integration_mappings_query_spec.rb @@ -5,7 +5,7 @@ RSpec.describe IntegrationMappingsQuery, type: :query do subject(:integration_mappings_query) { described_class.new(organization:, pagination:, filters:) } - let(:pagination) { BaseQuery::Pagination.new } + let(:pagination) { nil } let(:filters) { BaseQuery::Filters.new(query_filters) } let(:query_filters) { {} } let(:membership) { create(:membership) } diff --git a/spec/queries/invoices_query_spec.rb b/spec/queries/invoices_query_spec.rb index 9da65c5cec5..6114613f391 100644 --- a/spec/queries/invoices_query_spec.rb +++ b/spec/queries/invoices_query_spec.rb @@ -4,9 +4,10 @@ RSpec.describe InvoicesQuery, type: :query do subject(:invoice_query) do - described_class.new(organization:) + described_class.new(organization:, pagination:) end + let(:pagination) { nil } let(:membership) { create(:membership) } let(:organization) { membership.organization } let(:customer_first) { create(:customer, organization:, name: 'Rick Sanchez', email: 'pickle@hotmail.com') } @@ -91,9 +92,7 @@ result = invoice_query.call( search_term: nil, status: nil, - payment_status: nil, - page: 1, - limit: 10 + payment_status: nil ) returned_ids = result.invoices.pluck(:id) @@ -109,14 +108,30 @@ end end + context 'with pagination' do + let(:pagination) { {page: 2, limit: 3} } + + it 'applies the pagination' do + result = invoice_query.call + + aggregate_failures do + expect(result).to be_success + expect(result.invoices.count).to eq(3) + expect(result.invoices.current_page).to eq(2) + expect(result.invoices.prev_page).to eq(1) + expect(result.invoices.next_page).to be_nil + expect(result.invoices.total_pages).to eq(2) + expect(result.invoices.total_count).to eq(6) + end + end + end + context 'when filtering by draft status' do it 'returns 2 invoices' do result = invoice_query.call( search_term: nil, status: 'draft', - payment_status: nil, - page: 1, - limit: 10 + payment_status: nil ) returned_ids = result.invoices.pluck(:id) @@ -137,9 +152,7 @@ result = invoice_query.call( search_term: nil, status: nil, - payment_status: 'failed', - page: 1, - limit: 10 + payment_status: 'failed' ) returned_ids = result.invoices.pluck(:id) @@ -160,9 +173,7 @@ result = invoice_query.call( search_term: nil, status: nil, - payment_status: ['succeeded', 'failed'], - page: 1, - limit: 10 + payment_status: ['succeeded', 'failed'] ) returned_ids = result.invoices.pluck(:id) @@ -183,9 +194,7 @@ result = invoice_query.call( search_term: nil, status: nil, - payment_dispute_lost: true, - page: 1, - limit: 10 + payment_dispute_lost: true ) returned_ids = result.invoices.pluck(:id) @@ -207,9 +216,7 @@ result = invoice_query.call( search_term: nil, status: nil, - payment_overdue: true, - page: 1, - limit: 10 + payment_overdue: true ) expect(result.invoices.pluck(:id)).to eq([invoice_third.id]) @@ -223,9 +230,7 @@ status: nil, filters: { invoice_type: 'credit' - }, - page: 1, - limit: 10 + } ) returned_ids = result.invoices.pluck(:id) @@ -243,9 +248,7 @@ status: nil, filters: { currency: 'USD' - }, - page: 1, - limit: 10 + } ) returned_ids = result.invoices.pluck(:id) @@ -263,9 +266,7 @@ status: nil, filters: { customer_external_id: customer_second.external_id - }, - page: 1, - limit: 10 + } ) returned_ids = result.invoices.pluck(:id) @@ -286,9 +287,7 @@ status: nil, filters: { issuing_date_from: 2.days.ago.iso8601 - }, - page: 1, - limit: 10 + } ) returned_ids = result.invoices.pluck(:id) @@ -309,9 +308,7 @@ status: nil, filters: { issuing_date_from: 'invalid_date_value' - }, - page: 1, - limit: 10 + } ) aggregate_failures do @@ -330,9 +327,7 @@ status: nil, filters: { issuing_date_to: 2.weeks.ago.iso8601 - }, - page: 1, - limit: 10 + } ) returned_ids = result.invoices.pluck(:id) @@ -352,9 +347,7 @@ status: nil, filters: { issuing_date_to: 'invalid_date_value' - }, - page: 1, - limit: 10 + } ) aggregate_failures do @@ -374,9 +367,7 @@ filters: { issuing_date_from: 2.weeks.ago.iso8601, issuing_date_to: 1.week.ago.iso8601 - }, - page: 1, - limit: 10 + } ) returned_ids = result.invoices.pluck(:id) @@ -395,9 +386,7 @@ result = invoice_query.call( search_term: invoice_fourth.id.scan(/.{10}/).first, status: nil, - payment_status: nil, - page: 1, - limit: 10 + payment_status: nil ) returned_ids = result.invoices.pluck(:id) @@ -418,9 +407,7 @@ result = invoice_query.call( search_term: invoice_first.number, status: nil, - payment_status: nil, - page: 1, - limit: 10 + payment_status: nil ) returned_ids = result.invoices.pluck(:id) @@ -441,9 +428,7 @@ result = invoice_query.call( search_term: customer_second.external_id, status: nil, - payment_status: nil, - page: 1, - limit: 10 + payment_status: nil ) returned_ids = result.invoices.pluck(:id) @@ -464,9 +449,7 @@ result = invoice_query.call( search_term: 'rick', status: nil, - payment_status: nil, - page: 1, - limit: 10 + payment_status: nil ) returned_ids = result.invoices.pluck(:id) @@ -488,9 +471,7 @@ result = invoice_query.call( search_term: 'gmail', status: nil, - payment_status: nil, - page: 1, - limit: 10 + payment_status: nil ) returned_ids = result.invoices.pluck(:id) @@ -511,9 +492,7 @@ result = invoice_query.call( customer_id: customer_second.id, search_term: '44444444', - status: nil, - page: 1, - limit: 10 + status: nil ) returned_ids = result.invoices.pluck(:id) @@ -534,9 +513,7 @@ result = invoice_query.call( customer_id: create(:customer, organization:).id, search_term: nil, - status: nil, - page: 1, - limit: 10 + status: nil ) returned_ids = result.invoices.pluck(:id) diff --git a/spec/queries/past_usage_query_spec.rb b/spec/queries/past_usage_query_spec.rb index 1a142dce442..6117d7b4112 100644 --- a/spec/queries/past_usage_query_spec.rb +++ b/spec/queries/past_usage_query_spec.rb @@ -6,7 +6,7 @@ subject(:usage_query) { described_class.new(organization:, pagination:, filters:) } let(:organization) { create(:organization) } - let(:pagination) { BaseQuery::Pagination.new } + let(:pagination) { nil } let(:filters) { BaseQuery::Filters.new(query_filters) } let(:customer) { create(:customer, organization:) } @@ -53,6 +53,32 @@ end end + context 'with pagination' do + let(:pagination) { {page: 2, limit: 2} } + + before do + create( + :invoice_subscription, + charges_from_datetime: DateTime.parse('2023-06-17T00:00:00'), + charges_to_datetime: DateTime.parse('2023-07-16T23:59:59'), + subscription: + ) + end + + it 'applies the pagination' do + result = usage_query.call + + aggregate_failures do + expect(result).to be_success + expect(result.current_page).to eq(2) + expect(result.prev_page).to eq(1) + expect(result.next_page).to be_nil + expect(result.total_pages).to eq(2) + expect(result.total_count).to eq(3) + end + end + end + context 'when external_customer_id is missing' do let(:query_filters) { {external_subscription_id: subscription.external_id} } diff --git a/spec/queries/subscriptions_query_spec.rb b/spec/queries/subscriptions_query_spec.rb index 9993db6db93..0579cb48c4b 100644 --- a/spec/queries/subscriptions_query_spec.rb +++ b/spec/queries/subscriptions_query_spec.rb @@ -6,7 +6,7 @@ subject(:subscriptions_query) { described_class.new(organization:, pagination:, filters:) } let(:organization) { create(:organization) } - let(:pagination) { BaseQuery::Pagination.new } + let(:pagination) { nil } let(:filters) { BaseQuery::Filters.new(query_filters) } let(:query_filters) { {} } @@ -29,7 +29,7 @@ end context 'with pagination' do - let(:pagination) { BaseQuery::Pagination.new(page: 2, limit: 10) } + let(:pagination) { {page: 2, limit: 10} } it 'applies the pagination' do result = subscriptions_query.call diff --git a/spec/services/data_exports/csv/invoice_fees_spec.rb b/spec/services/data_exports/csv/invoice_fees_spec.rb index c7adbc1082d..1922e2f139a 100644 --- a/spec/services/data_exports/csv/invoice_fees_spec.rb +++ b/spec/services/data_exports/csv/invoice_fees_spec.rb @@ -205,7 +205,7 @@ allow(InvoicesQuery) .to receive(:new) - .with(organization: data_export.organization) + .with(organization: data_export.organization, pagination: nil) .and_return(invoices_query) allow(invoices_query) @@ -213,8 +213,6 @@ .with( search_term:, status:, - page: nil, - limit: nil, filters: ) .and_return(query_results) diff --git a/spec/services/data_exports/csv/invoices_spec.rb b/spec/services/data_exports/csv/invoices_spec.rb index a0267486f05..95e9cfc9904 100644 --- a/spec/services/data_exports/csv/invoices_spec.rb +++ b/spec/services/data_exports/csv/invoices_spec.rb @@ -94,7 +94,7 @@ allow(InvoicesQuery) .to receive(:new) - .with(organization: data_export.organization) + .with(organization: data_export.organization, pagination: nil) .and_return(invoices_query) allow(invoices_query) @@ -102,8 +102,6 @@ .with( search_term:, status:, - page: nil, - limit: nil, filters: ) .and_return(query_results)