Skip to content

Commit

Permalink
feat: Return downgrade plan date instead of next and previous externa…
Browse files Browse the repository at this point in the history
…l id
  • Loading branch information
rsempe committed Sep 15, 2022
1 parent 1c916b5 commit 93fd814
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
6 changes: 1 addition & 5 deletions app/graphql/types/subscriptions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ def next_name
end

def next_pending_start_date
return unless object.next_subscription
return unless object.next_subscription.pending?

::Subscriptions::DatesService.new_instance(object, Time.zone.today)
.next_end_of_period(Time.zone.today) + 1.day
object.downgrade_plan_date
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ def validate_external_id
used_ids = organization.subscriptions.active.pluck(:external_id)
errors.add(:external_id, :value_already_exists) if used_ids&.include?(external_id)
end

def downgrade_plan_date
return unless next_subscription
return unless next_subscription.pending?

::Subscriptions::DatesService.new_instance(self, Time.zone.today)
.next_end_of_period(Time.zone.today) + 1.day
end
end
3 changes: 1 addition & 2 deletions app/serializers/v1/subscription_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def serialize
created_at: model.created_at.iso8601,
previous_plan_code: model.previous_subscription&.plan&.code,
next_plan_code: model.next_subscription&.plan&.code,
previous_external_id: model.previous_subscription&.external_id,
next_external_id: model.next_subscription&.external_id,
downgrade_plan_date: model.downgrade_plan_date&.iso8601,
}
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,30 @@
end
end
end

describe '#downgrade_plan_date' do
let(:subscription) { create(:subscription) }

context 'without next subscription' do
it 'returns nil' do
expect(subscription.downgrade_plan_date).to be_nil
end
end

context 'without pending next subscription' do
it 'returns nil' do
create(:subscription, previous_subscription: subscription, status: :active)
expect(subscription.downgrade_plan_date).to be_nil
end
end

it 'returns the date when the plan will be downgraded' do
current_date = DateTime.parse('20 Jun 2022')
create(:subscription, previous_subscription: subscription, status: :pending)

travel_to(current_date) do
expect(subscription.downgrade_plan_date).to eq(Date.parse('1 Jul 2022'))
end
end
end
end
16 changes: 9 additions & 7 deletions spec/requests/api/v1/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
expect(json[:subscription][:billing_time]).to eq('anniversary')
expect(json[:subscription][:previous_plan_code]).to be_nil
expect(json[:subscription][:next_plan_code]).to be_nil
expect(json[:subscription][:previous_external_id]).to be_nil
expect(json[:subscription][:next_external_id]).to be_nil
expect(json[:subscription][:downgrade_plan_date]).to be_nil
end

context 'with invalid params' do
Expand Down Expand Up @@ -141,12 +140,15 @@
expect(subscription[:next_plan_code]).to eq(next_subscription.plan.code)
end

it 'returns next and previous external ids' do
get_with_token(organization, "/api/v1/subscriptions?external_customer_id=#{customer.external_id}")
it 'returns the downgrade plan date' do
current_date = DateTime.parse('20 Jun 2022')

subscription = json[:subscriptions].first
expect(subscription[:previous_external_id]).to eq(previous_subscription.external_id)
expect(subscription[:next_external_id]).to eq(next_subscription.external_id)
travel_to(current_date) do
get_with_token(organization, "/api/v1/subscriptions?external_customer_id=#{customer.external_id}")

subscription = json[:subscriptions].first
expect(subscription[:downgrade_plan_date]).to eq('2022-07-01')
end
end
end

Expand Down

0 comments on commit 93fd814

Please sign in to comment.