From 3509335067d828c4262741327cd1ef11bdd78d20 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Wed, 19 Oct 2022 11:03:03 -0400 Subject: [PATCH] Backfill onboarding status and ensure set on user create (#624) We currently default this to nil, when we should be setting the status to :new by default --- apps/core/lib/core/backfill/users.ex | 15 +++++++++++++++ apps/core/lib/core/schema/user.ex | 2 +- apps/core/lib/core/services/users.ex | 1 + .../priv/repo/seeds/010_onboarding_backfill.exs | 5 +++++ apps/core/test/backfill/users_test.exs | 16 ++++++++++++++++ apps/core/test/services/users_test.exs | 1 + 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 apps/core/lib/core/backfill/users.ex create mode 100644 apps/core/priv/repo/seeds/010_onboarding_backfill.exs create mode 100644 apps/core/test/backfill/users_test.exs diff --git a/apps/core/lib/core/backfill/users.ex b/apps/core/lib/core/backfill/users.ex new file mode 100644 index 000000000..606a75cd4 --- /dev/null +++ b/apps/core/lib/core/backfill/users.ex @@ -0,0 +1,15 @@ +defmodule Core.Backfill.Users do + alias Core.{Schema.User} + + def onboarding() do + User + |> User.ordered(asc: :id) + |> Core.Repo.stream(method: :keyset) + |> Core.throttle() + |> Enum.each(fn user -> + # don't show onboarding to existing users + {:ok, _} = User.changeset(user, %{onboarding_checklist: %{dismissed: true, status: :finished}}) + |> Core.Repo.update() + end) + end +end diff --git a/apps/core/lib/core/schema/user.ex b/apps/core/lib/core/schema/user.ex index f1dbe5d38..499928bf5 100644 --- a/apps/core/lib/core/schema/user.ex +++ b/apps/core/lib/core/schema/user.ex @@ -263,7 +263,7 @@ defmodule Core.Schema.User do end defimpl Jason.Encoder, for: Core.Schema.User do - @ignore ~w(password password_hash jwt)a + @ignore ~w(password password_hash jwt onboarding_checklist)a def encode(struct, opts) do Piazza.Ecto.Schema.mapify(struct) diff --git a/apps/core/lib/core/services/users.ex b/apps/core/lib/core/services/users.ex index e10876bf8..7d01cfedf 100644 --- a/apps/core/lib/core/services/users.ex +++ b/apps/core/lib/core/services/users.ex @@ -256,6 +256,7 @@ defmodule Core.Services.Users do confirm_by = Timex.now() |> Timex.shift(days: 7) %User{email_confirm_by: confirm_by} |> User.changeset(attrs) + |> User.changeset(%{onboarding_checklist: %{status: :new}}) |> Core.Repo.insert() end) |> add_operation(:user, fn %{pre: user} -> diff --git a/apps/core/priv/repo/seeds/010_onboarding_backfill.exs b/apps/core/priv/repo/seeds/010_onboarding_backfill.exs new file mode 100644 index 000000000..9db7e1c38 --- /dev/null +++ b/apps/core/priv/repo/seeds/010_onboarding_backfill.exs @@ -0,0 +1,5 @@ +import Botanist + +seed do + Core.Backfill.Users.onboarding() +end diff --git a/apps/core/test/backfill/users_test.exs b/apps/core/test/backfill/users_test.exs new file mode 100644 index 000000000..496f0ab9b --- /dev/null +++ b/apps/core/test/backfill/users_test.exs @@ -0,0 +1,16 @@ +defmodule Core.Backfill.UsersTest do + use Core.SchemaCase, async: true + alias Core.Backfill.Users + + describe "#onboarding/0" do + test "it will set onboarding_checklist structs" do + users = insert_list(3, :user) + + Users.onboarding() + + for user <- users do + assert refetch(user).onboarding_checklist.dismissed + end + end + end +end diff --git a/apps/core/test/services/users_test.exs b/apps/core/test/services/users_test.exs index 428d3a111..3942ade4f 100644 --- a/apps/core/test/services/users_test.exs +++ b/apps/core/test/services/users_test.exs @@ -15,6 +15,7 @@ defmodule Core.Services.UsersTest do assert user.name == "some user" assert user.email == "something@example.com" assert user.password_hash + assert user.onboarding_checklist.status == :new assert Timex.after?(user.email_confirm_by, Timex.now()) %{account: account} = Core.Repo.preload(user, [:account])