B2B Second Inventory Returns and Refunds Deploy Notes
Deploy order, migrations 0067 and 0068, the locks they take, the required permissions:sync, the manual grant step and rollback limits for bb phase 5. Phase 5 ships together with phase 4.
B2B Second Inventory (bb) — Returns and Refunds Deploy Notes
These notes cover migrations 0067_bb_returns_refunds and 0068_bb_return_stock_audit. The earlier steps in Deploy Notes (phase 1), Stock Deploy Notes (phase 3) and Orders Deploy Notes (phase 4) still apply. This page adds what phase 5 changes.
Commands marked not run were read against the repo but were not executed against a database when this page was written.
0. Ship with phase 4
Phase 4 and phase 5 go live together (see Orders Deploy Notes). Phase 5 changes order cancel: it now leaves a refund request, and it is refused while a return or an undecided refund exists.
1. Deploy order
- Back up with
pg_dump. Replay0067and0068on 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; the new codes 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_Returns_*andBB_Refunds_*codes. Until then only a superadmin sees Returns and Refunds.
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 |
|---|---|---|
0067_bb_returns_refunds | ACCESS EXCLUSIVE | bb_order, bb_order_item, bb_order_event |
0067_bb_returns_refunds | SHARE ROW EXCLUSIVE (new foreign keys) | admin_users |
0068_bb_return_stock_audit | ACCESS EXCLUSIVE | bb_inventory_movement, bb_audit |
Every order and staff write blocks until the batch commits. A running import holds a lock on the 0068 tables for its whole transaction, so the batch aborts until the import ends. 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 permission row. permissions:sync creates five codes for each of BB_Returns and BB_Refunds: CREATE, READ, UPDATE, DELETE and RESTORE. It grants them to nobody. Without it the Roles screen has no Returns or Refunds 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 returns list and detail | BB_Returns_READ |
| Open, decide, receive, inspect or close a return | BB_Returns_UPDATE (with READ to see the list) |
| Open the refunds list | BB_Refunds_READ |
| Draft, approve, reject, mark not owed or settle a refund | BB_Refunds_UPDATE (with READ to see the list) |
BB_Returns_CREATE, DELETE, RESTORE and the same three for BB_Refunds are not checked by any route. BB_Orders_UPDATE does not give the right to approve money; that is BB_Refunds_UPDATE on purpose.
2. Pre-deploy check list
| # | Check | Why |
|---|---|---|
| 1 | Build with the swc builder and boot the output: node dist/src/main.js. check-types may run before or after the build. | check-types used to run tsc -b and emit JS with unresolved @/ imports into dist/src, which broke the boot. It now runs tsc --noEmit --composite false --incremental false (apps/api/package.json:17) and leaves dist byte-identical; the W10b backend note shows the dist hash unchanged across a run. The browser pass BP2 built, ran check-types after the build (no errors), then booted node dist/src/main.js against a scratch database: it came up, a superadmin login answered 201 and /health/ready kept answering, with no "Cannot find module". |
| 2 | Run permissions:sync against a database with no BB_Returns_* or BB_Refunds_* rows and confirm it adds ten rows and grants no role. | The first run on a clean database is the one that matters. Not run against production. |
| 3 | Read the pipeline's first real run. | .gitlab-ci.yml now filters on @nano-store/fumadocs, so check-types and build:docs can start. The pipeline has not been run with this fix, so a green local check-types is not yet a passing pipeline. |
3. Intermediate states
| State | Effect |
|---|---|
| New backend, old Admin #2 | Supported for the existing screens. The old order screen does not show the refund a cancel now leaves, and has no Returns or Refunds screen. |
| Old backend, new Admin #2 | Not supported. The Returns and Refunds links answer 404. No data is at risk. |
| Admin #1 and the storefront | No effect. The retail return and refund tables are untouched; verify the retail counts unchanged. |
4. Rollback
Redeploy the old images. Both migrations are reversible only before the first return or refund. Their down scripts (down/0067_bb_returns_refunds.down.sql, down/0068_bb_return_stock_audit.down.sql) refuse once a return, a refund, a refunded order, a return movement or a return or refund audit row exists, and name the counts. Run 0068's down first. These rows are frozen and never hard-deleted, so after the first use treat 0067 and 0068 as forward-fix only. Getting past the guard means truncating the record of money owed or the audit trail, and needs the owner's written decision.
migrate-down.ts refuses production URLs; a production rollback is by hand.
5. See Also
- Returns and Refunds API reference
- Orders Deploy Notes (phase 4)
- Stock Deploy Notes (phase 3)
- Deploy Notes (phase 1)
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.
B2B Second Inventory Reports Feature
What the bb phase 6 reports show, who may open them, the surfaces in Admin