From 380745c7ac4f6d480c6ec702a4fd9c5716942d1f Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Thu, 13 Jul 2023 15:11:34 -0400 Subject: [PATCH] Autotrial new accounts (#1157) --- apps/core/lib/core/schema/platform_plan.ex | 2 ++ apps/core/lib/core/services/accounts.ex | 6 ++++++ apps/core/lib/core/services/payments.ex | 10 ++++++++++ apps/core/test/services/users_test.exs | 6 +++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/apps/core/lib/core/schema/platform_plan.ex b/apps/core/lib/core/schema/platform_plan.ex index b14bfd911..a8db7f4b3 100644 --- a/apps/core/lib/core/schema/platform_plan.ex +++ b/apps/core/lib/core/schema/platform_plan.ex @@ -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]), diff --git a/apps/core/lib/core/services/accounts.ex b/apps/core/lib/core/services/accounts.ex index 8eed7e728..a5418c7af 100644 --- a/apps/core/lib/core/services/accounts.ex +++ b/apps/core/lib/core/services/accounts.ex @@ -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 diff --git a/apps/core/lib/core/services/payments.ex b/apps/core/lib/core/services/payments.ex index 9c1a92169..666b535a1 100644 --- a/apps/core/lib/core/services/payments.ex +++ b/apps/core/lib/core/services/payments.ex @@ -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 """ diff --git a/apps/core/test/services/users_test.exs b/apps/core/test/services/users_test.exs index 6cfc557bd..43a2c39a4 100644 --- a/apps/core/test/services/users_test.exs +++ b/apps/core/test/services/users_test.exs @@ -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", @@ -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