POS Serial Numbers Features and Flows
Optional per-unit serial numbers recorded at the counter, the shared uniqueness rule, release on return, error codes, deploy order and the mistyped-serial runbook.
POS - Serial Numbers Features and Flows
A serial number identifies one physical unit (a phone IMEI, a laptop serial). A cashier can optionally record one per unit when a sale is completed at the counter. The serial is stored against the order the sale creates, so it is traceable for a warranty claim, a theft report or a recall.
1. Documentation Evidence
| Source Type | Files | What Was Extracted |
|---|---|---|
| Util | apps/api/src/modules/order/shared/order-item-serial.util.ts | Normalisation, character rule, count and duplicate checks, availability pre-check, insert, release, read. |
| POS util | apps/api/src/modules/pos/shared/pos-sale-serial.util.ts | Per-line resolution on complete, order of checks. |
| POS release util | apps/api/src/modules/pos/shared/pos-counter-return-serial.util.ts | Counter-return picks and refusals. |
| Services | pos-sale-completion.service.ts, order-counter-handover.service.ts, order-dispatch.service.ts | Where serials are checked and written. |
| DTOs | pos/admin/sale/dto/pos-sale-request.dto.ts, pos-sale-response.dto.ts, pos/admin/return/dto/create-pos-counter-return.dto.ts, pos/admin/sale-support/dto/pos-returnable.dto.ts, order/admin/order/dto/dispatch-order-admin.dto.ts | Request and response fields and validators. |
| Schema | packages/db/src/schema/order/order-item-serial.ts, migration 0073_order_item_serial_release.sql | Columns, indexes, CHECKs. |
| Error codes | apps/api/src/common/types/error-codes.ts | The five serial codes. |
| POS app | nano-store-pos/src/renderer/src/features/sale/serials/, features/returns/return-serial-picker.tsx | Cashier behaviour. |
| Plan | .omc/plans/pos-serial-numbers/master-plan.md (D5, R4-R6) | Release rules and follow-ups. |
2. Feature Summary
| Field | Value |
|---|---|
| Module | POS (shared table and util owned by order) |
| Primary user value | Trace a specific sold unit after the sale. |
| Actors | Cashier (admin with Pos_CREATE / Pos_UPDATE), dispatching admin, customer (reads own order). |
| Main entry points | POST /api/admin/pos/sales/{publicId}/complete, POST /api/admin/pos/sales/{publicId}/returns, POST /api/orders/{id}/shipments. |
| Main outputs | Rows in order_item_serial; serialNumbers / releasedSerialNumbers on the sale lines. |
3. What a serial is
- Optional free text, entered by the cashier at sale time. It is never entered at product creation, and there is no "requires serial" product flag.
- One entry per unit, at most the line quantity. A line of three phones has up to three serials.
- Stored one row per unit in
order_item_serial, the same table retail dispatch already writes. - Normalised by one function for every writer: each entry is trimmed, blank entries are dropped, and each remaining entry must match
^[\x21-\x7E]{1,120}$(printable ASCII, no internal space, 1 to 120 characters). - Case-sensitive:
abcandABCare different serials.
4. Uniqueness rule
A serial is unique per product among active serials, across retail dispatch and POS.
| Aspect | Rule |
|---|---|
| Scope | (product_id, serial_number), not global. Two different products may carry the same string. |
| Active | A row with released_at IS NULL. Enforced by the partial unique index uq_order_item_serial_product_serial_active. |
| Released | A released row is history and never blocks the same serial being recorded again. |
| Deletion | Rows are never deleted. |
| Channels | Retail dispatch and POS share the table, so a serial recorded on one channel blocks the other. |
Duplicates are refused in three layers: the in-request check, a pre-check SELECT on active rows before stock is reserved or an order number is consumed, and the unique index as the race guard.
5. Actor Matrix
| Actor | Can Do | Cannot Do | Auth |
|---|---|---|---|
| Cashier | Record serials on complete of a counter pickup; pick serials on a counter return. | Record serials for a delivery sale; correct a recorded serial. | Pos_CREATE (complete), Pos_UPDATE + PosReturns_CREATE (returns). |
| Dispatching admin | Record serials on POST /api/orders/{id}/shipments. | — | Orders_UPDATE. |
| Customer | See serials on their own order. | Write serials. | Customer JWT. |
No permission was added or changed.
6. Flows
6.1 Record at the counter (pickup)
Serials travel in the complete request. They are never stored on the draft.
Order of checks inside resolvePosLineSerials (this order is the contract):
- Normalise every entry and drop entries that normalise to nothing, so an all-blank entry never fails a delivery sale or names a missing line.
- Look up each
linePublicIdon the sale (lower-cased). Unknown line:409 POS_SERIAL_LINE_NOT_FOUND. - Any serial left on a sale whose goods are not handed over now:
409 POS_SERIALS_NOT_ALLOWED. - At most one serial per unit, and none repeated within a product:
409 ORDER_SERIAL_COUNT_MISMATCHor409 ORDER_SERIAL_ALREADY_RECORDED. - Pre-check against active serials of the same product:
409 ORDER_SERIAL_ALREADY_RECORDED.
All of this runs before prices are checked, stock is reserved or an order number is taken, and the whole completion is one transaction. A refused serial rolls everything back and the sale stays a live draft.
6.2 Delivery sales
A sale whose goods do not leave with the customer now refuses serials. Record them when the order is dispatched, with POST /api/orders/{id}/shipments.
6.3 Dispatch (retail)
Same util, same rules. The request shape is unchanged: lines[].serialNumbers, at most 99 per line. Serials are checked across every line of the request, and an already-active serial is refused before the shipment exists.
6.4 Read
GET /api/admin/pos/sales/{publicId} and every route returning a sale carry two always-present arrays per line:
| Field | Meaning |
|---|---|
serialNumbers | Every serial recorded for the line, released or not. This is what was sold. |
releasedSerialNumbers | The released subset. |
Drafts return [] for both. The same serials appear on the order the sale created (admin order view and the customer's order), released ones included as history.
6.5 Release on return
A released serial can be sold again. Release happens in the same transaction as the return or failure.
| Trigger | Releases | Reason stored |
|---|---|---|
POS counter return that names serials in lines[].serialNumbers | Exactly the picked serials of that line. | counter_return |
| POS counter return that makes a line fully returned | Every remaining active serial of that line. | order_return |
Retail return accepted for the whole line (returnedQuantity + accepted >= deliveredQuantity) | Every active serial of that line. | order_return |
| Shipment marked failed | Every active serial of that shipment. | shipment_failed |
| Shipment marked returned to sender | Every active serial of that shipment. | shipment_returned_to_sender |
Rules:
- A partial retail return releases nothing. The inspect route takes no serial picks, so the system cannot know which unit came back. The serial stays active until a full-line return or a future admin feature releases it.
- Counter-return picks are validated first. A pick that is not an active serial of that line is
409 POS_COUNTER_RETURN_NOT_ALLOWEDwithdetails.reason = "serial_not_on_line". More picks than units returned, or a repeated pick, isdetails.reason = "serial_count_exceeds_quantity". The serial itself is never echoed. GET /api/admin/pos/sales/{publicId}/returnablelists each line's active serials inlines[].serialNumbers, always present, so the till can offer them for picking.- Releasing is idempotent: only rows with
released_at IS NULLmatch. - Each release writes
released_at,released_by_admin_idandrelease_reason.
7. Cashier experience (POS app)
Read from nano-store-pos; no code was changed.
- Each cart row has a "Serials n/qty" control that opens one dialog. For a delivery sale the dialog cannot be opened (
serialStore.openForreturns early) and thecompletepayload omitsserials. - In the dialog the cashier scans or types a serial and presses Enter or Tab. The client applies the same rule as the server and refuses with copy such as "That serial number is already entered for this product." or "Every unit already has a serial number."
- When the quantity of a line is lowered, the list is cut to the new quantity: the earliest entries are kept and the newest extras are dropped.
- The same serial cannot be entered twice for one product across lines of the same sale.
- The store holds one active sale at a time. Serials are not visible on another terminal and are lost on restart.
- On a
completeconflict the server names the clashing serials indetails, and the dialog flags them "Already recorded". - The
serialsfield is omitted from thecompleterequest when nothing was entered or the sale is a delivery. - On the returns screen a checkbox picker lists the line's active serials ("Serial numbers coming back (n of qty)"); ticks are capped at the quantity being returned.
- Serials print under the line's quantity row on the receipt as
S/N: a, b, c, wrapped to at most four lines. Before validation the receipt data is clipped to 99 serials of 120 characters each, so a long list never fails the whole receipt. The receipt email does not include them.
8. Request shapes
8.1 POST /api/admin/pos/sales/{publicId}/complete
{
"paymentReference": "TXN-88213",
"serials": [
{
"linePublicId": "019ff590-0000-7000-8000-000000000131",
"serialNumbers": ["356938035643809", " 356938035643817 "]
}
]
}| Field | Type | Required | Validation |
|---|---|---|---|
serials | array | No | At most 100 entries; linePublicId must be unique across entries. |
serials[].linePublicId | uuid v7 | Yes | @IsUUID("7"), lower-cased before validation. |
serials[].serialNumbers | string array | Yes | At most 99 items; each at most 128 raw characters matching ^ *[\x21-\x7E]* *$. The util then trims and enforces 1 to 120. |
{} stays a valid body. The second serial above is trimmed and accepted; "3569 3803" (internal space) or a non-ASCII value is refused.
8.2 Response (sale line excerpt)
The project envelope is { message, data, errorCode }. A sale read:
{
"message": "Sale retrieved successfully",
"errorCode": null,
"data": {
"publicId": "019ff590-0000-7000-8000-000000000101",
"status": "picked_up",
"items": [
{
"publicId": "019ff590-0000-7000-8000-000000000131",
"productName": "Galaxy A55",
"variantName": "8GB/256GB Navy",
"quantity": 2,
"serialNumbers": ["356938035643809", "356938035643817"],
"releasedSerialNumbers": ["356938035643817"]
}
]
}
}8.3 Returnable line and counter-return line
{
"productPublicId": "018f1e2a-0000-7000-8000-000000000012",
"variantPublicId": "019ff58f-0956-722f-80c9-b5d7fc484b08",
"sold": 2,
"returned": 1,
"pending": 0,
"returnable": 1,
"unitNetMinor": 4500000,
"serialNumbers": ["356938035643809"]
}{
"productPublicId": "018f1e2a-0000-7000-8000-000000000012",
"variantPublicId": "019ff58f-0956-722f-80c9-b5d7fc484b08",
"quantity": 1,
"serialNumbers": ["356938035643809"]
}serialNumbers on a counter-return line is optional, at most 99 items, same per-item validation as above. The serials are part of the idempotency hash, so the same Idempotency-Key with different picks is IDEMPOTENCY_KEY_CONFLICT.
9. Error codes
| HTTP | Code | When | Client action |
|---|---|---|---|
| 400 | ORDER_SERIAL_INVALID | After trimming, a serial is not 1 to 120 printable ASCII characters without spaces. Backstop to the DTO regex. details.index is the position; the value is never echoed. | Validate the same rule client-side; highlight the entry at details.index. |
| 409 | ORDER_SERIAL_COUNT_MISMATCH | More serials than units on a line. | Cap inputs at the line quantity. |
| 409 | ORDER_SERIAL_ALREADY_RECORDED | The serial is active for the same product (details.conflicts), or entered twice in one request (details.duplicates). A race lost at the database returns details.conflicts: []. | Highlight the named serials. Branch on errorCode, never on message. |
| 409 | POS_SERIAL_LINE_NOT_FOUND | A linePublicId is not on the sale. | Re-fetch the sale and re-sync. |
| 409 | POS_SERIALS_NOT_ALLOWED | Serials sent on a sale that is not handed over now (delivery). | Record them at dispatch. |
| 409 | POS_COUNTER_RETURN_NOT_ALLOWED | details.reason is serial_not_on_line or serial_count_exceeds_quantity. | Re-fetch /returnable and re-pick. |
Example ORDER_SERIAL_ALREADY_RECORDED body:
{
"statusCode": 409,
"errorCode": "ORDER_SERIAL_ALREADY_RECORDED",
"message": "One or more serial numbers are already recorded against another unit of the same product.",
"details": {
"conflicts": [
{
"productPublicId": "018f1e2a-0000-7000-8000-000000000012",
"productName": "Galaxy A55",
"serialNumber": "356938035643809"
}
]
}
}Serials go in details, never in message, and serial exceptions carry no cause. AllExceptionsFilter returns details in the response body and its log lines carry only the message, cause message and stack, so serial values are not written to the server log.
10. Data model
Table order_item_serial (schema packages/db/src/schema/order/order-item-serial.ts).
| Column | Notes |
|---|---|
public_id | uuid v7, unique. |
order_shipment_item_id, order_item_id | FKs, ON DELETE CASCADE. order_item_id is denormalised for lookup. |
variant_id, product_id | FKs, ON DELETE RESTRICT. product_id is denormalised so the unique index can exist. |
serial_number | varchar(120), not blank. |
recorded_by_admin_id, recorded_at | Who and when. |
released_at, released_by_admin_id, release_reason | NULL means active. Added by migration 0073. |
| Index / Constraint | Purpose |
|---|---|
uq_order_item_serial_product_serial_active | Unique (product_id, serial_number) where released_at IS NULL. |
idx_order_item_serial_product_serial | Full non-unique twin; serves the product FK and the history lookup (warranty, theft). |
idx_order_item_serial_released_by_admin_id | Serves the SET NULL of the releasing admin FK. |
chk_order_item_serial_release_pair | released_at and release_reason are both set or both NULL. |
chk_order_item_serial_release_reason | Reason is one of counter_return, order_return, shipment_failed, shipment_returned_to_sender. |
chk_order_item_serial_released_by_requires_released | An admin is recorded only on a released row. |
chk_order_item_serial_not_blank | Existing. |
11. Deploy order
- Migration
0073_order_item_serial_release. It runsSET LOCAL lock_timeout = '5s'and takesACCESS EXCLUSIVElocks onorder_item_serialwhile it adds constraints and builds three indexes. Deploy in a quiet window; a timeout aborts the whole batch and it can be re-run. - The backend. It expects the release columns and the partial unique index that migration 0073 creates, so the migration must already be applied.
- The POS app. An old backend answers
400to the unknownserialsfield, so the app must not ship first. The app omits the field when empty.
Admin and storefront can ship any time after step 2. Before it, the only effect is the stricter dispatch validation surfacing as 400.
Rollback: down/0073_order_item_serial_release.down.sql refuses once any serial has been released or a serial has duplicate history. A code-only rollback is safe.
12. Behaviour changes on existing routes
- Dispatch (
POST /api/orders/{id}/shipments): a serial with an internal space or a non-ASCII character used to be accepted and is now400. More than 99 serials on a line is400. A raw value of 121 to 128 characters is accepted when trimming brings it to 120 or fewer. - Dispatch duplicate message text changed; branch on
errorCode. - Admin order and shipment views have no released flag; they list released serials as history.
- A customer whose account a POS sale is attached to sees the device serials on their storefront order.
13. Runbook: a mistyped serial
There is no route to correct or delete a recorded serial. A typo blocks the real device's serial until the mistyped row is released.
-
Find the row by who recorded it and when:
SELECT id, order_item_id, product_id, serial_number, recorded_by_admin_id, recorded_at FROM order_item_serial WHERE released_at IS NULL AND recorded_by_admin_id = '<admin uuid>' ORDER BY recorded_at DESC; -
Release it through a return of that unit: a POS counter return that picks the serial, or a full-line return. This is the only supported release path.
-
Record the correct serial on the next sale or dispatch.
Do not update or delete rows by hand in production.
14. Follow-ups (not built)
| Item | Note |
|---|---|
| Correct or delete a serial | No route yet. Needs an irreversible-class review. |
| Release on a partial retail return | Needs a serial picker in the admin inspect UI and a backend extension. |
| Admin UI to view or mark released serials, and a released flag in admin DTOs | Admin repo, other owner. |
| Serials on manual or WhatsApp orders and at WhatsApp pickup collection | Not recorded; the parameter exists. |
| Serials in the POS receipt email | Not included. |
Residual 40P01 from unrelated lock orders surfaces as a 500 | Mapping to SYS_TRANSACTION_CONFLICT is a follow-up. |
Consumer contract for the admin and storefront owners: .omc/plans/pos-serial-numbers/consumer-handoff.md.
See Also
- API doc:
/docs/developer/pos/api - Backend doc:
/docs/developer/pos/backend - Features and flows doc:
/docs/developer/pos/feature - Deploy notes:
/docs/developer/pos/deploy - TDD: not yet published
POS Deploy Notes
Migrations 0070-0073, deploy order, env and nginx delta, the cashier role, POS client rollout, rollback and post-deploy checks for the counter till.
Reviews Module Overview
Customer product reviews and the rating aggregate — verified purchases only, published on submission, moderated reactively, with an aggregate that cannot be internally inconsistent.