Fix split convergence: normal order on a split table must not coexist

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.
This commit is contained in:
Jorem Juárez committed 2026-06-15 19:11:44 -06:00
1 parent 1dfdecb262
commit ac08731aaf
2 files changed
+21 -3

No files matched your search

+8
View File
@@ -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,
},
});
+13 -3
View File
@@ -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<number, Order[]>();
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 (
<div className="flex flex-col gap-4">