9.1 KiB
MULTI_REGION_I2 code review
Scope and result
- Comparison:
main...HEAD - Reviewed HEAD:
d892cfbc2b1c6c4b2bccaf32b368013393a8e139 - Base main:
a09cb936c09d728d76063023b4ec89aed426e8f8 - Focus: discovery contracts, global-product editing, regional market data, and coupon pricing/checkout.
- Result: 7 confirmed outstanding findings: 3 P1 and 4 P2.
- This is a static review, not an exhaustive validation of every changed file. No test suites, builds, or migrations were run during this re-review. No application code was changed.
- The unrelated uncommitted Expo cache file was excluded from the branch review.
P1 means a high-priority functional or financial correctness issue. P2 means a normal-priority correctness issue under the described scenario.
Resolved finding from the previous review
Shared pricing exports are now committed
packages/shared/index.ts:22–23 now exports ./cart-pricing. The previous finding that a clean checkout lacks the shared pricing exports is resolved and is not counted below.
Outstanding findings
R1 — [P1] Web discovery sends regionId where the backend requires boundId
Location: apps/web-ui/src/hooks/prominent-api-hooks.ts:25–27
Scenario: Open the web storefront and select a browsing region.
The hook calls common.essentialConsts with { regionId }. The backend input schema in apps/backend/src/trpc/apis/common-apis/common-trpc-index.ts:267–278 requires a positive boundId. The as any bypasses compile-time checking but not Zod validation, so the discovery request fails. With no discovery response, the cache URL builders cannot obtain the asset domain and version values, blocking catalog, availability, and slot loading on a fresh session.
Suggested fix: Resolve the applicable delivery/browsing bound, send { boundId }, and enable the query only when that bound is available. Remove the cast hiding the contract mismatch. Keep the bound in the query input so switching bounds refreshes delivery constants.
Verification: A fresh web session can load the selected region's catalog, and switching between bounds refreshes the bound-specific delivery settings.
R2 — [P1] Global-product updates strip existing SKU IDs and insert duplicates
Location: apps/backend/src/trpc/apis/superadmin-apis/apis/global-products.ts:95–107
Scenario: A superadmin saves an existing global product after changing an image, feature, or suspension flag.
The update schema uses skuInput, which does not declare an id field. Zod strips unknown properties, including the SKU IDs sent by the UI. Consequently (sku as any).id is undefined in the handler. The database helper's existing-SKU branch at packages/db_helper_sqlite/src/admin-apis/product.ts:663–674 is skipped, and the new-SKU branch at lines 731–751 inserts duplicate variants and market-stat rows. The original variants remain unchanged; an original SKU can remain orderable even when the intended edit suspends its replacement.
Suggested fix: Define an update SKU schema that preserves an optional positive integer id, and map its typed field directly. Validate that supplied existing IDs belong to the edited product; only variants intentionally lacking an ID should be inserted.
Verification: Editing an existing variant preserves its ID and the product's SKU count. Adding one new variant increases the count by exactly one.
R3 — [P1] Overlapping targeted coupons can create negative sub-order totals
Location: packages/shared/cart-pricing.ts:158–181
Scenario: A scheduled cart has ₹100 of SKU A in one slot and ₹900 of unrelated goods in another slot. Two nonexclusive ₹100 coupons both target SKU A.
Each coupon is calculated against the original ₹100 eligible subtotal and allocates ₹100 to the first sub-order. The combined ₹200 discount is below the ₹1,000 whole-cart subtotal, so the global cap does not run. The first sub-order receives ₹200 of discount on ₹100 of goods and stores a −₹100 total; the unrelated sub-order still costs ₹900. This effectively uses the targeted coupons to offset goods outside their targets and persists an invalid negative payable sub-order.
Suggested fix: Track remaining discountable value per line when applying coupons, allocating only discounts that those eligible lines can absorb. Derive coupon shares, sub-order discounts, and the cart total from the same capped allocation. A whole-cart cap or a final zero clamp alone does not enforce product targeting.
Verification: In the example, SKU A can receive at most ₹100 combined discount; the unrelated goods remain ₹900 and every sub-order total is nonnegative. Also verify partially overlapping target sets.
R4 — [P2] Mobile cart and checkout previews omit the combined discount cap
Locations:
apps/user-ui/components/checkout-page.tsx:230–244apps/user-ui/components/cart-page.tsx:434–448
Scenario: A ₹100 cart has two eligible nonexclusive ₹80 flat coupons.
The UI sums the individual discounts to ₹160 and displays a −₹60 goods total before delivery. The backend's computeCartPricing caps the combined discount at ₹100. The customer therefore sees a different payable amount from the stored order, and delivery-threshold calculations can also diverge because they use the uncapped preview total.
Suggested fix: Use the shared cart-pricing result in both screens instead of independently summing computeCouponDiscount results. Ensure the pricing lines are the same available items that will actually be submitted.
Verification: Cart, checkout, and stored order agree when multiple coupons exceed the goods subtotal, including cases where the delivery threshold is affected.
R5 — [P2] Reopening a global product loses its regional suspension state
Location: packages/db_helper_sqlite/src/admin-apis/product.ts:198–200
Scenario: A regional admin enables “Suspend in my region” for a globally unsuspended SKU, saves it, reopens it, and saves an unrelated price edit.
mapSku returns sku.isSuspended but omits marketStats.isSuspendedInRegion. The regional form initializes its checkbox from sku.isSuspended (apps/admin-ui/src/components/GlobalProductForm.tsx:37–48), so the saved regional suspension appears false after reopening. Submitting the price edit sends false, and updateProductMarketDataForRegion at packages/db_helper_sqlite/src/admin-apis/product.ts:535–541 clears the regional suspension, making the SKU available again.
Suggested fix: Expose global and regional suspension flags separately in the admin response. Initialize and submit the regional checkbox using isSuspendedInRegion, and represent the global kill-switch independently. Apply the same distinction in both regional admin clients.
Verification: A regional suspension survives reopening and unrelated edits. A global suspension does not incorrectly initialize or write the regional flag.
R6 — [P2] Prices Overview rejects global SKUs included in its listing
Location: packages/db_helper_sqlite/src/admin-apis/product.ts:1255–1258
Scenario: A regional admin edits a global SKU's price or flash availability through Prices Overview, either alone or in a batch with regional SKUs.
The listing includes global products, but bulk-price validation only accepts products whose regionId equals the staff region. Global products have a null region, so they are reported as invalid and the entire batch is rejected. This also prevents valid regional changes in the same batch from being saved. The subsequent market-stat writes are already scoped to the authenticated region.
Suggested fix: Accept SKUs whose parent product is global or belongs to the authenticated region, while continuing to reject another region's regional SKUs. Keep the existing region-scoped writes.
Verification: A mixed global/regional batch saves only the authenticated region's market data; another region's regional SKU remains rejected.
R7 — [P2] Targeted-coupon eligibility still compares minOrder with the whole cart
Locations:
apps/user-ui/components/checkout-page.tsx:182–185apps/user-ui/components/cart-page.tsx:284–286
Scenario: A coupon requires ₹200 of its targeted SKU, but the cart contains only ₹50 of that SKU plus ₹300 of unrelated goods.
Both screens mark the coupon eligible because they compare minOrder with the whole-cart subtotal. The backend correctly compares it with the ₹50 eligible subtotal in packages/db_helper_sqlite/src/user-apis/order.ts:187–202, so placing the order fails after the UI has offered and displayed the coupon. Coupons with no matching cart lines are also not disabled by this eligibility calculation.
Suggested fix: Use computeEligibleSubtotal over the actual effective-price, available checkout lines for both the minimum requirement and target-match check. Align the cart and checkout eligibility rules with server validation.
Verification: The example coupon is disabled with the appropriate minimum explanation; a targeted coupon with no matching items is unavailable; flash-priced eligible lines use their flash price.