Nano Store - Ecommerce Docs
Developer Resourcesb2b-second-inventory

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

  1. Back up with pg_dump. Replay 0065 and 0066 on a copy of the production dump first, as in Deploy Notes.
  2. Migrate, in a quiet window with no import running: pnpm db:migrate.
  3. Deploy the API. The old Admin #2 keeps working; BB_Orders_* codes that are not yet present are invisible to it.
  4. Sync permissions: pnpm --filter @nano-store/api permissions:sync. Required.
  5. Deploy Admin #2 after the backend.
  6. 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'.

MigrationLockTables
0065_bb_ordersSHARE ROW EXCLUSIVEbb_customers, bb_product_variant, admin_users
0066_bb_order_stock_auditACCESS EXCLUSIVE, with a full validating scanbb_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 roleGrant
Open the list, detail and receiptBB_Orders_READ
Create an order and use the pickersBB_Orders_CREATE (create-only staff see no list; they need READ to read their own order)
Add a note or cancelBB_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.

#CheckWhy
1Run 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.
2Run 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.
3Build 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).
4Serve 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.
5Check 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

StateEffect
New backend, old Admin #2Supported. 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 #2Not supported. A superadmin sees an Orders nav link that answers 404. No data is at risk.
Admin #1 and the storefrontNo 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