From a7ec25997f167db53fda74732596336264905e9b Mon Sep 17 00:00:00 2001 From: iris Date: Fri, 31 Jul 2026 11:12:26 +0200 Subject: [PATCH] admin: reuse the deduped drink_ids in the bar_drinks insert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit argus's review on this PR noted the insert loop recomputed new Set(drink_ids) instead of reusing uniqueIds from the existence check above — same result (deterministic dedup), just needless duplication. Hoisted uniqueIds out of the existence-check block so both call sites share it. Verified: tsc --noEmit and server build clean; re-ran the live smoke test (dup drink_ids dedup to the right set, nonexistent drink_id still 400s). --- server/src/routes/admin.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/src/routes/admin.ts b/server/src/routes/admin.ts index 133902e..b388d8d 100644 --- a/server/src/routes/admin.ts +++ b/server/src/routes/admin.ts @@ -476,6 +476,10 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) { if (pfand_cents !== undefined && !isValidCents(pfand_cents)) { return sendProblem(req, reply, 400, 'Bad Request', 'invalid pfand_cents'); } + // Deduped once here (existence-checked below, and reused as the + // insert order in the transaction) — a repeated id would otherwise + // trip bar_drinks' own (bar_id, drink_id) primary key. + let uniqueIds: number[] = []; if (drink_ids !== undefined) { if (!Array.isArray(drink_ids) || !drink_ids.every(did => Number.isInteger(did))) { return sendProblem(req, reply, 400, 'Bad Request', 'invalid drink_ids'); @@ -484,7 +488,7 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) { // bogus id threw an uncaught FK-constraint error inside the // transaction below (rollback held correctly, but surfaced as a // raw 500 instead of a 400). - const uniqueIds = [...new Set(drink_ids)]; + uniqueIds = [...new Set(drink_ids)]; if (uniqueIds.length > 0) { const placeholders = uniqueIds.map(() => '?').join(','); const found = db @@ -508,9 +512,7 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) { const ins = db.prepare( 'INSERT INTO bar_drinks (bar_id, drink_id, sort_order) VALUES (?, ?, ?)' ); - // Dedupe: a repeated id would otherwise trip bar_drinks' own - // (bar_id, drink_id) primary key. - [...new Set(drink_ids)].forEach((did, idx) => ins.run(id, did, idx)); + uniqueIds.forEach((did, idx) => ins.run(id, did, idx)); } })(); } catch (e: any) {