defmodule Mv.Membership.JoinRequest.Changes.RegenerateConfirmationToken do @moduledoc """ Sets a new confirmation token hash and expiry on an existing join request (resend flow). Used when the user submits the join form again with the same email while a request is still pending_confirmation. Internal use only (domain calls with authorize?: false). """ use Ash.Resource.Change alias Mv.Membership.JoinRequest @confirmation_validity_hours 24 @spec change(Ash.Changeset.t(), keyword(), Ash.Resource.Change.context()) :: Ash.Changeset.t() def change(changeset, _opts, _context) do token = Ash.Changeset.get_argument(changeset, :confirmation_token) if is_binary(token) and token != "" do now = DateTime.utc_now() expires_at = DateTime.add(now, @confirmation_validity_hours, :hour) changeset |> Ash.Changeset.force_change_attribute( :confirmation_token_hash, JoinRequest.hash_confirmation_token(token) ) |> Ash.Changeset.force_change_attribute(:confirmation_token_expires_at, expires_at) |> Ash.Changeset.force_change_attribute(:confirmation_sent_at, now) else changeset end end end