def before_action(:delete, _struct, opts), do: :ok, nil # prevent actual delete def before_action( , struct, opts), do: :ok, struct def after_action( , _, _), do: nil end
def run(attrs) do Uni.new() |> Uni.put(:original_attrs, attrs) |> add_step(:build_changeset, fn ctx -> changeset = User.registration_changeset(ctx.data.original_attrs) :ok, changeset end) |> add_step(:insert_user, insert(&1.data.build_changeset)) |> add_step(:assign_default_role, fn ctx -> user = ctx.data.insert_user updated_changeset = User.changeset(user, %role: "verified_member") :ok, updated_changeset end) |> add_step(:update_role, update(&1.data.assign_default_role)) |> add_step(:send_welcome, fn ctx -> # Imagine this calls an email service IO.inspect("Sending welcome to #ctx.data.update_role.email") :ok, %delivered: true end) |> Uni.execute() end end uni ecto plugin
You might be thinking: "I can just add where(tenant_id: ^current_user.tenant_id) to every query." def before_action(:delete, _struct, opts), do: :ok, nil #
schema "posts" do field :title, :string field :body, :string timestamps() end struct def after_action(
Let’s get our hands dirty. Add the necessary dependencies to your mix.exs :