Skip to content

Commit

Permalink
Autotrial new accounts (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Jul 13, 2023
1 parent b8b928f commit 380745c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apps/core/lib/core/schema/platform_plan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ defmodule Core.Schema.PlatformPlan do
timestamps()
end

def for_name(query \\ __MODULE__, name), do: from(p in query, where: p.name == ^name)

def visible(query \\ __MODULE__), do: from(p in query, where: p.visible)

def ordered(query \\ __MODULE__, order \\ [asc: :name]),
Expand Down
6 changes: 6 additions & 0 deletions apps/core/lib/core/services/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ defmodule Core.Services.Accounts do
|> Ecto.Changeset.change(%{account_id: id})
|> Core.Repo.update()
end)
|> add_operation(:trial, fn %{account: account} ->
case Payments.trial_exists?() do
true -> Payments.begin_trial(account)
_ -> {:ok, nil}
end
end)
|> execute()
end

Expand Down
10 changes: 10 additions & 0 deletions apps/core/lib/core/services/payments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ defmodule Core.Services.Payments do
do: Core.Repo.get_by(Subscription, installation_id: id)
end

@doc """
Returns whether the trial plan has been created
"""
@spec trial_exists?() :: boolean
def trial_exists?() do
Core.conf(:trial_plan)
|> PlatformPlan.for_name()
|> Core.Repo.exists?()
end

@doc """
Adds the trial plan to a user's account
"""
Expand Down
6 changes: 5 additions & 1 deletion apps/core/test/services/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Core.Services.UsersTest do

describe "#create_user" do
test "Users can be created" do
trial = trial_plan()

{:ok, user} = Users.create_user(%{
name: "some user",
password: "superstrongpassword",
Expand All @@ -18,10 +20,12 @@ defmodule Core.Services.UsersTest do
assert user.onboarding_checklist.status == :new
assert Timex.after?(user.email_confirm_by, Timex.now())

%{account: account} = Core.Repo.preload(user, [:account])
%{account: account} = Core.Repo.preload(user, [account: [subscription: :plan]])
assert account.name == user.email
assert account.root_user_id == user.id

assert account.subscription.plan.id == trial.id

assert_receive {:event, %PubSub.UserCreated{item: ^user}}
end
end
Expand Down

0 comments on commit 380745c

Please sign in to comment.