Skip to content

Commit fd8015a

Browse files
More robust enterprise plan setup (#1407)
1 parent d8d7498 commit fd8015a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

apps/core/lib/core/services/payments.ex

+9
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,15 @@ defmodule Core.Services.Payments do
675675
defp discount(amount, :yearly), do: round(9 * amount / 10) * 12
676676
defp discount(amount, _), do: amount
677677

678+
def setup_enterprise_plan(%User{} = user) do
679+
case Repo.preload(user, [account: [subscription: :plan]]) do
680+
%{account: %Account{} = account} ->
681+
with {:ok, _} <- remove_trial(account),
682+
do: setup_enterprise_plan(user.account_id)
683+
_ -> setup_enterprise_plan(user.account_id)
684+
end
685+
end
686+
678687
def setup_enterprise_plan(account_id) do
679688
plan = get_platform_plan_by_name!("Enterprise")
680689

apps/core/test/services/payments_test.exs

+13
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,19 @@ defmodule Core.Services.PaymentsTest do
869869
assert subscription.plan_id == plan.id
870870
refute subscription.external_id
871871
end
872+
873+
test "it will set up a plan for a user with a trial" do
874+
plan = insert(:platform_plan, name: "Enterprise", enterprise: true)
875+
trial = insert(:platform_plan, name: "Pro Trial", trial: true)
876+
account = insert(:account)
877+
insert(:platform_subscription, account: account, plan: trial)
878+
user = insert(:user, account: account)
879+
880+
{:ok, subscription} = Payments.setup_enterprise_plan(user)
881+
882+
assert subscription.account_id == account.id
883+
assert subscription.plan_id == plan.id
884+
end
872885
end
873886

874887
describe "#begin_trail/1" do

0 commit comments

Comments
 (0)