feat(tours): keep whole tours in collections and take them into trips

A saved tour can now be kept in a collection, apart from any trip, and
later made into a tour of another trip. The bookmark button in a tour's
footer and in the planner's head (waiting for the save while there are
unsaved changes) opens a dialog with every list of the user; a list they
may only read stays in it, dimmed with a lock and a tooltip. The list
keeps an independent copy: the line, the type, every measure and setting,
the waypoints in order with their roles, the origin, and the trip and tour
it was saved from as they were called then. Its days, its links to places
of the trip and its obstacle decisions stay in the trip, and later changes
to the tour do not reach the copy.

A collection shows its kept tours as cards above its places: the grade
tile with the source badge, measures and the waypoint count as badges,
the trip it came from, and who kept it. The owner or an admin of the list
can remove one after a question; tours made from it stay. "Add to a trip"
shows the kept tour on a map with its facts and numbered waypoints, lists
the user's trips (the ones they may not add tours to dimmed), and asks
outright whether the waypoints come along. "Line only" keeps the line as
saved and adds no waypoints; a tour kept without a line can only bring
them. The new tour has an id of its own, no days and no new places, and
names the trip and tour it was saved from; the dialog then offers to open
that trip. Neither the collection nor the original tour changes.

On the server the kept tours are a table of their own in the tours domain
(collection_tours, migration 046100), each row written whole once. Every
copy, the in-trip "save as new tour", the take-over from another trip and
both directions here, now goes through one copy model (tour-copy.helpers)
instead of a second one. Reading and removing follow the collection's
roles, keeping one also needs access to the source trip, and taking one
over needs place_edit on the target trip; the kept tour is read again
inside the write, so one removed meanwhile leaves no half-made tour. The
MCP tools list_collection_tours, get_collection_tour,
save_tour_to_collection, take_over_collection_tour and
delete_collection_tour run over the same service and checks. All 27
locales carry the new strings, and the wiki describes the flow.
This commit is contained in:
Maurice
2026-10-20 13:15:42 +02:00
parent 3659297af1
commit 3fe34061fd
85 changed files with 6144 additions and 181 deletions
+4 -3
View File
@@ -43,14 +43,15 @@ describe('ALL_ENTITIES', () => {
* + tour_station_visits, how far a city walk got on one of its days
* + tour_change_steps, the changes to a tour saved from a confirmed proposal
* + tour_activities, the recorded tracks assigned to a tour
* + tour_activity_exclusions, the tours a user left out of their own activity sums)
* + tour_activity_exclusions, the tours a user left out of their own activity sums
* + collection_tours, the whole tours a collection keeps)
* is registered, and no two share a class name (a silent duplicate would
* shadow one of them in `orm.getMetadata()`, exactly the kind of drift
* `entity-schema-parity.test.ts` can't see — it iterates the metadata map,
* which de-duplicates by class name).
*/
it('ENT-003: exactly 146 entities, every class name unique', () => {
expect(ALL_ENTITIES.length).toBe(146);
it('ENT-003: exactly 147 entities, every class name unique', () => {
expect(ALL_ENTITIES.length).toBe(147);
const names = ALL_ENTITIES.map((s) => s.meta.className);
expect(new Set(names).size).toBe(names.length);
});
@@ -40,7 +40,9 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
// only ever written whole, by the save of a confirmed change).
// + tour_activities: id, place_id, track_place_id (an assignment always names its tour and its recording).
// + tour_activity_exclusions: id, user_id, place_id (an exclusion always names its user and its tour).
const KNOWN_UNINITIALISED_NOT_NULL_SCALARS = 510;
// + collection_tours: id, collection_id, name, waypoints, tour_type, origin, source_trip_title, source_tour_name
// (a kept tour is only ever written whole, from the tour it was saved from).
const KNOWN_UNINITIALISED_NOT_NULL_SCALARS = 518;
describe('entity class fields never carry a NaN initialiser (task-4-review-shape.md, Important 1)', () => {
it('CLASSFIELD-001: new X() has no own property whose value is NaN, across all 133 entities', () => {
@@ -76,6 +76,7 @@ const UNNUMBERED = [
'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',
'Migration20200101046100_a_collection_keeps_whole_tours',
];
// The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it.
@@ -0,0 +1,173 @@
/**
* `CollectionToursRepository` (LIB-03): the whole tours a collection keeps.
* A kept tour is written whole (CT3), listed newest first (CT1), read only
* within its collection (CT2) and removed only from it (CT4); the trips one
* could go into come with their owner (CT5).
*/
import { CollectionTours } from '../../../../src/db/entities/CollectionTours.entity';
import { Collections } from '../../../../src/db/entities/Collections.entity';
import { Users } from '../../../../src/db/entities/Users.entity';
import type {
CollectionTourInsert,
CollectionToursRepository,
} from '../../../../src/db/repositories/CollectionTours.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { makeCollection } from '../../../helpers/factories/collections';
import { deleteRows, findRow, updateRows } from '../../../helpers/factories/rows';
import { makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: CollectionToursRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(CollectionTours);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
const POINTS = [
{ lat: 47, lng: 11, role: 'start', sequence: 0 },
{ lat: 47.01, lng: 11.01, role: 'via', sequence: 1, name: 'Hut', key: 'stop-hut-1' },
{ lat: 47.02, lng: 11.02, role: 'end', sequence: 2 },
];
/** A kept hike with a line and three points, the middle one named, its points encoded as the service writes them. */
function snapshot(collectionId: number, savedBy: number | null, overrides: Partial<CollectionTourInsert> = {}) {
const row: CollectionTourInsert = {
collection_id: collectionId,
saved_by: savedBy,
name: 'Ridge',
description: 'Up high',
website: null,
route_geometry: '[[47,11,600],[47.01,11.01,650],[47.02,11.02,630]]',
waypoints: JSON.stringify(POINTS),
tour_type: 'hike',
distance: 4.2,
elevation_gain: 300,
elevation_loss: null,
duration: 90,
planned_duration_minutes: null,
break_additional_minutes: 20,
difficulty: null,
wanderer_ref: null,
match_confidence: 1,
max_hiking_difficulty: 3,
bike_profile: null,
boat_type: null,
water_context: null,
paddle_speed_kmh: null,
portage_speed_kmh: null,
paddle_lake_style: null,
track_marks: null,
hiking_preferences: null,
origin: 'trek',
origin_file_name: null,
source_trip_title: 'Alps',
source_tour_name: 'Ridge',
...overrides,
};
return row;
}
async function fixture() {
const { user } = await makeUser(t, { username: 'keeper' });
const mine = await makeCollection(t, user.id, { name: 'Hikes' });
const other = await makeCollection(t, user.id, { name: 'Other' });
return { user, mine, other };
}
describe('CollectionToursRepository', () => {
it('CT3/CT2: keeps a snapshot whole and reads it back with its points decoded and who saved it', async () => {
const { user, mine, other } = await fixture();
const id = await repo.insertSnapshot(snapshot(mine.id, user.id));
const row = await repo.findInCollection(mine.id, id);
expect(row).toMatchObject({
id,
collection_id: mine.id,
saved_by: user.id,
saved_by_name: 'keeper',
name: 'Ridge',
tour_type: 'hike',
distance: 4.2,
elevation_loss: null,
source_trip_title: 'Alps',
});
expect(row?.waypoints).toEqual(POINTS);
expect(typeof row?.created_at).toBe('string');
// Only within its own collection.
expect(await repo.findInCollection(other.id, id)).toBeUndefined();
});
it('CT1: lists a collection’s tours newest first, the id breaking ties, and nothing of another', async () => {
const { user, mine, other } = await fixture();
const first = await repo.insertSnapshot(snapshot(mine.id, user.id, { name: 'First' }));
const second = await repo.insertSnapshot(snapshot(mine.id, user.id, { name: 'Second' }));
const older = await repo.insertSnapshot(snapshot(mine.id, user.id, { name: 'Older' }));
await repo.insertSnapshot(snapshot(other.id, user.id, { name: 'Elsewhere' }));
await updateRows(t, CollectionTours, { id: older }, { created_at: '2020-01-01 00:00:00' });
await updateRows(t, CollectionTours, { id: { $in: [first, second] } }, { created_at: '2026-10-20 10:00:00' });
const rows = await repo.listForCollection(mine.id);
expect(rows.map((row) => row.name)).toEqual(['Second', 'First', 'Older']);
});
it('CT1/CT2: an account that is gone leaves the kept tour with nobody named', async () => {
const { mine } = await fixture();
const { user: gone } = await makeUser(t, { username: 'gone' });
const id = await repo.insertSnapshot(snapshot(mine.id, gone.id));
await deleteRows(t, Users, { id: gone.id });
expect(await repo.findInCollection(mine.id, id)).toMatchObject({ saved_by: null, saved_by_name: null });
});
it('CT2: a broken point list, or one of another shape, reads as no points, the line kept as it is', async () => {
const { user, mine } = await fixture();
const id = await repo.insertSnapshot(snapshot(mine.id, user.id));
await updateRows(t, CollectionTours, { id }, { waypoints: 'not json' });
const row = await repo.findInCollection(mine.id, id);
expect(row?.waypoints).toEqual([]);
expect(row?.route_geometry).toContain('47.01');
await updateRows(t, CollectionTours, { id }, { waypoints: JSON.stringify([{ lat: 147, lng: 11 }]) });
expect((await repo.listForCollection(mine.id))[0]?.waypoints).toEqual([]);
});
it('CT4: removes a kept tour only from its own collection', async () => {
const { user, mine, other } = await fixture();
const id = await repo.insertSnapshot(snapshot(mine.id, user.id));
expect(await repo.deleteInCollection(other.id, id)).toBe(false);
expect(await findRow(t, CollectionTours, { id })).not.toBeNull();
expect(await repo.deleteInCollection(mine.id, id)).toBe(true);
expect(await repo.findInCollection(mine.id, id)).toBeUndefined();
});
it('CT5: names the trips asked for with their owner and dates, and none for no ids', async () => {
const { user } = await fixture();
const alps = await makeTrip(t, user.id, { title: 'Alps', start_date: '2026-07-01', end_date: '2026-07-08' });
const coast = await makeTrip(t, user.id, { title: 'Coast', start_date: null, end_date: null });
await makeTrip(t, user.id, { title: 'Not asked' });
const rows = await repo.targetTrips([coast.id, alps.id]);
expect([...rows].sort((a, b) => a.id - b.id)).toEqual([
{ id: alps.id, user_id: user.id, title: 'Alps', start_date: '2026-07-01', end_date: '2026-07-08' },
{ id: coast.id, user_id: user.id, title: 'Coast', start_date: null, end_date: null },
]);
expect(await repo.targetTrips([])).toEqual([]);
});
it('goes with its collection', async () => {
const { user, mine } = await fixture();
const id = await repo.insertSnapshot(snapshot(mine.id, user.id));
await deleteRows(t, Collections, { id: mine.id });
expect(await findRow(t, CollectionTours, { id })).toBeNull();
});
});