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) {