Purchase lots, a product catalogue, and the supplier manifest
Modelling stock that is bought before it is counted — a product catalogue, declared sub-lot specifications, pre-arrival expected quantities, and a browser-side importer for the inconsistent spreadsheets suppliers actually send — plus the TypeORM migration-ordering hazard that had already caused an outage.
What was done
The domain gained the idea of stock that is bought before it is counted: a product catalogue so a hundred identical ThinkPads share one specification record, declared sub-lot specifications for what the seller said a tranche would be, and pre-arrival fields on the purchase lot so a lot exists with a supplier and expected quantities before a device is touched. The supplier manifest was modelled as expected line items with a browser-side CSV/XLSX importer: SheetJS is dynamically imported so it stays out of the main bundle, each target field carries a list of header aliases, and the operator sees and can correct the guessed mapping before anything is committed. The vocabulary fixed here — "Purchase Lot", "Sub-lot" — held for the rest of the project. The same work documented the TypeORM migration-ordering hazard: because TypeORM SELECTs every mapped column, deploying an entity that declares a column before its migration adds it makes every query on that table 500, which had already taken three pages down.
before arrival after unpacking
--------------- ---------------
purchase lot assets counted in
supplier one row per device
expected quantity
| |
v v
EXPECTED line items --+ +-- ACTUAL inventory
(what the seller said) | | (what is on the floor)
v v
reconcile
found / missing / extraTwo independent records of the same pallet, and the comparison that only exists because both are kept.
Status
Shipped, 10 July 2026.
The problem
Stock arrives as a purchase lot — a pallet or a van-load bought from a supplier, described in advance by a spreadsheet, and only later unpacked and counted. Before this, the system had assets but no concept of what was expected, so there was nothing to reconcile against.
What was built
A product catalogue, and assets linked to it. The point is that a hundred identical ThinkPads should share one product record carrying the specification, rather than each device carrying its own free text. Sub-lots gained a declared specification — what the seller said this tranche would be.
Pre-arrival fields on the purchase lot so a lot exists, with expected quantities and a supplier, before a single device is touched.
The supplier manifest: the expected inventory, and a client-side CSV/XLSX importer for the list the supplier emails over. Parsing in the browser rather than on the server was deliberate — the files are small, the formats are inconsistent, and the operator needs to see what was understood before it is committed.
Vocabulary. "Purchase Lot" and "Sub-lot" were fixed here and have held ever since. Naming the domain early is why later features could be described in the operator's own words.
The deploy guardrail documented the deploy process and, more importantly, the
migration-ordering hazard: TypeORM SELECTs every mapped column, so
deploying an entity that declares a new column before its migration adds that
column makes every query on that table 500. This had already caused a an early phase
outage taking /assets, /lots and /notifications down.
It is written down because it is not intuitive and it recurs.
What was deliberately not built
No supplier portal, no EDI. The manifest is a spreadsheet because that is what suppliers actually send.
Open questions
None outstanding. The reconciliation engine that consumes the manifest followed the next day.
The code
Header matching in the manifest importer: exact on the normalised header first, then a contains match, then nothing — the operator fixes what it got wrong.
{ key: 'serialNumber', label: 'Serial number',
aliases: ['serial', 'serial number', 'serial no', 'sn', 'service tag'] },
// ... ten more target fields, each with its own aliases
function normalize(s: string): string {
return s.toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim();
}
function autoGuess(columns: string[], aliases: string[]): string {
const norm = columns.map((c) => ({ raw: c, n: normalize(c) }));
for (const alias of aliases) {
const hit = norm.find((c) => c.n === alias);
if (hit) return hit.raw;
}
// looser: alias contained in the header (e.g. "RAM (GB)" contains "ram")
for (const alias of aliases) {
const hit = norm.find((c) => c.n.includes(alias));
if (hit) return hit.raw;
}
return '';
}apps/web/app/batches/[id]/import-expected.tsx — an excerpt, trimmed to the technique it illustrates.
Not shown. The server side of the importer was not excerpted: replace-on-import deletes a batch's existing lines before inserting, and the guard around that is a data-safety control better described than published. No entity definitions, column names for pricing, or supplier identities.
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.