Skip to content

Commit

Permalink
feat: Add previous and next plan code to subscription object
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Sep 14, 2022
1 parent 5bc8e0c commit 3ec6cc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/serializers/v1/subscription_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def serialize
terminated_at: model.terminated_at&.iso8601,
canceled_at: model.canceled_at&.iso8601,
created_at: model.created_at.iso8601,
previous_plan_code: model.previous_subscription&.plan&.code,
next_plan_code: model.next_subscription&.plan&.code,
}
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/requests/api/v1/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
expect(result[:name]).to eq('subscription name')
expect(result[:started_at]).to be_present
expect(result[:billing_time]).to eq('anniversary')
expect(result[:previous_plan_code]).to be_nil
expect(result[:next_plan_code]).to be_nil
end

context 'with invalid params' do
Expand Down Expand Up @@ -119,6 +121,33 @@
expect(records.first[:lago_id]).to eq(subscription1.id)
end

context 'with next and previous plans' do
it 'returns plans code' do
previous_subscription = create(
:subscription,
customer: customer,
plan: create(:plan, organization: organization),
status: :terminated,
)

next_subscription = create(
:subscription,
customer: customer,
plan: create(:plan, organization: organization),
status: :pending,
)

subscription1.update!(previous_subscription: previous_subscription, next_subscriptions: [next_subscription])

get_with_token(organization, "/api/v1/subscriptions?external_customer_id=#{customer.external_id}")

expect(response).to have_http_status(:success)
subscription = JSON.parse(response.body, symbolize_names: true)[:subscriptions].first
expect(subscription[:previous_plan_code]).to eq(previous_subscription.plan.code)
expect(subscription[:next_plan_code]).to eq(next_subscription.plan.code)
end
end

context 'with pagination' do
let(:plan2) { create(:plan, organization: organization, amount_cents: 30000) }
let(:subscription2) { create(:subscription, customer: customer, plan: plan2) }
Expand Down

0 comments on commit 3ec6cc8

Please sign in to comment.