fix(reservations): scope the per-day position write to the trip

updatePositions has two branches. The legacy one updates
`WHERE id = ? AND trip_id = ?`; the per-day one, three lines below it, bound
the caller's ids straight into reservation_day_positions and never touched the
tripId it was handed. That table has no trip_id of its own and its two foreign
keys only require the ids to exist, so nothing further down caught a pair that
did not belong together.

The trip now comes from the join, so the row materialises only when the
reservation and the day agree on it. Doing that in the statement rather than as
a pre-check also turns a stale id into a quiet no-op instead of a foreign-key
error surfacing as a 500, and lets an omitted day_plan_position bind the way
the wire contract has always said it may.

reorder_reservations reports a day that is not on the trip, the way its sibling
tools in that file already do, and a migration clears rows whose reservation
and day disagree.
This commit is contained in:
Maurice
2026-08-16 21:37:21 +02:00
committed by Maurice
parent 688eea23a4
commit ff27624e02
6 changed files with 150 additions and 12 deletions
+18
View File
@@ -3891,6 +3891,24 @@ function runMigrations(db: Database.Database): void {
db.exec('ALTER TABLE budget_items ADD COLUMN place_id INTEGER REFERENCES places(id) ON DELETE SET NULL DEFAULT NULL');
}
},
// A reservation_day_positions row only makes sense when its reservation and
// its day are on the same trip. The table carries no trip_id and its two
// foreign keys only ask that the ids exist, so pairs that never belonged
// together could accumulate; the writer refuses them now, and this clears
// whatever an older build let through. Appended LAST — the array is
// index-addressed against schema_version.
() => {
db.exec(`
DELETE FROM reservation_day_positions
WHERE rowid IN (
SELECT rdp.rowid
FROM reservation_day_positions rdp
JOIN reservations r ON r.id = rdp.reservation_id
JOIN days d ON d.id = rdp.day_id
WHERE d.trip_id <> r.trip_id
)
`);
},
];
if (currentVersion < migrations.length) {
@@ -468,6 +468,13 @@ export class ReservationsMcp {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
if (!this.reservations.verifyTripAccess(tripId, ctx.userId)) return noAccess();
if (!this.guards.hasTripPermission('reservation_edit', tripId, ctx.userId)) return permissionDenied();
// The service scopes the write to the trip on its own, so a foreign id is
// already harmless — say so rather than reporting a success that moved
// nothing, the way the sibling tools above do.
if (dayId && !this.days.getDay(dayId, tripId))
return errorResult('dayId does not belong to this trip.');
this.reservations.updatePositions(tripId, positions, dayId);
this.guards.safeBroadcast(tripId, 'reservation:positions', { positions, dayId });
return ok({ success: true });
@@ -527,13 +527,26 @@ export class ReservationsService {
return { reservation, accommodationCreated };
}
updatePositions(tripId: string | number, positions: { id: number; day_plan_position: number }[], dayId?: number | string | null) {
updatePositions(tripId: string | number, positions: { id: number; day_plan_position?: number }[], dayId?: number | string | null) {
if (dayId) {
// Per-day positions for multi-day reservations
const stmt = this.db.prepare('INSERT OR REPLACE INTO reservation_day_positions (reservation_id, day_id, position) VALUES (?, ?, ?)');
// Per-day positions for multi-day reservations, scoped the way the legacy
// branch below already scopes its update. The table carries no trip_id and
// its two foreign keys only ask that the ids exist, so the trip has to come
// from the join: the row materialises only when the reservation and the day
// agree on it. Doing that in the statement rather than as a pre-check also
// makes a stale id a quiet no-op instead of a foreign-key error surfacing
// as a 500.
const stmt = this.db.prepare(`
INSERT OR REPLACE INTO reservation_day_positions (reservation_id, day_id, position)
SELECT r.id, d.id, ?
FROM reservations r
JOIN days d ON d.trip_id = r.trip_id
WHERE r.id = ? AND d.id = ? AND r.trip_id = ?
`);
this.db.transaction(() => {
for (const item of positions) {
stmt.run(item.id, dayId, item.day_plan_position);
// position is NOT NULL while the wire contract leaves the value optional.
stmt.run(item.day_plan_position ?? 0, item.id, dayId, tripId);
}
});
} else {