# Unfixed issues — MULTI_REGION_I2 ## 3. [P1] Pending scheduled orders become unfulfillable after three calendar days **Location:** `packages/db_helper_sqlite/src/lib/order-items-archive.ts:224–233` **Still present:** `listEligibleOrderIds` selects orders only by creation time: ```ts sql`datetime(${orders.createdAt}) < datetime(${cutoff.toISOString()})` ``` It does not exclude orders that are still open (not delivered/cancelled) or whose delivery slot is in the future. Once archived, `order_items` rows are deleted and the order becomes read-only, so packaging/fulfilment operations hit the archived path and return “not found”. **Failure scenario:** An order is placed several days before its scheduled delivery (or is simply still pending after three calendar days). The nightly archive job moves its items into `orders.items_json`, after which the admin can no longer package or fulfil it. **Recommended fix:** Add a fulfilment guard to the archive query (and the `0006` migration snapshot): skip orders whose status is not terminal and/or whose slot delivery time is still in the future. Mirror the same window in the admin order list so archived orders and open orders never overlap incorrectly. --- ## 5. [P2] Restarting can assign a saved cart to the wrong region **Location:** `apps/user-ui/src/hooks/useRegion.ts:358–360, 383–385` **Still present:** On startup, when `regionId == null`, both branches adopt the resolved region “without clearing the cart”: ```ts if (regionId == null) { // First region of this launch — adopt it without clearing the cart. setRegion(region, anchorAddress?.boundId ?? null) } ``` The active cart is still persisted under unscoped keys (`cart_items` / `flash_cart_items` — see `apps/user-ui/hooks/cart-query-hooks.tsx`), with no recorded owning region. **Failure scenario:** Shop in region B, leave items in the cart, terminate the app, then restart where GPS/default address resolves to region A. B's cart is adopted as A's cart. Switching back to B later stashes that cart under A and restores B's older/empty snapshot, losing the latest B cart. **Recommended fix:** Persist the owning region with the active cart (or add a `cart_region` marker). During startup adoption, if the persisted cart belongs to another region, stash it under that region before `setRegion`, and restore the new region's snapshot. --- ## 9. [P2] Migration discards historical discount amounts **Location:** `packages/db_helper_sqlite/drizzle/0006_regions_and_superadmins.sql:576–577` **Still present:** The orders rebuild hard-codes the discount to `'0'`: ```sql SELECT ..., o.`total_amount`, o.`delivery_charge`, '0', o.`readable_id`, ... ``` Both order-detail implementations read `orders.discount_amount` as the authoritative discount, so every historical coupon discount disappears from existing order bills after the migration. **Recommended fix:** Backfill the real charged discount in the migration. It can be derived from the pre-migration order/item totals (e.g. sum of item totals + delivery charge − `total_amount`, clamped at 0), or copied from the old order discount source if one exists. Historical orders must keep their final payable amount unchanged. --- ## 10. [P2] Fractional prices produce different checkout and persisted totals **Location:** `packages/shared/cart-pricing.ts:313–317` **Still present:** `allocateBounded` floors each sub-order's discount capacity: ```ts const limits = caps.map((cap) => Math.max(0, Math.floor(cap))) ``` `computeCouponStack` previews a discount on fractional prices, but `computeCartPricing` can allocate less after flooring the per-sub-order caps. **Failure scenario:** Two ₹99.50 sub-orders with a ₹199 flat coupon. `computeCouponStack` previews ₹199 off, while `computeCartPricing` allocates ₹99 to each sub-order (₹198 total) and charges ₹1 more than the preview. **Recommended fix:** Use one arithmetic model for previews and allocation — either do all allocation in paise/minor units and round only at the boundary, or make `computeCouponStack` apply the same per-cap flooring rule so the preview matches the persisted totals exactly. --- ## 11. [P2] Newly created foreign-region addresses bypass region switching **Location:** `apps/user-ui/components/CartAddressGate.tsx:129–132`; `apps/user-ui/components/CheckoutAddressSelector.tsx:290–297` **Still present:** Both success handlers accept a newly created address without applying the region the address form resolved: - `CartAddressGate` closes the form and refetches addresses. - `CheckoutAddressSelector` closes the dialog, selects the new address id, and invalidates queries. The existing-address click path already routes through `confirmRegionSwitch` (`CheckoutAddressSelector.tsx:116`); the create path does not. **Failure scenario:** Browsing region A, the customer uses “Choose another location” and saves an address in region B. The app keeps region A's products and slots while selecting the B address, so checkout submits conflicting region / address data and fails (or quotes the wrong products). **Recommended fix:** Consume `AddressForm`'s resolved region on create. If it differs from the browsing region, call the same `confirmRegionSwitch` flow used for existing foreign addresses before selecting the new address (in both the gate and the checkout selector). --- ## 12. [P2] Vendor snippets reject valid global SKUs **Location:** `packages/db_helper_sqlite/src/admin-apis/vendor-snippets.ts:113–115, 202–203` **Still present:** Both checks require the product's region to equal the staff region: ```ts const foreign = rows.filter((r: any) => r.product?.regionId !== regionId) // ... .filter((sku) => !sku.isDeleted && (regionId == null || sku.product?.regionId === regionId)) ``` A global product's `regionId` is `null`, which `!== regionId`, so global SKUs are rejected on snippet create/update and dropped from snippet listings. Slot creation already allows global SKUs, so this is inconsistent. **Failure scenario:** An admin creates a vendor snippet containing a global SKU → 400 “belongs to a different region”. Existing snippets that reference a SKU that was later elevated stop displaying that product. **Recommended fix:** Use the same region rule as slots: `sku.product?.regionId == null || sku.product?.regionId === regionId`. Update both `assertSnippetSkuIdsInRegion` and the listing filter. --- ## Verification notes - Statuses above were verified by reading the current working tree. - No test suites were run while producing this list.