Skip to content
The record
Report23 July 20264 min read

Hierarchy, batch ownership, and the activity log

A Lot to Asset to Hardware drill-down with Total/Audited/Pending counts on each lot card, plus ownership scoping enforced in six passes — the fifth being the one that mattered, because scoping the API is not enough when clients sync a local database directly and the sync rules have to be scoped too.

systemssecurityitad-lifecycle

What was done

Built the Lot to Asset to Hardware drill-down with a breadcrumb trail and Total/Audited/Pending counts on each lot card, so the state of a lot is readable without opening it; the global Assets page was deliberately kept as a search, because "what is in this lot" and "where is this serial" are two different questions. Added batch ownership and then enforced it in six passes — batch reads, asset reads via their batch's owner, dashboard and reports and certificates, write guards with admin reassignment, the PowerSync upload path and the sync rules, and the activity log. The fifth pass is the one that mattered: scoping the HTTP API is not enough when clients replicate a local database directly, because without scoped sync rules a device downloads rows it could never have fetched over HTTP. Scoping applies to managers only, stated once and read from one place, with an unowned pool so technician-created lots stay visible to every manager. The sync rules crashed on the OR / IS NULL parameter query expressing "owned by me, or unowned", and took sync down entirely rather than returning a narrower set.

            manager's device
            /              \
     HTTP fetch        PowerSync replication
          |                     |
     API guards            sync rules
     (E1-E4, E6)           (E5)
          |                     |
          v                     v
    rows they may        rows their local
    read on request      database holds
 
  a scoped API with unscoped rules still
  ships the rows down to the device.
  guarding one door does not guard the other.

Why E5 was two jobs: a manager's device reaches the data by two independent routes.


Status

Shipped, 23 July 2026.

A single day, and one of the densest in the project.

The problem

Two problems, solved together because they are the same problem.

Navigation. The system had grown flat: assets, lots, pallets, all listed separately. The warehouse thinks in a hierarchy — a purchase lot contains devices, a device has hardware — and the screens did not.

Accountability. With several managers buying and processing stock, nobody could answer who did this, and whose stock is this?

What was built

The drill-down: Lot → Asset → Hardware, with a breadcrumb trail, and Total/Audited/Pending counts on each lot card so the state of a lot is visible without opening it.

A deliberate exception: the global Assets page was kept as a search, not converted into another drill-down. Two different questions — what is in this lot and where is this serial — need two different screens.

Ownership, then enforcement in six passes:

Scope
E1Batch reads
E2Asset reads, via their batch's owner
E3Dashboard, reports and certificates
E4Write guards, plus admin reassignment
E5The PowerSync upload path and the sync rules themselves
E6The activity log

E5 is the one that mattered. Scoping the API is not enough when clients sync a local database directly: without scoping the sync rules, a manager's device would download rows they could not fetch over HTTP. Guarding the upload path and the download rules are two separate jobs.

A system-wide activity log, and ownership carried into the batch report so a printed report says who generated it.

Two decisions that shaped later work

Managers only. Scoping applies to managers. Admins and technicians see everything — technicians are floor staff who scan into any lot, and scoping them would break the job. This is stated once, in isScopedManager, and every guard reads it from there.

An unowned pool. Technician-created lots belong to nobody and are visible to all managers. Without this, a technician's work would have been invisible to everyone, including the manager who needed it.

The defect worth recording

the PowerSync sync rules crashed on the OR / IS NULL parameter query. The rule expressing "owned by me, or unowned" was not expressible the way it was first written, and the failure took sync down entirely rather than returning a narrower set.

What was deliberately not built

Per-user isolation on the audit station was deferred. The sticks share a station account; scoping them needed PowerSync work that was not done here. It was still outstanding when the station got its own least-privilege account in August.

Open questions

None outstanding from this work; the permission model was rebuilt fail-closed on 22 August, which superseded the ad-hoc guards added here.


The code

Every lot card's counts come from one grouped query over assets, not a query per lot.

const LIVE = `asset.stock_status != 'sold'`;
const rows = await this.assets
  .createQueryBuilder('asset')
  .select('asset.batchId', 'batchId')
  .addSelect(`COUNT(*) FILTER (WHERE ${LIVE})`, 'total')
  .addSelect(
    `COUNT(*) FILTER (WHERE asset.audit_status IS NOT NULL AND ${LIVE})`,
    'audited',
  )
  .addSelect(`COUNT(*) FILTER (WHERE ${LIVE} AND asset.pallet_id IS NULL)`, 'unallocated')
  .where('asset.batchId IN (:...ids)', { ids: batches.map((b) => b.id) })
  .groupBy('asset.batchId')
  .getRawMany<{ batchId: string; total: string; audited: string }>();
// Pending is never stored: Math.max(0, total - audited), derived where it is
// shown. One pass for the whole page, so the list never queries per lot.
From apps/api/src/batches/batches.service.ts — an excerpt, trimmed to the technique it illustrates.

Not shown. No ownership or scoping code: isScopedManager, the per-pass guards, and the PowerSync parameter and data queries are permission evaluation still in production, so none of it is shown — the diagram states the shape of the mistake without the predicates. The excerpt is the benign counts query instead. The activity log's schema and the certificate path are also left out.

A published copy. Commit references and internal identifiers have been removed and the operator is not named; the engineering, the counts and the stated limits are unchanged.