Modern Apps git

← Modern-Apps.git

fooddelivery: checkout drafts no longer leak across restaurants

Commit
6d09a065d62be42fb8e0c28613fe0e7c577c51c4
Author
Vayun Mathur <[email protected]>
When
2026-11-30 21:53 UTC (5 days ago)
Parents
5b4e6b3
The persisted checkout draft carries merchantId but nothing
read it. After process death during a payment sheet, starting a
cart at a different restaurant let the stale draft fire against
the new cart: a placed draft cleared the new unpaid cart and
showed the old restaurant's success screen; an unplaced one
POSTed merchant 7's order UUID to merchant 9's checkout (the
foreign-order hazard Models.kt already warns about); a
payment-presented one blocked the new checkout entirely.

checkoutDraftAction now takes the current merchant: a
merchant-bound draft that doesn't match — including merchant
not yet known (cart unrestored, 0) — returns None, and the draft
stays persisted so returning to its own restaurant's cart can
still resume it. CheckoutScreen hoists merchantId above the entry
effect and keys the effect on it, so reconciliation re-runs when
the restored cart lands. New tests cover cross-merchant and
deferred cases. Cart totals, menu/search, order stages and the
lookup intent were audited and cleared. Verified:
:fooddelivery:testDevUnitTest 168 tests pass; lint errors are
pre-existing on untouched files.

Diff

fooddelivery/src/main/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStore.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStore.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStore.ktindex 35c2f18b40..5aa3545a6b 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStore.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStore.kt@@ -47,9 +47,23 @@ enum class CheckoutDraftAction { RestorePlaced, CheckOrders, ResumeDraft, None }  * PaymentSheet was presented but whose order is not placed has an unknown payment  * outcome, so it reconciles to "check your orders" rather than re-presenting or  * re-creating; anything else simply resumes the same draft order uuid.+ *+ * The draft only ever applies to the merchant it was created for: its order uuid+ * belongs to that merchant's checkout, so applying it to a different merchant's+ * cart would either post the uuid to the wrong merchant's endpoint (ResumeDraft)+ * or declare the other merchant's already-placed order a success for this cart+ * and clear it unpaid (RestorePlaced). A mismatched draft is left persisted, not+ * applied — switching back to its merchant's cart can still resume it. While the+ * current cart's merchant is unknown (0, cart not restored yet) a merchant-bound+ * draft likewise defers rather than applying blind.  */-fun checkoutDraftAction(draft: CheckoutDraft?, existingOrderFound: Boolean): CheckoutDraftAction = when {+fun checkoutDraftAction(+    draft: CheckoutDraft?,+    existingOrderFound: Boolean,+    currentMerchantId: Int,+): CheckoutDraftAction = when {     draft == null -> CheckoutDraftAction.None+    draft.merchantId != 0 && draft.merchantId != currentMerchantId -> CheckoutDraftAction.None     existingOrderFound -> CheckoutDraftAction.RestorePlaced     draft.paymentPresented -> CheckoutDraftAction.CheckOrders     else -> CheckoutDraftAction.ResumeDraft
fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutScreen.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutScreen.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutScreen.ktindex 90c7de9d06..b320b03474 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutScreen.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutScreen.kt@@ -83,6 +83,8 @@ fun CheckoutScreen(     var selectedAddress by remember { mutableStateOf<SavedAddress?>(null) }     /** Address id restored from a persisted checkout draft, applied once addresses load. */     var restoredAddressId by remember { mutableStateOf<String?>(null) }+    /** Merchant this checkout is for: the cart's first line, or 0 while it is unrestored/empty. */+    val merchantId = items.firstOrNull()?.merchantId ?: 0      LaunchedEffect(Unit) {         val all = AddressStore.getAll(context)@@ -103,13 +105,16 @@ fun CheckoutScreen(     // success/tracking path, never pay again); presented-but-not-found means the     // payment outcome is unknown (check Orders, don't re-create or re-present);     // otherwise resume the same draft uuid so re-pricing UPDATEs it in place.-    LaunchedEffect(Unit) {+    // Keyed on merchantId: entry can compose before the cart restore lands, and a+    // persisted draft must only be reconciled once the cart's merchant is known —+    // and only against that merchant (see checkoutDraftAction).+    LaunchedEffect(merchantId) {         val draft = CheckoutDraftStore.get(context) ?: return@LaunchedEffect         AppInit.awaitReady()         val existing = draft.orderUuid?.takeIf { it.isNotBlank() }?.let { uuid ->             BitesOrders.getOrders().firstOrNull { it.uuid == uuid }         }-        when (checkoutDraftAction(draft, existing != null)) {+        when (checkoutDraftAction(draft, existing != null, merchantId)) {             CheckoutDraftAction.RestorePlaced -> {                 CheckoutDraftStore.clear(context)                 if (existing != null) {@@ -129,7 +134,7 @@ fun CheckoutScreen(                     selectedAddress = addresses.firstOrNull { it.id == draft.addressId } ?: selectedAddress                 }                 lastOrderUuid = draft.orderUuid-                if (checkoutDraftAction(draft, existing != null) == CheckoutDraftAction.CheckOrders) {+                if (checkoutDraftAction(draft, existing != null, merchantId) == CheckoutDraftAction.CheckOrders) {                     error = "Your last payment may have completed. Check Orders before paying again."                 }             }@@ -152,7 +157,6 @@ fun CheckoutScreen(     /** Reference formula: component sum minus the reward credit applied to this order. */     val rewardsApplied = (rewards?.rewardsAvailable ?: 0) / 100.0     val payTotal = confirmedOrder?.let { it.componentTotal - rewardsApplied }-    val merchantId = items.firstOrNull()?.merchantId ?: 0     val canFetch = items.isNotEmpty() && (isPickup || selectedAddress != null)      // Deals the merchant currently has running; picking one sends its dealId with checkout.
fooddelivery/src/test/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStoreTest.kt
diff --git a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStoreTest.kt b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStoreTest.ktindex 358d2aa21c..072dc946c6 100644--- a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStoreTest.kt+++ b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/data/CheckoutDraftStoreTest.kt@@ -59,10 +59,29 @@ class CheckoutDraftStoreTest {     }      @Test fun reconcileDecisionMatrix() {-        assertEquals(CheckoutDraftAction.None, checkoutDraftAction(null, existingOrderFound = false))-        assertEquals(CheckoutDraftAction.RestorePlaced, checkoutDraftAction(draft(), existingOrderFound = true))+        assertEquals(CheckoutDraftAction.None, checkoutDraftAction(null, existingOrderFound = false, currentMerchantId = 7))+        assertEquals(CheckoutDraftAction.RestorePlaced, checkoutDraftAction(draft(), existingOrderFound = true, currentMerchantId = 7))         // Presented but not placed: unknown outcome, check orders — never re-create.-        assertEquals(CheckoutDraftAction.CheckOrders, checkoutDraftAction(draft(presented = true), existingOrderFound = false))-        assertEquals(CheckoutDraftAction.ResumeDraft, checkoutDraftAction(draft(presented = false), existingOrderFound = false))+        assertEquals(CheckoutDraftAction.CheckOrders, checkoutDraftAction(draft(presented = true), existingOrderFound = false, currentMerchantId = 7))+        assertEquals(CheckoutDraftAction.ResumeDraft, checkoutDraftAction(draft(presented = false), existingOrderFound = false, currentMerchantId = 7))+    }++    @Test fun reconcileIgnoresDraftFromAnotherMerchant() {+        // Draft was created for merchant 7 (see draft()); the cart now holds+        // merchant 9. None of the draft's actions may fire for this cart:+        // RestorePlaced would clear merchant 9's unpaid cart as if merchant 7's+        // placed order were its success, ResumeDraft would post merchant 7's+        // order uuid to /merchants/9/checkout, and CheckOrders would block+        // merchant 9's checkout over merchant 7's unknown payment outcome.+        assertEquals(CheckoutDraftAction.None, checkoutDraftAction(draft(), existingOrderFound = true, currentMerchantId = 9))+        assertEquals(CheckoutDraftAction.None, checkoutDraftAction(draft(presented = true), existingOrderFound = false, currentMerchantId = 9))+        assertEquals(CheckoutDraftAction.None, checkoutDraftAction(draft(presented = false), existingOrderFound = false, currentMerchantId = 9))+    }++    @Test fun reconcileDefersMerchantDraftUntilCartMerchantKnown() {+        // Cart not restored yet (merchant 0): do not apply a merchant-bound+        // draft blind; the merchant-keyed entry effect re-runs once it lands.+        assertEquals(CheckoutDraftAction.None, checkoutDraftAction(draft(), existingOrderFound = true, currentMerchantId = 0))+        assertEquals(CheckoutDraftAction.None, checkoutDraftAction(draft(presented = false), existingOrderFound = false, currentMerchantId = 0))     } }