From 3ec6cc8c178bd2da9284db7dab5e12823cedf41c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Semp=C3=A9?= Date: Wed, 14 Sep 2022 11:19:44 +0200 Subject: [PATCH] feat: Add previous and next plan code to subscription object --- app/serializers/v1/subscription_serializer.rb | 2 ++ spec/requests/api/v1/subscriptions_spec.rb | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/serializers/v1/subscription_serializer.rb b/app/serializers/v1/subscription_serializer.rb index 36069b68a07..bdd3913111c 100644 --- a/app/serializers/v1/subscription_serializer.rb +++ b/app/serializers/v1/subscription_serializer.rb @@ -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 diff --git a/spec/requests/api/v1/subscriptions_spec.rb b/spec/requests/api/v1/subscriptions_spec.rb index cc1686a42ee..bd241c22f9e 100644 --- a/spec/requests/api/v1/subscriptions_spec.rb +++ b/spec/requests/api/v1/subscriptions_spec.rb @@ -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 @@ -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) }