feat(tours): show a multi-day tour's stages in the list and put each on a day

A multi-day tour in the planner's list of the trip's tours now unfolds into
its stages (MDT-02), so the stages can be seen together and fitted into the
trip without leaving the one route they are cut from.

- The fold beside a multi-day tour lists its stages in route order, each
  from stop to stop with its distance, climb, descent and moving time, read
  off the saved tour the way the planner reads them, and below them what the
  whole tour adds up to. A value no stage tells is left out, a total some
  stages do not tell is marked as at least that much and says for how many,
  and a stage whose route is not planned yet says so.
- Each stage has its own day: a pill opens the trip's days, moves the stage
  to another one or takes it off again. A stage without a day shows a dashed
  "No day" pill. A day holds a tour, or one of its stages, once, and a stage
  is on one day at most; the plan's own offline path writes it.
- A stage opens in the planner: the tour opens as any tour does and the
  stage is shown once its points are in.
- The day plan names the stage a day holds with its own figures, and the
  day's route runs to that stage's start and on from its end; the trip map
  draws that stretch only. The tour list still counts a tour once, however
  many days hold it or its stages.
- A saved change that takes a stage away (its stage end gone, the tour a day
  tour again, back to the recorded track) takes it off its day in the same
  write and tells every client; the planner says so before the save.
- REST and the assign_place_to_day tool take the stage by the key of the
  stop it starts at, with the same checks; list_tours names the stops
  between the stages and the stage each day holds.

A day assignment remembers its stage by that key (migration 046010); every
assignment made before holds the whole tour, as it did.
This commit is contained in:
Maurice
2026-10-20 13:21:10 +02:00
parent 6ec06c90f4
commit 8fa75b3c4e
100 changed files with 2787 additions and 127 deletions
@@ -75,6 +75,7 @@ const UNNUMBERED = [
'Migration20200101045900_a_tour_keeps_the_track_it_was_done_with',
'Migration20200101045910_a_tour_can_be_left_out_of_ones_activities',
'Migration20200101046000_a_tour_waypoint_can_end_a_stage',
'Migration20200101046010_a_day_can_hold_one_stage_of_a_tour',
];
// The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it.
@@ -1341,3 +1341,79 @@ describe('DayAssignmentsRepository.listPublicForShare (SH9, share.service.ts)',
expect(await assignments.listPublicForShare([])).toEqual([]);
});
});
describe('DayAssignmentsRepository tour stages (MDT-02)', () => {
/** A tour on a trip with three days, the first holding nothing yet. */
function tourOnTrip() {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const days = [createDay(testDb, trip.id), createDay(testDb, trip.id), createDay(testDb, trip.id)];
const place = createPlace(testDb, trip.id);
createTour(testDb, place.id);
return { trip, days, place };
}
const stageOf = async (id: number) => (await findRow(t, DayAssignments, { id }))?.tour_stage_key;
it('ASSIGNREPO-043: writes the stage a day holds, and none for the whole tour', async () => {
const { days, place } = tourOnTrip();
const base = { place_id: place.id, order_index: 0, notes: null, accommodation_id: null };
const stage = await assignments.insertAssignment({ ...base, day_id: days[0].id, tour_stage_key: 'stage-one' });
const whole = await assignments.insertAssignment({ ...base, day_id: days[1].id });
expect(await stageOf(stage)).toBe('stage-one');
expect(await stageOf(whole)).toBeNull();
});
it('ASSIGNREPO-044: finds the day a stage is on, and nothing for a stage on none or of another tour', async () => {
const { trip, days, place } = tourOnTrip();
const held = createDayAssignment(testDb, days[1].id, place.id, { tour_stage_key: 'stage-two' });
expect(await assignments.findTourStage(place.id, 'stage-two')).toEqual({ id: held.id, day_id: days[1].id });
expect(await assignments.findTourStage(place.id, 'stage-one')).toBeUndefined();
const other = createPlace(testDb, trip.id);
expect(await assignments.findTourStage(other.id, 'stage-two')).toBeUndefined();
});
it('ASSIGNREPO-045: takes off only the stages not kept, leaving the whole tour and other tours alone', async () => {
const { trip, days, place } = tourOnTrip();
const first = createDayAssignment(testDb, days[0].id, place.id, { tour_stage_key: 'stage-one' });
const second = createDayAssignment(testDb, days[1].id, place.id, { tour_stage_key: 'stage-two' });
const whole = createDayAssignment(testDb, days[2].id, place.id);
const other = createPlace(testDb, trip.id);
const otherStage = createDayAssignment(testDb, days[2].id, other.id, { tour_stage_key: 'stage-two' });
const gone = await uow.transactional(() => assignments.deleteTourStagesNotIn(place.id, ['stage-one']));
expect(gone).toEqual([{ id: second.id, day_id: days[1].id }]);
const left = (await findRows(t, DayAssignments, {}, { id: 'asc' })).map((row) => row.id);
expect(left).toEqual([first.id, whole.id, otherStage.id]);
});
it('ASSIGNREPO-046: takes every stage off with no key kept, and writes nothing when no stage is held', async () => {
const { days, place } = tourOnTrip();
const first = createDayAssignment(testDb, days[0].id, place.id, { tour_stage_key: 'stage-one' });
const second = createDayAssignment(testDb, days[1].id, place.id, { tour_stage_key: 'stage-two' });
expect(await assignments.deleteTourStagesNotIn(place.id, [])).toEqual([
{ id: first.id, day_id: days[0].id },
{ id: second.id, day_id: days[1].id },
]);
expect(await assignments.deleteTourStagesNotIn(place.id, [])).toEqual([]);
});
it('ASSIGNREPO-047: a copied assignment keeps the stage it held', async () => {
const { days, place } = tourOnTrip();
const copy = {
place_id: place.id,
order_index: 0,
notes: null,
reservation_status: null,
reservation_notes: null,
reservation_datetime: null,
assignment_time: null,
assignment_end_time: null,
end_day: 0,
};
const stage = await assignments.insertAssignmentCopy({ ...copy, day_id: days[0].id, tour_stage_key: 'stage-one' });
const whole = await assignments.insertAssignmentCopy({ ...copy, day_id: days[1].id });
expect(await stageOf(stage)).toBe('stage-one');
expect(await stageOf(whole)).toBeNull();
});
});
@@ -263,6 +263,44 @@ describe('ToursRepository reads', () => {
});
});
describe('ToursRepository stages on days (MDT-02)', () => {
it('TOURREPO-017: listStagePoints reads the points in route order with key, role and stage end, none for a plain place', async () => {
const { trip } = fixture();
const hutToHut = await makePlace(t, trip.id, { name: 'Hut to hut' });
const plain = await makePlace(t, trip.id, { name: 'Plain' });
createTour(testDb, hutToHut.id);
await insertRows(t, TourWaypoints, [
{ place: hutToHut.id, lat: 47.2, lng: 11, role: 'end', sequence: 3, station_key: 'stop-end' },
{ place: hutToHut.id, lat: 47, lng: 11, role: 'start', sequence: 0, station_key: 'stop-start' },
{ place: hutToHut.id, lat: 47.1, lng: 11, role: 'via', sequence: 2, station_key: 'stop-hut', stage_end: 1 },
{ place: hutToHut.id, lat: 47.05, lng: 11, role: 'via', sequence: 1, shaping: 1 },
]);
expect(await repo.listStagePoints(hutToHut.id)).toEqual([
{ role: 'start', lat: 47, lng: 11, key: 'stop-start' },
{ role: 'via', lat: 47.05, lng: 11, shaping: true },
{ role: 'via', lat: 47.1, lng: 11, key: 'stop-hut', stage_end: true },
{ role: 'end', lat: 47.2, lng: 11, key: 'stop-end' },
]);
expect(await repo.listStagePoints(plain.id)).toEqual([]);
});
it('TOURREPO-018: listTourDays names the stage each day holds, null where a day holds the whole tour', async () => {
const { trip } = fixture();
const tour = createPlace(testDb, trip.id);
createTour(testDb, tour.id);
const first = createDay(testDb, trip.id);
const second = createDay(testDb, trip.id);
createDayAssignment(testDb, first.id, tour.id);
createDayAssignment(testDb, second.id, tour.id, { tour_stage_key: 'stop-hut' });
expect((await repo.listTourDays(trip.id)).map((row) => [row.day_id, row.tour_stage_key])).toEqual([
[first.id, null],
[second.id, 'stop-hut'],
]);
});
});
describe('ToursRepository writes', () => {
it('TOURREPO-006: insertTour writes the metrics and leaves the unset metadata NULL', async () => {
const { trip } = fixture();