State, actions and permissions
Read and write firm data through the repository and store, check permissions with can(), and keep the audit trail and undo working.
All firm data is one immutable State object. The repository in packages/t2/src/repo.ts is the only thing that changes it, and every change is checked against the caller's organization and role and recorded in the return's audit trail. The web app holds the state in apps/web/lib/store.ts and saves it to localStorage.
State and Session
export type State = {
schema: 1
orgs: Org[]
users: User[]
memberships: Membership[]
corporations: Corporation[]
returns: TaxReturn[]
}
export type Session = { userId: string; orgId: string }A TaxReturn carries its status, formSet (release id), data (inputs, tables, overrides, trial balance, GIFI mappings, answers), versions (locked snapshots) and an append-only audit array. Entity details are in src/model/types.ts and docs/data-model.md.
Permissions
There are 11 actions and four fixed roles. Roles can't be customised per firm.
export const PERMISSIONS: Record<Role, Action[]> = {
owner: ADMIN,
admin: ADMIN,
preparer: ["corp.read", "corp.write", "return.edit", "return.override", "return.submit", "return.export", "return.review"],
reviewer: ["corp.read", "return.override", "return.approve", "return.reopen", "return.export", "return.review", "return.file"],
}ADMIN is all 11: corp.read, corp.write, return.edit, return.override, return.submit, return.approve, return.reopen, return.export, return.review, return.file and org.manage. Only an owner can add, change or remove an owner (guardOwner).
can(state, session, action)returns a boolean and never throws. Use it to hide or disable UI.roleOf(state, session)throwsAccessErrorfor a non-member or a deactivated member.- Every repository write calls an internal
requirefirst, so the check holds even if the UI forgets.AccessErrorreads like "The preparer role cannot return approve." - Another firm's record gives the same "not found" error as a missing one, so ids don't leak across tenants.
In a component, useSession() from lib/use-return.ts returns { state, session, user, org, role, can, saveFailed }:
action={s.can("corp.write") && <Button nativeButton={false} render={<Link href="/app/customers/new" />}>Add customer</Button>}Writing: act()
Screens never mutate state. They call act(fn, success?, opts?) from lib/store.ts with a function that takes (state, session) and returns the next state, usually a single repository call:
if (act((st, se) => setOrgSettings(st, se, { name }), "Firm name saved")) setName(name.trim())- On success it saves, notifies subscribers and shows the
successtoast, with an Undo action if you passopts.undo. - Any thrown error (access, lock, validation) becomes an error toast, the state is unchanged, and
actreturnsfalse, so keep your dialog open. - If another tab saved since this tab last read, the write is refused and the newer state is loaded. A
storagelistener also adopts other tabs' saves. - If localStorage is full or blocked, the write isn't shown as done and
saveFailedbecomes true.
Return lifecycle
ReturnStatus values. The UI derives labels such as Preparing, Filed and Awaiting signature from these and from the filing record (lib/return-status.ts).updateReturn enforces the locks for every write: a cancelled return must be restored first, a filed one needs an amendment, and an approved one must be reopened. A preparer's edit to a return in review takes it back to in-progress.
Audit trail and undo
- Every write appends an
AuditEntry { id, at, by, action, target, before, after, note }. There are about 50 action codes, frominput.settoorg.settings.describeChange(entry, reg?, ret?)turns one into a sentence for the history views. undoLastandredoLastwalk the audit trail back to the last boundary (a status change or lock), and refuse on an approved return.nextUndoandnextRedogive the tooltip text.- Overrides need a reason. Warnings are acknowledged with a reason (
ackDiagnostic).
The state is saved under easyt2:state:v3 and the session under easyt2:session:v3. Firm GIFI mappings are under easyt2:firm-gifi:v1. Clearing site data loses everything, and the audit trail isn't tamper-proof. A backend would implement the same repository contract server-side (milestone M7).
For how roles look to a firm, see Roles and permissions and The audit log.
Related
- ArchitectureSee how the web app, the tax engine and the UI kit fit together, where state lives, and what the prototype leaves out.
- The tax engineUnderstand how a return is evaluated: the registry, the dependency graph, value types, parameters and diagnostics.
- TestingRun the engine, web and Playwright suites, know what each covers, and add the right tests for a change without overloading your machine.