Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(override-plan): Take into consideration children for customers_count #1338

Merged
merged 2 commits into from
Sep 22, 2023
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
4 changes: 0 additions & 4 deletions app/graphql/types/plans/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ def charges_count
object.charges.count
end

def customers_count
object.subscriptions.active.select(:customer_id).distinct.count
end

def subscriptions_count
object.subscriptions.count
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def yearly_amount_cents
amount_cents * 52
end

def customers_count
count = subscriptions.active.select(:customer_id).distinct.count
return count unless children

count + children.joins(:subscriptions).where(
subscriptions: { status: :active },
).select('distinct(customer_id)').count
end

private

def validate_code_unique
Expand Down
1 change: 1 addition & 0 deletions app/serializers/v1/plan_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def serialize
trial_period: model.trial_period,
pay_in_advance: model.pay_in_advance,
bill_charges_monthly: model.bill_charges_monthly,
customers_count: model.customers_count,
active_subscriptions_count:,
draft_invoices_count:,
parent_id: model.parent_id,
Expand Down
14 changes: 14 additions & 0 deletions spec/models/plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,18 @@
end
end
end

describe '#customers_count' do
let(:customer) { create(:customer) }
let(:plan) { create(:plan) }

it 'returns the number of impacted customers' do
create(:subscription, customer:, plan:)
overridden_plan = create(:plan, parent_id: plan.id)
customer2 = create(:customer, organization: plan.organization)
create(:subscription, customer: customer2, plan: overridden_plan)

expect(plan.customers_count).to eq(2)
end
end
end
11 changes: 9 additions & 2 deletions spec/serializers/v1/plan_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
subject(:serializer) { described_class.new(plan, root_name: 'plan', includes: %i[charges taxes]) }

let(:plan) { create(:plan) }
let(:customer) { create(:customer, organization: plan.organization) }
let(:subscription) { create(:subscription, customer:, plan:) }
let(:charge) { create(:standard_charge, plan:) }

before { charge }
before { subscription && charge }

it 'serializes the object', :aggregate_failures do
overridden_plan = create(:plan, parent_id: plan.id)
customer2 = create(:customer, organization: plan.organization)
create(:subscription, customer: customer2, plan: overridden_plan)

result = JSON.parse(serializer.to_json)

expect(result['plan']).to include(
Expand All @@ -26,7 +32,8 @@
'trial_period' => plan.trial_period,
'pay_in_advance' => plan.pay_in_advance,
'bill_charges_monthly' => plan.bill_charges_monthly,
'active_subscriptions_count' => 0,
'customers_count' => 2,
'active_subscriptions_count' => 1,
'draft_invoices_count' => 0,
'parent_id' => nil,
'taxes' => [],
Expand Down
Loading