Files
Claper/lib/claper/audit/log.ex
Raúl R Pearson 4bbf9742c5 Add audit log (#214)
* Add audit log

* Update translation files

* Improve Audit queries

* Improve Audit.Log schema

* Make user auth audit logs async

* Relax Flop and Flop Phoenix dep specs

* Make Flop.validate!/2 call more robust

* Improve a11y and UI for audit log live view

* Improve audit_logs indexes

* Fix formatting

* Fix async_log_action and tests

* Add remote_ip

* Update translation files
2026-03-19 18:29:09 +01:00

46 lines
1015 B
Elixir

defmodule Claper.Audit.Log do
use Ecto.Schema
import Ecto.Changeset
@derive {
Flop.Schema,
max_limit: 100,
filterable: [:action, :user_email],
sortable: [:inserted_at, :action],
default_order: %{
order_by: [:inserted_at],
order_directions: [:desc]
},
adapter_opts: [
join_fields: [
user_email: [
binding: :user,
field: :email,
path: [:user, :email]
]
]
]
}
schema "audit_logs" do
field :action, :string
field :resource_type, :string
field :resource_id, :integer
field :metadata, :map, default: %{}
belongs_to :user, Claper.Accounts.User
timestamps(updated_at: false)
end
@doc false
def changeset(log, attrs) do
log
|> cast(attrs, [:action, :resource_type, :resource_id, :metadata, :user_id])
|> validate_required([:action])
|> validate_length(:action, max: 255)
|> validate_length(:resource_type, max: 255)
|> assoc_constraint(:user)
end
end