# MULTI_REGION_I2 Code Review Comparison: `main...MULTI_REGION_I2` ## Findings 1. **[P1] Name searches bypass regional customer isolation** — `packages/db_helper_sqlite/src/admin-apis/user.ts:224` The predicate becomes `(inRegion AND mobile LIKE … OR name LIKE …)`. A regional admin searching by name receives matching customers from other regions, including their mobile numbers. Group the search predicates with Drizzle’s `or()`. 2. **[P1] Web discovery sends the wrong required parameter** — `apps/web-ui/src/hooks/prominent-api-hooks.ts:25–27` The hook sends `{ regionId }`, but `essentialConsts` now requires `{ boundId }`. Every discovery request fails validation after selecting a region, leaving catalog and slot requests disabled. Pass the selected bound and remove the masking `as any`. 3. **[P1] Global-product updates discard existing SKU IDs** — `apps/backend/src/trpc/apis/superadmin-apis/apis/global-products.ts:95–107` `skuInput` does not declare `id`, so Zod strips the IDs before the resolver reads them. Saving existing variants therefore inserts duplicates instead of updating them. Suspending an existing variant creates a suspended duplicate while the original remains purchasable. Preserve optional SKU IDs in the update schema. 4. **[P1] Rejected regional edits can delete a global store’s image** — `apps/backend/src/trpc/apis/admin-apis/apis/store.ts:93–110` The visibility check now accepts global stores, and the resolver deletes their old image before the database helper rejects regional writes. A regional staff member can consequently remove an image shared across all regions despite the mutation failing. Check edit ownership before deleting assets. 5. **[P1] Regional cache requests suppress pending global regeneration** — `apps/backend/src/jobs/cache-creator.ts:25–33` A global request leaves `pendingRegions` unchanged. If a regional request occurs in the same debounce window, the alarm regenerates only that region. For example, a global-product edit followed by a region-A banner edit leaves every other region’s catalog stale. Persist an explicit “all regions pending” flag that regional requests cannot narrow. 6. **[P1] Scheduled web checkout always submits rejected slot IDs** — `apps/backend/src/trpc/apis/user-apis/apis/order.ts:190–194` The new validation rejects null scheduled slots, but `apps/web-ui/src/routes/home.checkout.tsx:93–101` still submits `slotId: null` for every item. Every nonempty scheduled web checkout fails. Forward each cart line’s selected slot ID. 7. **[P1] New-region cache generation is detached from the Worker lifetime** — `apps/backend/src/trpc/apis/superadmin-apis/apis/regions.ts:88–93` Cache generation starts without being awaited, registered with `waitUntil`, or scheduled through the Durable Object. In production, Cloudflare can cancel it after the response, leaving a successfully created region without usable catalog/discovery files. Schedule durable generation or attach the work to the execution context. 8. **[P2] Global-product Edit retains the create form’s draft** — `apps/superadmin-ui/src/routes/global-products.tsx:274–282` Both branches render the same unkeyed component, whose fields initialize only on mount. Clicking Edit therefore retains the empty create draft—or another product’s draft—while submission targets the selected product. Key the form by product ID or explicitly reset its state when the product changes. 9. **[P2] Global ID windows hide orders from quieter regions** — `packages/db_helper_sqlite/src/admin-apis/order.ts:415–427` The new regional filter is combined with a pagination threshold derived from the globally newest order ID. Once another region creates more than `limit + 5` newer orders, a quiet region’s list becomes empty and returns no next cursor, even when it has pending orders. Paginate matching regional rows rather than bounded global ID ranges. 10. **[P2] Startup address selection deletes or rolls back persisted carts** — `apps/user-ui/src/lib/region-switch.ts:34–41` The region store starts null on every launch, but carts remain persisted. Selecting the same address through the startup chooser skips stashing the live carts, clears them, and restores an absent or older snapshot. Persist cart ownership independently of the transient browsing region before performing this reset. 11. **[P2] Unrelated market edits undo regional suspension** — `packages/db_helper_sqlite/src/admin-apis/product.ts:539–545` Regional forms initialize suspension from `sku.isSuspended`, but the response mapper returns the global structural flag instead of `marketStats.isSuspendedInRegion`. Suspend a global SKU regionally, reopen it, and save a price change: this writes false and re-enables it. Return and edit the regional suspension separately. 12. **[P2] Coupon allocation can persist negative sub-order totals** — `packages/shared/cart-pricing.ts:411–423` Each coupon’s shares are rounded independently without respecting each sub-order’s remaining balance. With two ₹101 sub-orders and two stackable ₹101 coupons, the resulting discounts are ₹100 and ₹102, producing totals of ₹1 and **−₹1**. Allocate against remaining per-order balances and reconcile without exceeding them. 13. **[P2] Migration erases historical discount amounts** — `packages/db_helper_sqlite/drizzle/0006_regions_and_superadmins.sql:488–489` Every existing order receives `discount_amount = '0'`, including orders whose stored total already includes a coupon discount. Both new order-detail APIs exclusively read this column, so historical invoices incorrectly report no discount. Backfill the charged discount from historical item totals, delivery charge, and order total. 14. **[P2] Changing a global coupon to flat discount retains its percentage** — `apps/backend/src/trpc/apis/superadmin-apis/apis/global-coupons.ts:137–138` Switching a 20% coupon to ₹50 sends only `flatDiscount`; the update preserves the existing percentage. Both values are stored, and pricing continues prioritizing 20%, despite the successful save. Explicitly clear the opposite discount field and validate the merged coupon. 15. **[P2] Browser reload disables vendor-snippet slot selection** — `apps/admin-web/src/components/VendorSnippetForm.tsx:25–28` Session restoration loads only the JWT, leaving `staff` and `regionId` null. The new query condition then permanently disables slot loading after a reload, making nonpermanent snippet creation impossible until another login. Restore the staff identity or obtain the region from the authenticated staff endpoint. 16. **[P2] Dedicated superadmin login breaks Admin Notifs** — `apps/superadmin-ui/src/lib/auth-context.tsx:37` The new login produces a superadmin-only principal, but the retained notification page calls `admin.staffUser.sendAdminNotification`, whose guard accepts only users or staff. Every send now fails with `UNAUTHORIZED`. Expose an appropriately guarded superadmin notification endpoint and update the caller. ## Verification Notes No files were changed during the review. The test suite and migrations were not run.