Skip to content

Commit

Permalink
Add the onboarding checklist data to the user object (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
floreks authored Oct 18, 2022
1 parent ca871d1 commit 3e9e3c0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
17 changes: 17 additions & 0 deletions apps/core/lib/core/schema/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ defmodule Core.Schema.User do
installed: 2,
active: 3

defenum OnboardingChecklistStatus,
new: 0,
configured: 1,
console_installed: 2,
finished: 3

schema "users" do
field :name, :string
field :email, :string
Expand Down Expand Up @@ -59,6 +65,11 @@ defmodule Core.Schema.User do
boolean_fields [:admin]
end

embeds_one :onboarding_checklist, OnboardingChecklist, on_replace: :update do
field :status, OnboardingChecklistStatus, default: :new
field :dismissed, :boolean, default: false
end

belongs_to :account, Account

has_one :publisher, Publisher, foreign_key: :owner_id
Expand Down Expand Up @@ -149,6 +160,7 @@ defmodule Core.Schema.User do
|> base_changeset(attrs, mode)
|> cast_embed(:address)
|> cast_embed(:roles, with: &roles_changeset/2)
|> cast_embed(:onboarding_checklist, with: &onboarding_checklist_changeset/2)
end

def invite_changeset(model, attrs \\ %{}), do: base_changeset(model, attrs, :primary)
Expand Down Expand Up @@ -198,6 +210,11 @@ defmodule Core.Schema.User do
|> cast(attrs, [:admin])
end

def onboarding_checklist_changeset(model, attrs \\ %{}) do
model
|> cast(attrs, [:status, :dismissed])
end

def service_account_changeset(model, attrs \\ %{}) do
model
|> cast(attrs, [:name, :email])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Core.Repo.Migrations.AddUserOnboardingChecklist do
use Ecto.Migration

def change do
alter table(:users) do
add :onboarding_checklist, :map
end
end
end
17 changes: 17 additions & 0 deletions apps/core/test/services/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ defmodule Core.Services.UsersTest do

{:error, _} = Users.update_user(%{name: "real user", roles: %{admin: true}}, user)
end

test "users can update their onboarding checklist" do
onboarding_checklist = %{status: :new, dismissed: false}
{:ok, user} = Users.create_user(%{
name: "some user",
password: "superstrongpassword",
email: "something@example.com",
onboarding_checklist: onboarding_checklist
})

{:ok, updated} = Users.update_user(%{onboarding_checklist: %{status: :finished}}, user)

assert updated.onboarding_checklist.status == :finished
assert updated.onboarding_checklist.dismissed == onboarding_checklist.dismissed

assert_receive {:event, %PubSub.UserUpdated{item: ^updated}}
end
end

describe "#update_user/3" do
Expand Down
55 changes: 34 additions & 21 deletions apps/graphql/lib/graphql/schema/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@ defmodule GraphQl.Schema.User do
ecto_enum :login_method, Schema.User.LoginMethod
ecto_enum :user_event_status, Schema.UserEvent.Status
ecto_enum :onboarding_state, Schema.User.OnboardingStatus
ecto_enum :onboarding_checklist_state, Schema.User.OnboardingChecklistStatus

input_object :user_attributes do
field :name, :string
field :email, :string
field :password, :string
field :avatar, :upload_or_url
field :onboarding, :onboarding_state
field :login_method, :login_method
field :roles, :roles_attributes
field :confirm, :string
field :name, :string
field :email, :string
field :password, :string
field :avatar, :upload_or_url
field :onboarding, :onboarding_state
field :onboarding_checklist, :onboarding_checklist_attributes
field :login_method, :login_method
field :roles, :roles_attributes
field :confirm, :string
end

input_object :roles_attributes do
field :admin, :boolean
end

input_object :onboarding_checklist_attributes do
field :status, :onboarding_checklist_state
field :dismissed, :boolean
end

input_object :publisher_attributes do
field :name, :string
field :description, :string
Expand Down Expand Up @@ -72,19 +79,20 @@ defmodule GraphQl.Schema.User do
end

object :user do
field :id, non_null(:id)
field :name, non_null(:string)
field :email, non_null(:string)
field :phone, :string
field :address, :address
field :login_method, :login_method
field :onboarding, :onboarding_state
field :default_queue_id, :id
field :service_account, :boolean
field :email_confirmed, :boolean
field :email_confirm_by, :datetime
field :provider, :provider
field :roles, :roles
field :id, non_null(:id)
field :name, non_null(:string)
field :email, non_null(:string)
field :phone, :string
field :address, :address
field :login_method, :login_method
field :onboarding, :onboarding_state
field :onboarding_checklist, :onboarding_checklist
field :default_queue_id, :id
field :service_account, :boolean
field :email_confirmed, :boolean
field :email_confirm_by, :datetime
field :provider, :provider
field :roles, :roles

field :bound_roles, list_of(:role), resolve: fn
%{id: id}, _, %{context: %{current_user: %{id: id} = current_user}} -> {:ok, Core.Schema.User.roles(current_user)}
Expand Down Expand Up @@ -124,6 +132,11 @@ defmodule GraphQl.Schema.User do
field :admin, :boolean
end

object :onboarding_checklist do
field :status, :onboarding_checklist_state
field :dismissed, :boolean
end

object :impersonation_policy do
field :id, non_null(:id)
field :bindings, list_of(:impersonation_policy_binding), resolve: dataloader(User)
Expand Down

0 comments on commit 3e9e3c0

Please sign in to comment.