@zachdaniel the provided solution works well expect when it comes to ash_double_entry
Balance
resource. Is there extra work to do with ash_double_entry
package?
When I pass a tenant
at the time of transfer like the following, it works:
Kamaro.Ledger.transfer(transfer_attrs, actor: user, tenant: user.current_tenant.domain)
But when I remove the tenant
, the preparation and the change are not seeing the actor
. Thus, failing with the error:
** (Ash.Error.Unknown) Unknown Error
* %KeyError{key: :current_tenant, term: nil, message: "key :current_tenant not found in: nil\n\nIf you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map"}
(kamaro 0.1.0) lib/kamaro/preparations/set_tenant_from_actor.ex:5: Kamaro.Preparations.SetTenantFromActor.prepare/3
(ash 3.2.4) lib/ash/query/query.ex:704: anonymous fn/6 in Ash.Query.run_preparations/6
Here is the balance and the transfer resources.
Balance Resource
defmodule Kamaro.Ledger.Resources.Balance do
use Ash.Resource,
domain: Kamaro.Ledger,
data_layer: AshPostgres.DataLayer,
extensions: [AshDoubleEntry.Balance],
authorizers: [Ash.Policy.Authorizer]
postgres do
table "ledger_balances"
repo Kamaro.Repo
end
preparations do
prepare Kamaro.Preparations.SetTenantFromActor
end
changes do
change Kamaro.Changes.SetTenantFromActor
end
multitenancy do
strategy :context
end
balance do
# configure the other resources it will interact with
transfer_resource(Kamaro.Ledger.Resources.Transfer)
account_resource(Kamaro.Ledger.Resources.Account)
end
actions do
read :read do
primary? true
# configure keyset pagination for streaming
pagination keyset?: true, required?: false
end
end
policies do
policy action_type(:create), authorize_if: Kamaro.Checks.Can
policy action_type(:read), authorize_if: Kamaro.Checks.Can
policy action_type(:update), authorize_if: Kamaro.Checks.Can
policy action_type(:destroy), authorize_if: Kamaro.Checks.Can
end
end
Transfer Resource
defmodule Kamaro.Ledger.Resources.Transfer do
use Ash.Resource,
domain: Kamaro.Ledger,
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
extensions: [AshDoubleEntry.Transfer]
postgres do
table "ledger_transfers"
repo Kamaro.Repo
end
preparations do
prepare Kamaro.Preparations.SetTenantFromActor
end
changes do
change Kamaro.Changes.SetTenantFromActor
end
multitenancy do
strategy :context
end
actions do
defaults [:read]
end
transfer do
account_resource(Kamaro.Ledger.Resources.Account)
balance_resource(Kamaro.Ledger.Resources.Balance)
end
policies do
policy action_type(:create), authorize_if: Kamaro.Checks.Can
policy action_type(:read), authorize_if: Kamaro.Checks.Can
policy action_type(:update), authorize_if: Kamaro.Checks.Can
policy action_type(:destroy), authorize_if: Kamaro.Checks.Can
end
end