fix(roadtrip): seat the nights booked before this release where their check-in says

A booked night leads its day now, but the stops the bookings put on their
check-in day before this release sit last, and the backfill for older
bookings appended them too. Every trip planned before the change kept that
order until somebody edited the check-in.

A migration seats every night a booking owns by the same rule the service
applies: first, behind only a stop whose own clock is at or before the
check-in. The drawn roads follow the stops they were drawn after, by the
rules the service uses when it moves a night. Stops the traveller placed
themselves are not touched. Re-runnable.
This commit is contained in:
Maurice
2026-09-20 18:45:37 +02:00
parent a86e4b560a
commit 417d0445b9
3 changed files with 340 additions and 0 deletions
+14
View File
@@ -2,6 +2,7 @@ import { ROADTRIP_PREFERENCE_KEYS } from '@trek/shared';
import { readEnv } from '../app-config';
import { encrypt_api_key } from '../nest/common/crypto/apiKeyCrypto';
import { seedDocumentProviders } from './document-provider-seed';
import { reseatBookedNights } from './reseat-booked-nights';
import Database from 'better-sqlite3';
import fs from 'fs';
@@ -5212,6 +5213,19 @@ function runMigrations(db: Database.Database): void {
ALTER TABLE roadtrip_day_boundaries_new RENAME TO roadtrip_day_boundaries;
`);
},
/*
* Seat every booked night where its check-in says, the way a night booked
* today is seated. The night leads its day now; the stops a booking put on
* their check-in day before this release sit last, and the trips people
* already have would keep that order until somebody edits the check-in.
* The rules, and the drawn roads that follow the stops, are in
* reseat-booked-nights.ts. Re-runnable.
*/
() => {
const seated = reseatBookedNights(db);
if (seated > 0) console.log(`[DB] Seated ${seated} booked night(s) at the head of their day`);
},
];
if (currentVersion < migrations.length) {
+115
View File
@@ -0,0 +1,115 @@
import Database from 'better-sqlite3';
type DayRow = { id: number; at: string | null; located: number };
type Via = { id: number; after_order_index: number; sequence: number };
/**
* Seat every booked night where its check-in says, the way a night booked today is
* seated (AccommodationsService.positionForCheckIn).
*
* The stop a booking puts on its check-in day used to go last unless a stop pinned to
* a later hour pulled it forward, and the backfill that gave older bookings their stop
* appended it too. The night leads its day now: first, behind only a stop whose own
* clock is at or before the check-in, with the stops that carry no hour following it.
* Without this step every trip planned before the change keeps the old order until
* somebody edits the check-in.
*
* Only the stops a booking owns move (accommodation_id). A stop the traveller placed
* themselves and then booked a night on is theirs, wherever it sits. The drawn roads
* (roadtrip_vias) are pinned to positions among the day's located stops, so they are
* carried along by the rules the service applies (AccommodationsService.reanchorVias):
* a via follows the stop it was drawn after, a via behind a stop that is last stays
* with it, and one left without a leg goes.
*
* Runs inside the migration's transaction. Re-runnable: a night already in its seat is
* passed over. Returns how many nights moved.
*/
export function reseatBookedNights(db: Database.Database): number {
const nights = db.prepare(`
SELECT da.id, da.day_id, a.check_in
FROM day_assignments da
JOIN day_accommodations a ON a.id = da.accommodation_id
ORDER BY da.day_id, da.order_index, da.id
`).all() as Array<{ id: number; day_id: number; check_in: string | null }>;
const dayRows = db.prepare(`
SELECT da.id, COALESCE(da.assignment_time, p.place_time) AS at,
(p.lat IS NOT NULL AND p.lng IS NOT NULL) AS located
FROM day_assignments da JOIN places p ON p.id = da.place_id
WHERE da.day_id = ?
ORDER BY da.order_index ASC, da.created_at ASC, da.id ASC
`);
const setIndex = db.prepare('UPDATE day_assignments SET order_index = ? WHERE id = ?');
const viasOf = db.prepare('SELECT id, after_order_index, sequence FROM roadtrip_vias WHERE day_id = ?');
const dropVia = db.prepare('DELETE FROM roadtrip_vias WHERE id = ?');
const moveVia = db.prepare('UPDATE roadtrip_vias SET after_order_index = ? WHERE id = ?');
const setSequence = db.prepare('UPDATE roadtrip_vias SET sequence = ? WHERE id = ?');
let seated = 0;
for (const night of nights) {
const rows = dayRows.all(night.day_id) as DayRow[];
const own = rows.findIndex((row) => row.id === night.id);
if (own < 0) continue;
const others = rows.filter((row) => row.id !== night.id);
let seat = 0;
if (night.check_in) {
others.forEach((row, i) => {
if (row.at !== null && row.at <= night.check_in!) seat = i + 1;
});
}
const next = [...others.slice(0, seat), rows[own]!, ...others.slice(seat)];
if (next.every((row, i) => row.id === rows[i]!.id)) continue;
const previousLocated = rows.filter((row) => row.located).map((row) => row.id);
next.forEach((row, i) => setIndex.run(i, row.id));
seated += 1;
const nextLocated = next.filter((row) => row.located).map((row) => row.id);
const vias = viasOf.all(night.day_id) as Via[];
if (!vias.length) continue;
const previousLast = previousLocated.length - 1;
const nextLast = nextLocated.length - 1;
const lastStayed = previousLast >= 0 && previousLocated[previousLast] === nextLocated[nextLast];
const landed = new Set<number>();
const kept: Array<Via & { was: number }> = [];
for (const via of vias) {
const leg = lastStayed && via.after_order_index === previousLast
? nextLast
: legAfter(via.after_order_index, previousLocated, nextLocated);
if (leg === null) {
dropVia.run(via.id);
continue;
}
if (leg !== via.after_order_index) {
moveVia.run(leg, via.id);
landed.add(via.id);
}
kept.push({ ...via, was: via.after_order_index, after_order_index: leg });
}
// Two legs that merged carry two sequence series side by side. Renumbered the way
// the service does it: the earlier leg's points first, then by their old sequence,
// then by id. Only a leg that received a via is touched.
const byLeg = new Map<number, Array<Via & { was: number }>>();
for (const via of kept) byLeg.set(via.after_order_index, [...(byLeg.get(via.after_order_index) ?? []), via]);
for (const onLeg of byLeg.values()) {
if (!onLeg.some((via) => landed.has(via.id))) continue;
onLeg.sort((a, b) => a.was - b.was || a.sequence - b.sequence || a.id - b.id);
onLeg.forEach((via, i) => {
if (via.sequence !== i) setSequence.run(i, via.id);
});
}
}
return seated;
}
/**
* The leg a via pinned behind the n-th stop of the old order is on in the new one
* (AccommodationsService.legAfter). Null when no leg is left for it.
*/
function legAfter(index: number, previousIds: number[], nextIds: number[]): number | null {
if (index > previousIds.length - 1) return null;
let at = index;
while (at >= 0 && !nextIds.includes(previousIds[at]!)) at -= 1;
if (at < 0) return null;
const next = nextIds.indexOf(previousIds[at]!);
return next >= nextIds.length - 1 ? null : next;
}