Skip to content

Commit

Permalink
Backfill onboarding status and ensure set on user create (#624)
Browse files Browse the repository at this point in the history
We currently default this to nil, when we should be setting the status to :new by default
  • Loading branch information
michaeljguarino authored Oct 19, 2022
1 parent cad1f71 commit 3509335
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
15 changes: 15 additions & 0 deletions apps/core/lib/core/backfill/users.ex
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion apps/core/lib/core/schema/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions apps/core/lib/core/services/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down
5 changes: 5 additions & 0 deletions apps/core/priv/repo/seeds/010_onboarding_backfill.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Botanist

seed do
Core.Backfill.Users.onboarding()
end
16 changes: 16 additions & 0 deletions apps/core/test/backfill/users_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions apps/core/test/services/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Core.Services.UsersTest do
assert user.name == "some user"
assert user.email == "[email protected]"
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])
Expand Down

0 comments on commit 3509335

Please sign in to comment.