fooddelivery: stop carts mixing two restaurants into one checkout
- Commit
69c196543dadf37e703ff032f121ed52025a4c97- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:00 UTC (29 days ago)
- Parents
ea8a644
Symptom: items from several restaurants could be added to one cart,
and checkout then posted every line to the first line's merchant
endpoint (/merchants/{id}/checkout). Another restaurant's item ids
were therefore priced and ordered as if they belonged to that
merchant (or failed pricing opaquely).
Root cause: RestaurantScreen tagged each CartItem with its merchant,
but nothing ever compared it with the cart's existing merchant;
MainActivity appended unconditionally.
Fix: guard the add in FoodDeliveryApp with canAddFromSameMerchant.
Adding from a second restaurant now asks 'Start a new cart?' --
confirming clears the old cart and starts with the new item,
dismissing keeps the existing cart untouched. Nothing is silently
dropped or silently mixed.
Verification: new StoresAndMiscTest.cartOnlyAcceptsOneMerchant
covers empty/same/other/unknown-merchant carts; full module test
run follows in the final verification.Diff
fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.ktindex 7644f0bc82..f2af6b4ceb 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt@@ -9,9 +9,11 @@ import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState+import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.core.content.ContextCompat import com.stripe.android.PaymentConfiguration@@ -22,9 +24,13 @@ import com.vayunmathur.library.ui.IconLocalOffer import com.vayunmathur.library.ui.IconPackage import com.vayunmathur.library.ui.IconPerson import com.vayunmathur.library.ui.IconShoppingCart+import com.vayunmathur.library.ui.AlertDialog+import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.PagerTab import com.vayunmathur.library.ui.TabStyle import com.vayunmathur.library.ui.TabbedPagerScaffold+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.util.ListDetailPage import com.vayunmathur.library.util.ListPage import com.vayunmathur.library.util.MainNavigation@@ -58,6 +64,17 @@ internal fun NavBackStack<Route>.pushDetail(route: Route) { if (backStack.isNotEmpty() && last()::class == route::class) setLast(route) else add(route) } +/**+ * Checkout posts every cart line to a single merchant's endpoint+ * (`/merchants/{id}/checkout`, keyed off the first line's merchant), so a cart may+ * only ever hold one merchant's items. A line with no merchant (0) carries no+ * evidence against the cart's merchant and is allowed through.+ */+internal fun List<CartItem>.canAddFromSameMerchant(item: CartItem): Boolean {+ val cartMerchant = firstOrNull()?.merchantId ?: return true+ return cartMerchant == 0 || item.merchantId == 0 || cartMerchant == item.merchantId+}+ sealed interface Route : NavKey { @Serializable data class Main(val initialTab: Int = 0) : Route @Serializable data class Restaurant(val id: Int) : Route@@ -191,6 +208,35 @@ private fun FoodDeliveryApp( // thread rather than in the remember initialiser. Anything added while the read was in // flight stays in the cart, and is re-persisted alongside the restored lines. val cart = remember { mutableStateListOf<CartItem>() }+ // An item from a second restaurant, held until the user decides whether it+ // replaces the cart they already have (checkout can only order from one).+ var pendingCartItem by remember { mutableStateOf<CartItem?>(null) }+ pendingCartItem?.let { pending ->+ val currentName = cart.firstOrNull()?.merchantName?.ifBlank { "another restaurant" }+ ?: "another restaurant"+ val newName = pending.merchantName.ifBlank { "a different restaurant" }+ AlertDialog(+ onDismissRequest = { pendingCartItem = null },+ title = { Text("Start a new cart?") },+ text = {+ Text(+ "Your cart has items from $currentName. Ordering is from one " ++ "restaurant at a time, so adding from $newName will clear it."+ )+ },+ confirmButton = {+ Button(onClick = {+ cart.clear()+ cart.add(pending)+ CartStore.save(context, cart)+ pendingCartItem = null+ }) { Text("Start new cart") }+ },+ dismissButton = {+ TextButton(onClick = { pendingCartItem = null }) { Text("Keep my cart") }+ },+ )+ } LaunchedEffect(Unit) { val saved = CartStore.getAll(context) if (saved.isEmpty()) return@LaunchedEffect@@ -212,8 +258,12 @@ private fun FoodDeliveryApp( merchantId = route.id, onBack = { backStack.pop() }, onAddToCart = { item ->- cart.add(item)- CartStore.save(context, cart)+ if (cart.canAddFromSameMerchant(item)) {+ cart.add(item)+ CartStore.save(context, cart)+ } else {+ pendingCartItem = item+ } } ) }
fooddelivery/src/test/java/com/vayunmathur/fooddelivery/StoresAndMiscTest.kt
diff --git a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/StoresAndMiscTest.kt b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/StoresAndMiscTest.ktindex 1799079b0f..a63c2c617c 100644--- a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/StoresAndMiscTest.kt+++ b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/StoresAndMiscTest.kt@@ -19,6 +19,7 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue@@ -101,6 +102,16 @@ class StoresAndMiscTest { assertEquals(true, order("""{"channel":"ORDER_CHANNEL_STOREFRONT_DELIVERY"}""").isDelivery) } + @Test fun cartOnlyAcceptsOneMerchant() {+ val burger = CartItem(menuItem = MenuItem(id = 1, name = "Burger"), merchantId = 1)+ val taco = CartItem(menuItem = MenuItem(id = 2, name = "Taco"), merchantId = 2)+ assertTrue(emptyList<CartItem>().canAddFromSameMerchant(burger))+ assertTrue(listOf(burger).canAddFromSameMerchant(burger.copy()))+ assertFalse(listOf(burger).canAddFromSameMerchant(taco))+ // A line with no merchant recorded is no evidence of a second restaurant.+ assertTrue(listOf(burger).canAddFromSameMerchant(CartItem(menuItem = MenuItem(id = 3))))+ }+ @Test fun liveUpdateNotificationBuilds() { OrderLiveUpdate.ensureChannel(ctx) val delivery = OrderLiveUpdate.build(ctx, order("""{"id":9,"merchant":{"id":1,"name":"Burger Barn"}}"""))