I have to build admin history similar to Django.
My goal is storage all user action. I have to know who did changes, when he did it and what he did.
I’ve already tried to save all operation in context, but this is not busines logic. So i tried put this logic inside Repo. I thought it was good idea. My Repo looks like this one
defmodule MyProject.Repo do
use Ecto.Repo, ...
def insert(changset, opts, logs_opts) do
{status, response} = insert(changset, opts)
if status == :ok, do: publish(changset.changes, response.uuid, logs_opts, "CREATE")
{status, response}
end
def update(changset, opts, logs_opts) do
{status, response} = update(changset, opts)
if status == :ok, do: publish(changset.changes, response.uuid, logs_opts, "UPDATE")
{status, response}
end
# delete is similar
defp publish(changes, uuid, [{:account_uuid,account_uuid}, {:module, module}], action) do
UserActivities.create(%{
account_uuid: account_uuid,
action: action,
changes: changes,
schema: to_string(module),
target_uuid: to_string(uuid)
})
end
end
But it require from me to pass account uuid inside context
# accounts.ex
def create(attrs, account_uuid \\ nil) do
%Account{}
|> Account.changeset(attrs)
|> Repo.insert([], account_uuid: account_uuid, module: __MODULE__)
end
I dont know how to get user fron conn inside context. I know that i can get it from controller
def create(conn, params) do
case Accounts.create(params, Session.get_current_user(conn)) do
# ..
end
end
After few months i realized that more than 50% of my controller doesnt pass user/account to context, so my activity_log is missing.
I’ve already tried to force usage account_uuid
in repo, but there is another issue. I cannot use seeds without or any script without account_uuid. I tried use "default_uid` built from “0”, but it looks bad.
–
So my question is “How to build activity log or logged user history”