# Code Review: MULTI_REGION_I2 against main ## High priority ### 1. [P1] Regional admins can delete shared global-product images **Location:** `apps/backend/src/trpc/apis/admin-apis/apis/product.ts:314–330` A regional admin can resend a global product’s existing images and put their keys in `deletedImageKeys`. Validation passes and deletes the objects, although the subsequent database update permits only regional market-data changes. Every region then has broken image references. **Recommendation:** Enforce structural-edit permissions before deleting images. ### 2. [P1] Creating slots can erase another region’s slot contents **Location:** `apps/backend/src/trpc/apis/admin-apis/apis/slots.ts:127–130` `staleSlotsCleanup()` still selects the globally latest 20 slots and clears the 20th slot’s `skuIds`. Creating slots in region A can therefore empty an active slot in B, without refreshing B’s cache. **Recommendation:** Pass the region into cleanup and scope both its selection and update. ### 3. [P1] Pending scheduled orders become unfulfillable after three calendar days **Location:** `packages/db_helper_sqlite/src/lib/order-items-archive.ts:228–233` Archiving checks creation time but not fulfillment status or delivery date. An order placed several days before delivery loses its relational items; packaging operations subsequently return “not found” because archived orders are read-only. **Recommendation:** Preserve open orders in the archive query, migration backfill, and corresponding admin-list window. ## Correctness issues ### 4. [P2] Editing an address can silently replace its delivery location **Location:** `apps/user-ui/src/components/AddressForm.tsx:172–180` The device-location prefill also runs in edit mode. The addresses screen omits initial coordinates for saved Maps-URL addresses, so editing only their name or phone while elsewhere replaces their coordinates—and potentially their town—with the device’s current location. **Recommendation:** Preserve the saved location and restrict automatic prefilling to new addresses. ### 5. [P2] Restarting can assign a saved cart to the wrong region **Location:** `apps/user-ui/src/hooks/useRegion.ts:358–360, 383–385` Region selection is non-persistent, but active carts persist under unscoped keys. Restarting in A after shopping in B adopts A without reconciling B’s cart. A later switch stashes that cart under A and restores an outdated or empty B snapshot. **Recommendation:** Persist and reconcile the active cart’s owning region during startup. ### 6. [P2] Bound changes can persist another bound’s constants **Location:** `apps/user-ui/src/hooks/prominent-api-hooks.ts:74–79` On a bound change, `initialData` still contains the previous bound’s snapshot for one render. The persistence effect writes that placeholder under the new bound’s key. A failed request or restart can then hydrate incorrect fees, region identity, and cache versions. **Recommendation:** Validate snapshot provenance and avoid persisting placeholder responses. The web counterpart has the same issue at `apps/web-ui/src/hooks/prominent-api-hooks.ts:101–106`. ### 7. [P2] “Suspend in my region” does not update regional suspension **Location:** `apps/admin-ui/src/components/GlobalProductForm.tsx:70–72` The form reads and submits `isSuspended`, but the global-product market-data path only updates `isSuspendedInRegion`. Saving reports success while leaving regional suspension unchanged. **Recommendation:** Read and submit the regional field in both this component and `apps/admin-web/src/components/GlobalProductForm.tsx`. ### 8. [P2] Failed reserved-coupon redemption permanently consumes the secret **Location:** `packages/db_helper_sqlite/src/user-apis/coupon.ts:127–144` The reservation is marked redeemed before inserting the coupon. The project’s D1 `transaction` wrapper does not roll back, so an insertion failure leaves it consumed without granting anything. A concrete case is collision with an ordinary coupon code, which reserved-coupon creation permits. **Recommendation:** Make claiming and issuance atomic or compensate on failure. ### 9. [P2] Migration discards historical discount amounts **Location:** `packages/db_helper_sqlite/drizzle/0006_regions_and_superadmins.sql:576–577` Every existing order receives `discount_amount = '0'`, including discounted orders. Both new order-detail implementations treat this field as authoritative, so historical coupon discounts disappear from their bills. **Recommendation:** Backfill the actual charged discount from existing item totals, delivery charge, and order total. ### 10. [P2] Fractional prices produce different checkout and persisted totals **Location:** `packages/shared/cart-pricing.ts:313–317` Flooring each sub-order’s discount capacity loses fractional rupees. For two ₹99.50 sub-orders and a ₹199 flat coupon, `computeCouponStack` previews a ₹199 discount, but `computeCartPricing` allocates only ₹198 and charges ₹1. **Recommendation:** Use consistent minor-unit arithmetic for previews and allocation. ### 11. [P2] Newly created foreign-region addresses bypass region switching **Location:** `apps/user-ui/components/CartAddressGate.tsx:129–132` Saving an address in B while browsing A only closes the form and refetches addresses. `apps/user-ui/components/CheckoutAddressSelector.tsx:290–297` likewise selects newly created addresses without its existing cross-region confirmation flow. Checkout can retain A’s products/slots with B’s address and fail. **Recommendation:** Route successful creation through the same region-switch flow as existing foreign addresses. ### 12. [P2] Vendor snippets reject valid global SKUs **Location:** `packages/db_helper_sqlite/src/admin-apis/vendor-snippets.ts:113–115, 202–203` These checks require the product’s region to equal the staff region, excluding global products whose region is null. Standalone snippet creation/update rejects them, and existing snippets lose their displayed products after elevation. **Recommendation:** Apply the global-SKU exception already used by slot creation. ### 13. [P2] Elevating a combo leaves regional-only components attached **Location:** `packages/db_helper_sqlite/src/superadmin-apis/products.ts:80–83` Elevation makes the combo global without validating its components. A combo containing A-only products becomes available in B, where cache generation removes those components and publishes an incomplete combo. **Recommendation:** Require global components before elevation or explicitly elevate the dependencies. ### 14. [P2] Global stores show zero products despite having a populated catalog **Location:** `packages/db_helper_sqlite/src/user-apis/stores.ts:25–28` The summary filter excludes every global product. A global store containing global products therefore publishes `productCount: 0` and no previews, although its detail document contains them. **Recommendation:** Include null product regions when collecting active SKUs. ### 15. [P2] Global stores lose all regional category filters **Location:** `apps/backend/src/stores/product-tag-store.ts:116–124` Tag lookup infers the browsing region from store ownership and returns an empty list for global stores. Regional tags attached to those stores never reach their cache documents. **Recommendation:** Pass the browsing region from `scaffoldStoreWithProducts(regionId, storeId)` instead. ### 16. [P2] Global SKU selectors use no region’s price or suspension data **Location:** `packages/db_helper_sqlite/src/user-apis/product.ts:347–349` Although this function receives the caller’s region, it selects market data using the product’s region—which is null for global products. Global SKUs consequently appear priced at ₹0 and ignore regional suspension in admin selectors. **Recommendation:** Select market data using the requested region. ### 17. [P2] Global-product creation fails once there are 12 regions **Location:** `packages/db_helper_sqlite/src/admin-apis/product.ts:472–481` The unchunked insert generates 108 bound parameters for 12 regions, exceeding D1’s 100-parameter limit. Creation then fails after product/SKU rows have already been written. Adding a global SKU repeats this at lines 750–759. **Recommendation:** Chunk both regional market-stat insertions. ## Review execution No test suites or migrations were run, and no files were changed during the review.