From ac08731aaf51e434a72dda99265a5de7c6278fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Mon, 15 Jun 2026 19:11:44 -0600 Subject: [PATCH] Fix split convergence: normal order on a split table must not coexist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A diner ordering 'normally' on a table that was already in split created a parallel non-split Order, so the dashboard showed two cards for the same table (e.g. 'Mesa 11 · Dividida' + a separate 'Mesa 11'). That violates convergence. - order POST: if the table already has an active split order, the new order is created with split=true (joins as an unnamed '(table)' subaccount) instead of a parallel normal order. - OrdersTab: a table is treated as split if ANY of its active orders is split, and every active order on that table renders under the single split card — so legacy/ race stray orders never get their own card. Verified: split table 11 + a normal order on 11 → the normal order becomes split=t, zero non-split active orders remain, table-status stays 'split'. Build passes. --- src/app/api/[slug]/order/route.ts | 8 ++++++++ src/components/editor/OrdersTab.tsx | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/app/api/[slug]/order/route.ts b/src/app/api/[slug]/order/route.ts index f1f0c97..a6fb046 100644 --- a/src/app/api/[slug]/order/route.ts +++ b/src/app/api/[slug]/order/route.ts @@ -21,6 +21,13 @@ export async function POST(req: Request, { params }: { params: { slug: string } return NextResponse.json({ error: 'invalid' }, { status: 400 }); } + // Convergence: if the table is already in split mode, a "normal" order must NOT + // coexist as a parallel card — it joins the split account as an unnamed + // ("table") subaccount instead. + const splitActive = await prisma.order.count({ + where: { restaurantId: restaurant.id, tableNumber, status: 'active', split: true }, + }); + const order = await prisma.order.create({ data: { restaurantId: restaurant.id, @@ -28,6 +35,7 @@ export async function POST(req: Request, { params }: { params: { slug: string } items: items as unknown as object, note, status: 'active', + split: splitActive > 0, }, }); diff --git a/src/components/editor/OrdersTab.tsx b/src/components/editor/OrdersTab.tsx index e936415..593c091 100644 --- a/src/components/editor/OrdersTab.tsx +++ b/src/components/editor/OrdersTab.tsx @@ -177,15 +177,25 @@ export default function OrdersTab({ await api(slug, `/orders/${id}`, { method: 'PATCH', json: { paid } }); } + // A table is "in split" if ANY of its active orders is split. Every active + // order on such a table is shown under the one split card (convergence) — even + // a stray non-split order from a race or legacy data never gets its own card. + const splitTableNumbers = useMemo( + () => new Set(orders.filter((o) => o.split).map((o) => o.tableNumber)), + [orders], + ); const splitTables = useMemo(() => { const map = new Map(); for (const o of orders) { - if (!o.split) continue; + if (!splitTableNumbers.has(o.tableNumber)) continue; (map.get(o.tableNumber) ?? map.set(o.tableNumber, []).get(o.tableNumber)!).push(o); } return Array.from(map.entries()).sort((a, b) => a[0] - b[0]); - }, [orders]); - const normalOrders = useMemo(() => orders.filter((o) => !o.split), [orders]); + }, [orders, splitTableNumbers]); + const normalOrders = useMemo( + () => orders.filter((o) => !splitTableNumbers.has(o.tableNumber)), + [orders, splitTableNumbers], + ); return (