mirror of
https://codeberg.org/JasterV/spazio-solazzo.git
synced 2026-04-26 18:20:03 +00:00
38 lines
1.3 KiB
Elixir
38 lines
1.3 KiB
Elixir
defmodule SpazioSolazzo.Accounts.User.Changes.ValidateRegistrationFields do
|
|
@moduledoc """
|
|
Conditionally validates that name and phone_number are present for new user registrations.
|
|
For existing users (upserts), these fields are not required.
|
|
"""
|
|
use Ash.Resource.Change
|
|
|
|
@impl true
|
|
def change(changeset, _opts, _context) do
|
|
email = Ash.Changeset.get_attribute(changeset, :email)
|
|
|
|
case SpazioSolazzo.Accounts.get_user_by_email(email, authorize?: false) do
|
|
{:ok, %{phone_number: phone, name: name}} ->
|
|
changeset
|
|
|> Ash.Changeset.force_change_attribute(:name, name)
|
|
|> Ash.Changeset.force_change_attribute(:phone_number, phone)
|
|
|
|
_ ->
|
|
name = Ash.Changeset.get_argument(changeset, :name)
|
|
phone = Ash.Changeset.get_argument(changeset, :phone_number)
|
|
|
|
changeset
|
|
|> validate_required_for_registration(:name, name)
|
|
|> validate_required_for_registration(:phone_number, phone)
|
|
end
|
|
end
|
|
|
|
defp validate_required_for_registration(changeset, field, value) do
|
|
if is_nil(value) || value == "" do
|
|
Ash.Changeset.add_error(
|
|
changeset,
|
|
Ash.Error.Changes.Required.exception(field: field, type: :argument)
|
|
)
|
|
else
|
|
Ash.Changeset.change_attribute(changeset, field, value)
|
|
end
|
|
end
|
|
end
|