Manager Approval Workflow with ash_paper_trail for Record Changes

'm working on a feature where a manager’s approval is needed before changes made by a user are considered. Here’s how it should work: users can make edits, but anyone else viewing or editing the same records will see the original values until the manager approves the changes.

I’m currently using ash_paper_trail to track changes, which has been great. However, I want the changes to appear only in the main table (not in the version history) after the manager approves.

Is it possible to do this with ash_paper_trail? If so, could someone guide me on how to set it up?

Example:
A user edits a record to update a customer’s address. While the change is pending manager approval, other users will still see the old address. Once the manager approves, the updated address should be the only one visible in the main table.

I haven’t done this myself, but one possible way I see using AshPaperTrail is

https://hexdocs.pm/ash_paper_trail/dsl-ashpapertrail-resource.html#paper_trail-attributes_as_attributes

to make the version resource pretty much a copy of the original resource. And then have a relationsip on the original resource that looks something like this.

relationships do
  has_one :approved_version, __MODULE__.Version do
    from_many? true
    filter expr(approved == true)
    sort {:inserted_at, :desc}
  end
end

And show the approved versions in places where only approved data should be visible.

There might be better ways. But this was the first thing that popped into my mind.

2 Likes