lib/ident/user_code/model.ex

defmodule Rivet.Ident.UserCode do
  @moduledoc """
  Schema for representing and working with a Ident.UserCode.
  """
  use TypedEctoSchema
  use Rivet.Ecto.Model, id_type: :intid
  alias Rivet.Ident

  typed_schema "user_codes" do
    belongs_to(:user, Ident.User, type: :binary_id, foreign_key: :user_id)
    field(:type, Rivet.Utils.Ecto.Atom)
    field(:code, :string)
    field(:meta, :map, default: %{})
    field(:expires, :utc_datetime)
    timestamps()
  end

  use Rivet.Ecto.Collection,
    not_found: :atom,
    required: [:user_id, :code, :type, :expires],
    update: [:meta],
    foreign_keys: [:user_id]
end