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 (