feat(trips): shortening a trip says first which days go and what is on them

The trip dialog on the desktop and the trip sheet on a phone now ask before a save whose new dates remove days with something on them. The question names those days the way the day list does and lists what goes with them in the same rows the delete day question uses: planned places stay in the place list, notes and day texts are deleted, bookings stay under Bookings, and a stay that checks in or out on a removed day goes as a whole while its booking and expense remain, shown in the danger tone. A moved start gets both questions in one step, with the booking hint following the chosen shift mode, and a save that only drops empty days asks nothing.

The rule that lays the days out moved into shared as planDayGrid, with resolveDayGridRange deciding whether the rows are rebuilt at all. generateDays on the server now carries that plan out in one transaction, so the warning names exactly the days the save takes, and a seeded fuzz test holds the new rebuild to the old one over 300 random day grids. The update_trip tool says in its description what shortening takes and lists removed_days in its answer.

Alongside, the dialog sends a day count only when it changed, so a stale count no longer trims a day just added, stays are reloaded after every date change including the one made on a phone, and the date shift tip now points to adding a day under Reorder days.
This commit is contained in:
Maurice
2026-09-23 19:54:09 +02:00
parent b56f335978
commit 1b63285ad0
50 changed files with 2122 additions and 231 deletions
+171 -1
View File
@@ -1,4 +1,12 @@
import { addIsoDays, nextTripDate, planDatedAppend } from './day-grid';
import {
type DayGridDay,
type DayGridStay,
addIsoDays,
nextTripDate,
planDatedAppend,
planDayGrid,
resolveDayGridRange,
} from './day-grid';
import { MAX_TRIP_DAYS } from './trip.schema';
import { describe, expect, it } from 'vitest';
@@ -68,3 +76,165 @@ describe('planDatedAppend', () => {
expect(planDatedAppend({}, ['2026-10-10'])).toBeNull();
});
});
describe('resolveDayGridRange', () => {
const dated = { start_date: '2026-10-01', end_date: '2026-10-10' };
it('keeps a stored date the update leaves out, and clears one it sends as null', () => {
expect(resolveDayGridRange(dated, { end_date: '2026-10-08' })).toEqual({
newStart: '2026-10-01',
newEnd: '2026-10-08',
dayCount: undefined,
regenerate: true,
});
expect(resolveDayGridRange(dated, { start_date: null, end_date: null })).toMatchObject({
newStart: null,
newEnd: null,
regenerate: true,
});
});
it('rebuilds nothing when the dates stay and no day count comes', () => {
expect(resolveDayGridRange(dated, {}).regenerate).toBe(false);
expect(resolveDayGridRange(dated, { ...dated }).regenerate).toBe(false);
const undated = { start_date: null, end_date: null };
expect(resolveDayGridRange(undated, { ...undated }).regenerate).toBe(false);
});
it('rebuilds on any day count, held to 1..MAX_TRIP_DAYS, and reads 0 or null as none', () => {
const undated = { start_date: null, end_date: null };
expect(resolveDayGridRange(undated, { day_count: 5 })).toMatchObject({ dayCount: 5, regenerate: true });
expect(resolveDayGridRange(undated, { day_count: MAX_TRIP_DAYS + 5 }).dayCount).toBe(MAX_TRIP_DAYS);
expect(resolveDayGridRange(undated, { day_count: -3 }).dayCount).toBe(1);
expect(resolveDayGridRange(undated, { day_count: 0 })).toMatchObject({ dayCount: undefined, regenerate: false });
expect(resolveDayGridRange(undated, { day_count: null })).toMatchObject({ dayCount: undefined, regenerate: false });
});
});
describe('planDayGrid', () => {
// The trip of the plan: Oct 1 to 10 with a day each (ids 1 to 10), then two
// days without a date: 11 is empty, 12 has a place on it.
const tenDays: DayGridDay[] = [
...Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
day_number: i + 1,
date: addIsoDays('2026-10-01', i),
hasPlanItems: true,
})),
{ id: 11, day_number: 11, date: null, hasPlanItems: false },
{ id: 12, day_number: 12, date: null, hasPlanItems: true },
];
const plan = (startDate: string | null, endDate: string | null, stays: DayGridStay[] = []) =>
planDayGrid({ days: tenDays, stays, startDate, endDate });
const ids = (p: ReturnType<typeof plan>) => p.rows.map((r) => r.id);
const gone = (p: ReturnType<typeof plan>) => p.removed.map((r) => `${r.id}:${r.reason}`);
it('an earlier end takes the last days with everything on them, and the empty spare day', () => {
const p = plan('2026-10-01', '2026-10-08');
expect(ids(p)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 12]);
expect(p.rows.at(-2)).toEqual({ id: 8, date: '2026-10-08' });
expect(p.rows.at(-1)).toEqual({ id: 12, date: null });
expect(gone(p)).toEqual(['9:overflow', '10:overflow', '11:spare']);
expect(p.removed[0]).toEqual({ id: 9, day_number: 9, date: '2026-10-09', reason: 'overflow' });
});
it('a later start and the same end still takes the last days, the plans move with the dates', () => {
const p = plan('2026-10-03', '2026-10-10');
expect(p.rows.slice(0, 8)).toEqual(
Array.from({ length: 8 }, (_, i) => ({ id: i + 1, date: addIsoDays('2026-10-03', i) })),
);
expect(gone(p)).toEqual(['9:overflow', '10:overflow', '11:spare']);
});
it('moving the whole range keeps every dated day and drops only the empty spare day', () => {
const p = plan('2026-10-05', '2026-10-14');
expect(ids(p)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]);
expect(p.rows[9]).toEqual({ id: 10, date: '2026-10-14' });
expect(gone(p)).toEqual(['11:spare']);
});
it('a later end dates the first spare day and removes nothing', () => {
const p = plan('2026-10-01', '2026-10-11');
expect(p.rows.slice(9)).toEqual([
{ id: 10, date: '2026-10-10' },
{ id: 11, date: '2026-10-11' },
{ id: 12, date: null },
]);
expect(p.removed).toEqual([]);
});
it('a range longer than all days adds new empty days after the spare ones', () => {
const p = plan('2026-10-01', '2026-10-14');
expect(ids(p)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, null, null]);
expect(p.rows.at(-1)).toEqual({ id: null, date: '2026-10-14' });
expect(p.removed).toEqual([]);
});
it('clearing the dates without a count keeps every day and takes all dates away', () => {
const p = plan(null, null);
expect(ids(p)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(p.rows.every((r) => r.date === null)).toBe(true);
expect(p.removed).toEqual([]);
});
it('a stay from a removed day to a spare day goes with the removed day, so the spare day is empty', () => {
const p = plan('2026-10-01', '2026-10-09', [{ start_day_id: 10, end_day_id: 11 }]);
expect(gone(p)).toEqual(['10:overflow', '11:spare']);
});
it('a stay that stands keeps the spare day it checks out on', () => {
const p = plan('2026-10-01', '2026-10-09', [{ start_day_id: 9, end_day_id: 11 }]);
expect(gone(p)).toEqual(['10:overflow']);
expect(ids(p).slice(-2)).toEqual([11, 12]);
});
it('reads the order from the day numbers, whatever order the rows come in', () => {
const shuffled = [tenDays[4]!, tenDays[11]!, tenDays[0]!, tenDays[9]!, tenDays[10]!];
const p = planDayGrid({ days: shuffled, stays: [], startDate: '2026-11-01', endDate: '2026-11-02' });
expect(p.rows).toEqual([
{ id: 1, date: '2026-11-01' },
{ id: 5, date: '2026-11-02' },
{ id: 12, date: null },
]);
expect(gone(p)).toEqual(['10:overflow', '11:spare']);
});
describe('without dates', () => {
const undated = (n: number, planned: number[] = [], stays: DayGridStay[] = [], dayCount?: number) =>
planDayGrid({
days: Array.from({ length: n }, (_, i) => ({
id: i + 1,
day_number: i + 1,
date: i < 2 ? '2026-10-01' : null,
hasPlanItems: planned.includes(i + 1),
})),
stays,
startDate: null,
endDate: null,
dayCount,
});
it('trims to the count from the back, and only empty days', () => {
const p = undated(6, [5, 6], [{ start_day_id: 3, end_day_id: 3 }], 2);
expect(p.removed.map((r) => r.id)).toEqual([4, 2, 1]);
expect(p.removed.every((r) => r.reason === 'spare')).toBe(true);
expect(p.rows).toEqual([
{ id: 3, date: null },
{ id: 5, date: null },
{ id: 6, date: null },
]);
expect(p.removed.at(-1)).toMatchObject({ id: 1, date: '2026-10-01' });
});
it('adds empty days up to the count, and seven for a trip that has none', () => {
expect(undated(2, [], [], 4).rows).toEqual([
{ id: 1, date: null },
{ id: 2, date: null },
{ id: null, date: null },
{ id: null, date: null },
]);
expect(undated(0).rows).toHaveLength(7);
expect(undated(3, [], [], MAX_TRIP_DAYS + 1).rows).toHaveLength(MAX_TRIP_DAYS);
});
});
});
+153
View File
@@ -65,3 +65,156 @@ export function planDatedAppend(
const date = dayAfterLatest(trip.end_date, dayDates);
return { date, fits: tripSpanDays(trip.start_date, date) <= MAX_TRIP_DAYS };
}
/** The dates and day count an update leaves a trip with, and whether its day rows are rebuilt. */
export interface DayGridRange {
newStart: string | null | undefined;
newEnd: string | null | undefined;
/** A requested day count, held to 1..MAX_TRIP_DAYS; undefined when none came. */
dayCount: number | undefined;
/** True when a date moves or a day count arrives: the day rows are laid out anew. */
regenerate: boolean;
}
/**
* Where an update takes a trip's range. A field left out keeps the stored
* value, an explicit null clears it. The server rebuilds the day rows exactly
* when `regenerate` is true, and the trip dialog asks the same question before
* it saves, so a warning about lost days never fires for a save that loses none.
* Refusing a bad range stays with the server.
*/
export function resolveDayGridRange(
prev: { start_date?: string | null; end_date?: string | null },
data: { start_date?: string | null; end_date?: string | null; day_count?: number | null },
): DayGridRange {
const newStart = data.start_date !== undefined ? data.start_date : prev.start_date;
const newEnd = data.end_date !== undefined ? data.end_date : prev.end_date;
const dayCount = data.day_count ? Math.min(Math.max(Number(data.day_count) || 7, 1), MAX_TRIP_DAYS) : undefined;
const regenerate = newStart !== prev.start_date || newEnd !== prev.end_date || dayCount !== undefined;
return { newStart, newEnd, dayCount, regenerate };
}
/** A day row as the layout reads it. */
export interface DayGridDay {
id: number;
day_number: number;
date: string | null | undefined;
/** Places planned on the day, or notes on it. A day title or text does not count. */
hasPlanItems: boolean;
}
/** A stay by the two days it checks in and out on. */
export interface DayGridStay {
start_day_id: number;
end_day_id: number;
}
/**
* Why a day goes. `overflow`: a dated day past the new range, removed with
* everything on it. `spare`: a day with nothing planned that the new layout has
* no place for.
*/
export type DayGridRemovalReason = 'overflow' | 'spare';
export interface DayGridRemoval {
id: number;
/** Where the day stood before, and the date it had. */
day_number: number;
date: string | null;
reason: DayGridRemovalReason;
}
/** One row of the new layout: an existing day by its id, or a new one (null). */
export interface DayGridRow {
id: number | null;
date: string | null;
}
/** The day rows a trip ends up with, in order (index + 1 is the day number), and the days that go. */
export interface DayGridPlan {
rows: DayGridRow[];
removed: DayGridRemoval[];
}
const byDayNumber = (a: DayGridDay, b: DayGridDay): number => a.day_number - b.day_number;
const removal = (day: DayGridDay, reason: DayGridRemovalReason): DayGridRemoval => ({
id: day.id,
day_number: day.day_number,
date: day.date || null,
reason,
});
/** Nothing planned on the day and no stay checking in or out on it. */
const isEmptyDay = (day: DayGridDay, stays: readonly DayGridStay[]): boolean =>
!day.hasPlanItems && !stays.some((s) => s.start_day_id === day.id || s.end_day_id === day.id);
/**
* The day rows a trip gets for a range, the rule the server writes by
* (TripsService.generateDays carries this plan out) and the trip dialog warns
* by before it saves.
*
* With dates, plans follow their position: the n-th dated day takes the n-th
* date of the new range, so shortening a trip always takes the last dated days,
* with everything on them, even when it was the start that moved. Days without
* a date fill any dates left over, then new empty days do. Days without a date
* that are still over are dropped when empty and kept, undated, at the end when
* not. A stay on a dropped dated day goes with that day, so it no longer keeps a
* spare day it touched.
*
* Without dates every day loses its date and the trip is held to `dayCount`
* days (its current count when none is given): short of it, empty days are
* added at the end; over it, empty days go, the highest numbers first. A day
* with something planned on it is never dropped, even if that leaves the trip
* over the count.
*/
export function planDayGrid(input: {
days: readonly DayGridDay[];
stays: readonly DayGridStay[];
startDate: string | null | undefined;
endDate: string | null | undefined;
dayCount?: number;
}): DayGridPlan {
const { days, stays, startDate, endDate, dayCount } = input;
if (!startDate || !endDate) {
const ordered = [...days].sort(byDayNumber);
const target = Math.min(Math.max(dayCount ?? (ordered.length || 7), 1), MAX_TRIP_DAYS);
const surplus = ordered.length - target;
const removed =
surplus > 0
? ordered
.filter((day) => isEmptyDay(day, stays))
.reverse()
.slice(0, surplus)
.map((day) => removal(day, 'spare'))
: [];
const gone = new Set(removed.map((r) => r.id));
const rows: DayGridRow[] = ordered.filter((day) => !gone.has(day.id)).map((day) => ({ id: day.id, date: null }));
for (let i = 0; i < -surplus; i++) rows.push({ id: null, date: null });
return { rows, removed };
}
const dated = days.filter((day) => day.date).sort(byDayNumber);
const undated = days.filter((day) => !day.date).sort(byDayNumber);
const span = tripSpanDays(startDate, endDate);
const rows: DayGridRow[] = [];
let used = 0;
for (let i = 0; i < span; i++) {
const date = addIsoDays(startDate, i);
if (i < dated.length) rows.push({ id: dated[i]!.id, date });
else if (used < undated.length) rows.push({ id: undated[used++]!.id, date });
else rows.push({ id: null, date });
}
const overflow = dated.slice(Math.max(span, 0));
const gone = new Set(overflow.map((day) => day.id));
const standing = stays.filter((s) => !gone.has(s.start_day_id) && !gone.has(s.end_day_id));
const removed = overflow.map((day) => removal(day, 'overflow'));
for (const day of undated.slice(used)) {
if (isEmptyDay(day, standing)) removed.push(removal(day, 'spare'));
else rows.push({ id: day.id, date: null });
}
return { rows, removed };
}