Skip to content

Commit

Permalink
Add soft delete for email and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksy committed May 6, 2024
1 parent ed12001 commit 9b35bc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/trento/users/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ defmodule Trento.Users.User do
|> custom_fields_changeset(attrs)
end

def delete_changeset(%__MODULE__{username: username} = user, %{deleted_at: deleted_at} = attrs) do
def delete_changeset(
%__MODULE__{username: username, email: email, email: email} = user,
%{deleted_at: deleted_at} = attrs
) do
user
|> cast(attrs, [:deleted_at])
|> validate_required(:deleted_at)
|> put_change(:username, "#{username}__#{deleted_at}")
|> put_change(:email, "#{email}__#{deleted_at}")
end

defp validate_current_password(changeset, %{password: _password} = attrs),
Expand Down
9 changes: 7 additions & 2 deletions test/trento/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,19 @@ defmodule Trento.UsersTest do
end

test "delete_user/1 deletes the user" do
%{id: user_id, username: original_username} = user = insert(:user)
%{id: user_id, username: original_username, email: original_email} = user = insert(:user)

assert {:ok, %User{}} = Users.delete_user(user)
assert {:error, :not_found} == Users.get_user(user.id)

%User{deleted_at: deleted_at, username: username} = Trento.Repo.get_by!(User, id: user_id)
%User{deleted_at: deleted_at, username: username, email: email} =
Trento.Repo.get_by!(User, id: user_id)

refute deleted_at == nil
refute username == original_username
refute email == original_email
assert username == "#{original_username}__#{deleted_at}"
assert email == "#{original_email}__#{deleted_at}"
end
end
end

0 comments on commit 9b35bc4

Please sign in to comment.