Skip to content

Commit

Permalink
Remove logic for charges on terminated previous subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Aug 4, 2022
1 parent 0c1287a commit 847ac9e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 279 deletions.
6 changes: 0 additions & 6 deletions app/services/subscriptions/dates/monthly_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ def compute_from_date(date = base_date)
subscription.anniversary? ? previous_anniversary_day(date) : date.beginning_of_month
end

def upgraded_charges_from_date(from_date)
return from_date.beginning_of_month if subscription.calendar?

previous_anniversary_day(from_date)
end

private

def compute_base_date
Expand Down
6 changes: 0 additions & 6 deletions app/services/subscriptions/dates/weekly_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ def previous_anniversary_day(date)
def subscription_day_name
@subscription_day_name ||= subscription_date.strftime('%A').downcase.to_sym
end

def upgraded_charges_from_date(from_date)
return from_date.beginning_of_week if calendar?

from_date.prev_occurring(subscription_day_name)
end
end
end
end
10 changes: 0 additions & 10 deletions app/services/subscriptions/dates/yearly_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ def previous_anniversary_day(date)

build_date(year, month, day)
end

def upgraded_charges_from_date(from_date)
if subscription.previous_subscription.plan.bill_charges_monthly?
monthly_service.upgraded_charges_from_date(from_date)
elsif calendar?
from_date.beginning_of_year
else
previous_anniversary_day(from_date)
end
end
end
end
end
30 changes: 4 additions & 26 deletions app/services/subscriptions/dates_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ def to_date

@to_date = compute_to_date

if subscription.terminated? && @to_date > subscription.terminated_at
# NOTE: When subscription is terminated, we cannot generate an invoice for a period after the termination
@to_date = if %i[pending active].include?(subscription.next_subscription&.status&.to_sym)
subscription.terminated_at.to_date - 1.day # TODO: check with multiple plan, and upgrade
else
subscription.terminated_at.to_date
end

# TODO: from_date / to_date of invoices should be timestamps so that to_date = subscription.terminated_at
end
@to_date = subscription.terminated_at.to_date if subscription.terminated? && @to_date > subscription.terminated_at

# NOTE: When price plan is configured as `pay_in_advance`, subscription creation will be
# billed immediatly. An invoice must be generated for it with only the subscription fee.
Expand All @@ -63,16 +54,10 @@ def to_date
end

def charges_from_date
charges_from_date = compute_charges_from_date

charges_from_date = subscription.started_at.to_date if charges_from_date < subscription.started_at

# NOTE: when upgrading, we bill the fees from the start of the complete period (from previous plan)
# and not only from the start of the current subscription
# TODO: This should be chalenged
return upgraded_charges_from_date(charges_from_date) if first_period_after_upgrade?
date = compute_charges_from_date
date = subscription.started_at.to_date if date < subscription.started_at

charges_from_date
date
end

def next_end_of_period(date)
Expand Down Expand Up @@ -103,13 +88,6 @@ def build_date(year, month, day)
Date.new(year, month, day)
end

def first_period_after_upgrade?
return false unless subscription.previous_subscription
return false unless subscription.previous_subscription.upgraded?

subscription.fees.charge_kind.none?
end

def compute_from_date
raise NotImplementedError
end
Expand Down
66 changes: 6 additions & 60 deletions spec/services/subscriptions/dates/monthly_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,6 @@
it 'returns the termination date' do
expect(result).to eq(subscription.terminated_at.to_date.to_s)
end

context 'with next subscription' do
let(:next_subscription) do
create(:subscription, previous_subscription: subscription)
end

before { next_subscription }

it 'returns the day before the termination date' do
expect(result).to eq((subscription.terminated_at.to_date - 1.day).to_s)
end
end
end
end

Expand Down Expand Up @@ -220,18 +208,6 @@
it 'returns the termination date' do
expect(result).to eq(subscription.terminated_at.to_date.to_s)
end

context 'with next subscription' do
let(:next_subscription) do
create(:subscription, previous_subscription: subscription)
end

before { next_subscription }

it 'returns the day before the termination date' do
expect(result).to eq((subscription.terminated_at.to_date - 1.day).to_s)
end
end
end
end
end
Expand All @@ -247,26 +223,11 @@
expect(result).to eq(date_service.from_date.to_s)
end

context 'when subscription is upgraded' do
let(:previous_plan) do
create(:plan, amount_cents: plan.amount_cents - 1)
end

let(:previous_subscription) do
create(
:subscription,
plan: previous_plan,
status: :terminated,
terminated_at: started_at,
)
end

context 'when subscription started in the middle of a period' do
let(:started_at) { DateTime.parse('03 Mar 2022') }

before { subscription.update!(previous_subscription: previous_subscription) }

it 'returns the beginning of the month' do
expect(result).to eq('2022-03-01')
it 'returns the start date' do
expect(result).to eq(subscription.started_at.to_date.to_s)
end
end
end
Expand All @@ -279,26 +240,11 @@
expect(result).to eq(date_service.from_date.to_s)
end

context 'when subscription is upgraded' do
let(:previous_plan) do
create(:plan, amount_cents: plan.amount_cents - 1)
end

let(:previous_subscription) do
create(
:subscription,
plan: previous_plan,
status: :terminated,
terminated_at: started_at,
)
end

context 'when subscription started in the middle of a period' do
let(:started_at) { DateTime.parse('03 Mar 2022') }

before { subscription.update!(previous_subscription: previous_subscription) }

it 'returns the beginning of the monthly period' do
expect(result).to eq('2022-03-02')
it 'returns the start date' do
expect(result).to eq(subscription.started_at.to_date.to_s)
end
end
end
Expand Down
69 changes: 8 additions & 61 deletions spec/services/subscriptions/dates/weekly_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,18 @@

context 'when subscription is just terminated' do
let(:billing_date) { DateTime.parse('07 Mar 2022') }
let(:terminated_at) { DateTime.parse('02 Mar 2022') }

before do
subscription.update!(
status: :terminated,
terminated_at: DateTime.parse('02 Mar 2022'),
terminated_at: terminated_at,
)
end

it 'returns the termination date' do
expect(result).to eq(subscription.terminated_at.to_date.to_s)
end

context 'with next subscription' do
let(:next_subscription) do
create(:subscription, previous_subscription: subscription)
end

before { next_subscription }

it 'returns the day before the termination date' do
expect(result).to eq((subscription.terminated_at.to_date - 1.day).to_s)
end
end
end
end

Expand Down Expand Up @@ -178,18 +167,6 @@
it 'returns the termination date' do
expect(result).to eq(subscription.terminated_at.to_date.to_s)
end

context 'with next subscription' do
let(:next_subscription) do
create(:subscription, previous_subscription: subscription)
end

before { next_subscription }

it 'returns the day before the termination date' do
expect(result).to eq((subscription.terminated_at.to_date - 1.day).to_s)
end
end
end
end
end
Expand All @@ -204,26 +181,11 @@
expect(result).to eq(date_service.from_date.to_s)
end

context 'when subscription is upgraded' do
let(:previous_plan) do
create(:plan, amount_cents: plan.amount_cents - 1)
end

let(:previous_subscription) do
create(
:subscription,
plan: previous_plan,
status: :terminated,
terminated_at: started_at,
)
end

context 'when subscription started in the middle of a period' do
let(:started_at) { DateTime.parse('03 Mar 2022') }

before { subscription.update!(previous_subscription: previous_subscription) }

it 'returns the beginning of the week' do
expect(result).to eq('2022-02-28')
it 'returns the start date' do
expect(result).to eq(subscription.started_at.to_date.to_s)
end
end
end
Expand All @@ -236,26 +198,11 @@
expect(result).to eq(date_service.from_date.to_s)
end

context 'when subscription is upgraded' do
let(:previous_plan) do
create(:plan, amount_cents: plan.amount_cents - 1)
end

let(:previous_subscription) do
create(
:subscription,
plan: previous_plan,
status: :terminated,
terminated_at: started_at,
)
end

context 'when subscription started in the middle of a period' do
let(:started_at) { DateTime.parse('03 Mar 2022') }

before { subscription.update!(previous_subscription: previous_subscription) }

it 'returns the beginning of weekly period' do
expect(result).to eq('2022-03-01')
it 'returns the start date' do
expect(result).to eq(subscription.started_at.to_date.to_s)
end
end
end
Expand Down
Loading

0 comments on commit 847ac9e

Please sign in to comment.