B2B Second Inventory Orders API Reference
Endpoint reference for the bb phase 4 admin order API — create, list, detail, notes, cancel, sales receipt PDF, and the pickers and submission check behind the new-order form — with permissions, throttles, idempotency and known limits.
B2B Second Inventory (bb) — Orders API Reference
Audience: Admin engineers integrating with the bb phase 4 backend. Orders are paid at creation: there is no cart, payment gateway or storefront endpoint.
All endpoints are prefixed with /api, need a JWT, and need the listed permission. Superadmin passes every permission check. Every response uses the ResponseDto envelope (message, errorCode, data).
Base path: /api/admin/bb/orders. Every :publicId is a UUID (a malformed one is a 400).
Routes
| Method and path | Permission | Throttle (per 60 s) |
|---|---|---|
GET options/customers | BB_Orders_CREATE | 120, per user (BB_ORDER_PICKER) |
GET options/variants | BB_Orders_CREATE | 120, per user (BB_ORDER_PICKER) |
GET submissions/:key | BB_Orders_CREATE | 120, per user (BB_ORDER_PICKER) |
GET / (list) | BB_Orders_READ | 30, per IP (ADMIN_READ) |
GET :publicId (detail) | BB_Orders_READ | 30, per IP (ADMIN_READ) |
GET :publicId/receipt.pdf | BB_Orders_READ | 30, per IP (ADMIN_READ) |
POST / (create) | BB_Orders_CREATE | 30, per user (BB_ORDER_CREATE) |
POST :publicId/cancel | BB_Orders_UPDATE | 10, per IP (ADMIN_WRITE) |
POST :publicId/notes | BB_Orders_UPDATE | 10, per IP (ADMIN_WRITE) |
The seed and permissions:sync create five BB_Orders_ codes: CREATE, READ, UPDATE, DELETE and RESTORE. No order route uses DELETE or RESTORE.
Create
POST /api/admin/bb/orders places a paid order and decrements stock in one transaction.
- Body:
customer({ kind: "existing", customerPublicId }or{ kind: "new", name, phone, email?, companyName?, address? }),lines(at least one; eachvariantPublicId,quantity,expectedUnitPricePaisa),paymentMethod, and optionalpaymentReferenceandnote. Blank optional text becomes null. - A new customer is created inside the same transaction as the order. A phone that already belongs to a customer answers 409
BB_ORDER_CUSTOMER_CONTACT_TAKENwithdetails.matches. - A price that moved since the form read it answers 409
BB_ORDER_PRICE_CHANGED. Short stock answers 409BB_ORDER_INSUFFICIENT_STOCKwithdetails.lines. A refused order consumes no order number. - Other codes: 400
VALIDATION_FAILED,BB_CUSTOMER_PHONE_INVALID,BB_ORDER_DUPLICATE_VARIANT,BB_ORDER_TOTAL_LIMIT,BB_ORDER_SUBMISSION_KEY_INVALID; 409BB_ORDER_CUSTOMER_NOT_FOUND,BB_ORDER_CUSTOMER_DISABLED,BB_ORDER_VARIANT_UNAVAILABLE,BB_CATALOG_BUSY. - On the routes that take a
:publicId(detail, receipt, notes, cancel), a missing order answers 404BB_ORDER_NOT_FOUND. - Each created order writes a
bb.order.createaudit row.
Idempotency
Idempotency-Key is required on POST / and POST :publicId/notes; without it the answer is 400 IDEMPOTENCY_KEY_REQUIRED.
| Situation | Answer |
|---|---|
| Same key, same request, again | The first answer is replayed; one order exists |
| Same key, different request | 409 IDEMPOTENCY_KEY_CONFLICT |
| Same key while the first request is running | 409 IDEMPOTENCY_REQUEST_IN_PROGRESS |
The key is also recorded durably with the order. If the response is lost after the order commits, GET submissions/:key tells the caller whether that key already made an order. A key made by another admin answers as if it did not exist. Cancel has no idempotency key: a second cancel answers 409 because the order is already cancelled.
List
GET /api/admin/bb/orders takes search, status, paymentMethod, sort (default createdAt), order (default desc), page and size. No query answers page 1, size 20. size above 100 answers 400. The pickers cap size at 20.
Detail and notes
GET :publicId returns the order with its lines, events, notes, refunds and returns (the last two are described in Returns and Refunds API reference). Notes are capped at the newest 100; when more exist, notesTruncated is true.
POST :publicId/notes takes { body } of 1 to 1000 characters with no NUL. A 1001-character body answers 400. The response omits body: the idempotency layer stores the answer in Redis, and a note can hold anything. Read the text from the detail route.
Cancel
POST :publicId/cancel takes a reason and a note of 1 to 500 characters. It restocks every line in the same transaction and returns the restocked lines (quantity, quantityAfter, variantDeleted, productDeleted). If a restock would pass the variant's stock cap, the whole cancel is refused. The response also carries refund, the requested cancellation refund the cancel leaves, or null when nothing is owed. A return or an undecided refund refuses the cancel: see Returns and Refunds API reference.
| Status | Code | Trigger |
|---|---|---|
| 404 | BB_ORDER_NOT_FOUND | No order has that publicId. |
| 409 | BB_ORDER_ALREADY_CANCELLED | The order's status is already cancelled; a second cancel. Checked under the row lock before any restock. |
| 409 | BB_ORDER_HAS_RETURN | A return holds pending units, or units were returned. Phase 5. |
| 409 | BB_REFUND_DECISION_REQUIRED | A requested refund covers a line the cancel would net against; details.refunds and details.lines. Phase 5. |
| 409 | BB_REFUND_ORDER_TOTAL_EXCEEDED | The cancellation refund would pass the order total. Phase 5. |
Sales receipt
GET :publicId/receipt.pdf returns application/pdf as an attachment named <orderNumber>.pdf. The page is titled "Sales Receipt" and carries no PAN, VAT, subtotal or grand total. Long names, companies, addresses and payment references wrap onto further lines. Each successful download writes its own bb.order.download audit row; the audit row is written after the PDF renders, so a failed render writes none.
Known limits
- Devanagari text prints garbled on the receipt. This is an accepted limit. The browser pass confirmed it: a customer named in Devanagari printed as garbage on the receipt PDF.
- Expired access token in an admin Server Action. The access JWT lapses after 15 minutes, but its cookie can outlive it for a short window. In that window an action on an open tab answers 401. The admin handles it per action:
- Cancel, add note, Open return and Draft refund: a toast says the session has ended and to check the order or the notes, then try again. The admin then logs out first and goes to the sign-in page with a callback to the same page. An earlier redirect without the logout bounced straight back to the same page in the browser pass; with the logout first, the browser pass banned a staff member mid-dialog and Open return, Draft refund, and the refund approve, reject, close-as-not-owed and settle dialogs each ended at the sign-in page with a callback to the page they were on. Cancel and add note use the same path but were not separately driven in that pass. A typed note stays in the field only if the user returns to the same page without going through the sign-in form.
- Create order: no redirect. A first-attempt 401 shows a banner saying the session needs renewing and that nothing was sold. The form and the submission key are kept; pressing Create order again sends the same key and body. While the cookie is still alive and the JWT is dead, the second press can answer 401 again. Once the cookie lapses,
proxy.tsrefreshes the session on that press and the order is created once. - The window is admin-wide, not specific to orders. Navigating refreshes the session.
- Boot the built output before deploying. Start
node dist/src/main.jsand confirm it comes up before a deploy. The entry path isdist/src/main.js.check-typesno longer emits intodist(apps/api/package.jsonrunstsc --noEmitwith--composite false --incremental false), so it can run in any order against the build. The full pre-deploy list is in Orders Deploy Notes.
Seeding the superadmin
pnpm db:seed and pnpm db:seed:prod create the superadmin only when SUPERADMIN_PASSWORD is set. On a database with no superadmin and no password, the seed refuses before its first write. With the superadmin already present, the seed runs without the variable and changes no login. The seed logs Created superadmin user: <email> and never prints the password.
The request shapes above were read from the DTO classes and controllers. They were not captured from a running server.
B2B Second Inventory Stock Deploy Notes
Deploy order, the variant row count check, the required permissions:sync and role grant, intermediate states, verification and the rollback runbook for bb phase 3 migrations 0063 and 0064.
B2B Second Inventory Orders Deploy Notes
Deploy order, migrations 0065 and 0066, the required permissions:sync, the SUPERADMIN_PASSWORD seed requirement, the pre-deploy check list and rollback limits for bb phase 4. Phase 4 ships together with phase 5.