admin: reuse the deduped drink_ids in the bar_drinks insert
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).
This commit is contained in:
parent
d6e9979439
commit
a7ec25997f
1 changed files with 6 additions and 4 deletions
|
|
@ -476,6 +476,10 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) {
|
||||||
if (pfand_cents !== undefined && !isValidCents(pfand_cents)) {
|
if (pfand_cents !== undefined && !isValidCents(pfand_cents)) {
|
||||||
return sendProblem(req, reply, 400, 'Bad Request', 'invalid 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 (drink_ids !== undefined) {
|
||||||
if (!Array.isArray(drink_ids) || !drink_ids.every(did => Number.isInteger(did))) {
|
if (!Array.isArray(drink_ids) || !drink_ids.every(did => Number.isInteger(did))) {
|
||||||
return sendProblem(req, reply, 400, 'Bad Request', 'invalid drink_ids');
|
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
|
// bogus id threw an uncaught FK-constraint error inside the
|
||||||
// transaction below (rollback held correctly, but surfaced as a
|
// transaction below (rollback held correctly, but surfaced as a
|
||||||
// raw 500 instead of a 400).
|
// raw 500 instead of a 400).
|
||||||
const uniqueIds = [...new Set(drink_ids)];
|
uniqueIds = [...new Set(drink_ids)];
|
||||||
if (uniqueIds.length > 0) {
|
if (uniqueIds.length > 0) {
|
||||||
const placeholders = uniqueIds.map(() => '?').join(',');
|
const placeholders = uniqueIds.map(() => '?').join(',');
|
||||||
const found = db
|
const found = db
|
||||||
|
|
@ -508,9 +512,7 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) {
|
||||||
const ins = db.prepare(
|
const ins = db.prepare(
|
||||||
'INSERT INTO bar_drinks (bar_id, drink_id, sort_order) VALUES (?, ?, ?)'
|
'INSERT INTO bar_drinks (bar_id, drink_id, sort_order) VALUES (?, ?, ?)'
|
||||||
);
|
);
|
||||||
// Dedupe: a repeated id would otherwise trip bar_drinks' own
|
uniqueIds.forEach((did, idx) => ins.run(id, did, idx));
|
||||||
// (bar_id, drink_id) primary key.
|
|
||||||
[...new Set(drink_ids)].forEach((did, idx) => ins.run(id, did, idx));
|
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue