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.
B2B Second Inventory (bb) — Orders Deploy Notes
These notes cover migrations 0065_bb_orders and 0066_bb_order_stock_audit. The earlier steps in Deploy Notes (phase 1) and Stock Deploy Notes (phase 3) still apply. This page adds what phase 4 changes.
Commands marked not run were read against package.json and the scripts but were not executed against a database when this page was written.
0. Phase 4 and phase 5 ship together
Do not deploy phase 4 to production alone. Phase 4 cancels an order and restocks it but writes no refund record; phase 5 owns refunds and returns. The product decision is that the two go live together, so phase 5 needs no cutover rule for orders cancelled earlier. If phase 4 reached production first, a cancelled order with no refund row could not serve as the refund queue, and orders repaid by hand would look owed.
1. Deploy order
- Back up with
pg_dump. Replay0065and0066on a copy of the production dump first, as in Deploy Notes. - Migrate, in a quiet window with no import running:
pnpm db:migrate. - Deploy the API. The old Admin #2 keeps working;
BB_Orders_*codes that are not yet present are invisible to it. - Sync permissions:
pnpm --filter @nano-store/api permissions:sync. Required. - Deploy Admin #2 after the backend.
- Grant: a superadmin opens Admin #2 Roles and gives each bb role its
BB_Orders_*codes. Until then only a superadmin sees Orders.
Why the window must be quiet
drizzle-kit migrate holds every lock to the end of the batch, with SET LOCAL lock_timeout = '5s'.
| Migration | Lock | Tables |
|---|---|---|
0065_bb_orders | SHARE ROW EXCLUSIVE | bb_customers, bb_product_variant, admin_users |
0066_bb_order_stock_audit | ACCESS EXCLUSIVE, with a full validating scan | bb_audit, bb_inventory_movement |
A running import holds ROW EXCLUSIVE on both 0066 tables for its whole transaction, so the batch aborts until the import ends. A retail admin login writes admin_users, so it can wait briefly. A lock that cannot be had in 5 s aborts the batch with nothing applied; run it again.
permissions:sync is required
Neither migration inserts a BB_Orders_* permission row. permissions:sync creates five: BB_Orders_CREATE, READ, UPDATE, DELETE and RESTORE. It grants them to nobody. Without it the Roles screen has no Orders module, and a bb staff member gets 403 while a superadmin still passes every check, so the screens look fine to the person deploying them.
The grant step is manual
| To let a role | Grant |
|---|---|
| Open the list, detail and receipt | BB_Orders_READ |
| Create an order and use the pickers | BB_Orders_CREATE (create-only staff see no list; they need READ to read their own order) |
| Add a note or cancel | BB_Orders_UPDATE |
BB_Orders_DELETE and BB_Orders_RESTORE are not checked by any route.
2. Seed: SUPERADMIN_PASSWORD
SUPERADMIN_PASSWORD is now required when the seed must create the superadmin. On a database with no superadmin and no password, pnpm db:seed and pnpm db:seed:prod refuse before their first write. With the superadmin already present, the seed runs without the variable and changes no login. If SUPERADMIN_EMAIL names a user that does not exist, the seed refuses. The code no longer carries a default password. Set the variable in the environment, never in a file that is committed.
3. Pre-deploy check list
Do each before the production deploy. None was run against production when this page was written.
| # | Check | Why |
|---|---|---|
| 1 | Run the hourly stock reconcile @Cron once over rows that have been sold, and confirm the API log shows [success] with no drift flag. | The reconcile compares stock against the ledger. Orders add sale movements; the browser pass could not wait for the top of the hour with orders present. |
| 2 | Run permissions:sync against a database that has no BB_Orders_* rows, and confirm it reports the five rows added and no role pairs granted. | The first run on a clean database is the one that matters. |
| 3 | Build with the swc builder and boot the output: node dist/src/main.js. Confirm it comes up. | A green build does not prove the entry path. The entry is dist/src/main.js. check-types no longer emits into dist, so it can run before or after the build without breaking the boot (see Returns and Refunds Deploy Notes). |
| 4 | Serve Admin #2 over HTTPS. | The new-order form makes its submission key with crypto.randomUUID, which browsers expose only on HTTPS or localhost. Over plain HTTP on another host the form cannot make a key. |
| 5 | Check deployed admin_users for blank or whitespace-only names. | The seed could store a whitespace-only SUPERADMIN_NAME. Order writes read the actor's name, and a blank one makes them answer 500. The failure is loud and records no bad sale. |
Check 5 as a read-only query (not run):
SELECT id FROM admin_users WHERE btrim(coalesce(name, '')) = '';Fix any row it returns before deploy.
4. Intermediate states
| State | Effect |
|---|---|
| New backend, old Admin #2 | Supported. Everything is additive. Old Admin #2 lacks labels for the two new movement reasons; whether a view breaks on them was not traced. |
| Old backend, new Admin #2 | Not supported. A superadmin sees an Orders nav link that answers 404. No data is at risk. |
| Admin #1 and the storefront | No effect. Verify with the orders, inventory and customers counts unchanged. |
5. Rollback
Redeploy the old images. Both migrations are irreversible in practice from the first order: their down scripts refuse once a sale exists, on purpose. After the first order, treat 0065 and 0066 as forward-fix only. Destroying the order, ledger or bb_audit history needs the owner's written decision.
6. See Also
- Orders API reference
- Stock Deploy Notes (phase 3)
- Deploy Notes (phase 1)
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 Returns and Refunds API Reference
Endpoint reference for the bb phase 5 admin returns and refunds API — open and process a return, inspect it into stock, draft and settle refunds, the refund a cancel leaves, with permissions, throttles, idempotency, error codes and known limits.