feat(places): own place identities, duplicate merging and lists linked to a trip

- A user can group the uses of one place (trip places and saved places) into a
  place identity of their own (user_places, user_place_links). It is personal:
  it groups their "All places" and is never written into a shared trip or list
  row. An identity without a use they can still reach is not shown, and one
  left without links is pruned on their next write and nightly.
- Possible duplicates are suggested server side (same provider id, or the same
  name at the same spot), each pair with what differs and whether the user may
  merge it. A merge joins two identities in one transaction and can be undone;
  a dismissed pair does not come back. No trip or list row is deleted or changed.
- A trip can link lists (trip_collection_links, needs place_edit and access to
  the list). A member sees a linked list only with their own access to it; the
  map layer lists its places and whether each is already in the trip.
  tripCollections:changed tells the members to read their view again.
- REST, MCP tools and client data layer for all of it; migrations on both
  engines, additive only.
- CI: the stage 1 to 2 places-library helper types, and the await-in-loop and
  non-null counts it had raised.
This commit is contained in:
Maurice
2026-11-29 15:57:05 +01:00
parent 01d327b502
commit f69a4c1a24
87 changed files with 4882 additions and 221 deletions
+57
View File
@@ -0,0 +1,57 @@
// FE-API-PLACE-IDENTITIES-001 to FE-API-PLACE-IDENTITIES-002
//
// D5/PLC-13: each method pins its verb, its path and its body or query, and
// hands back what the server answered.
import { http, HttpResponse } from 'msw';
import { beforeEach, describe, expect, it } from 'vitest';
import { server } from '../../tests/helpers/msw/server';
import { placeIdentitiesApi } from './placeIdentities';
const BASE = '/api/addons/collections/library';
let calls: Array<[string, string, Record<string, string>, unknown]> = [];
beforeEach(() => {
calls = [];
});
function record(data: object) {
return async ({ request }: { request: Request }) => {
const url = new URL(request.url);
const text = request.method === 'GET' ? '' : await request.text();
calls.push([request.method, url.pathname, Object.fromEntries(url.searchParams), text ? JSON.parse(text) : null]);
return HttpResponse.json(data);
};
}
describe('placeIdentitiesApi', () => {
it('FE-API-PLACE-IDENTITIES-001: links and unlinks uses of an own place', async () => {
const answer = { entry: null };
server.use(http.post(`${BASE}/places/link`, record(answer)), http.post(`${BASE}/places/unlink`, record(answer)));
expect(await placeIdentitiesApi.link({ uses: [{ kind: 'trip', id: 3 }], identity_id: 2 })).toEqual(answer);
expect(await placeIdentitiesApi.unlink({ kind: 'list', id: 4 })).toEqual(answer);
expect(calls).toEqual([
['POST', `${BASE}/places/link`, {}, { uses: [{ kind: 'trip', id: 3 }], identity_id: 2 }],
['POST', `${BASE}/places/unlink`, {}, { kind: 'list', id: 4 }],
]);
});
it('FE-API-PLACE-IDENTITIES-002: reads duplicates, dismisses, merges and undoes', async () => {
server.use(
http.get(`${BASE}/duplicates`, record({ duplicates: [], next_cursor: null })),
http.post(`${BASE}/duplicates/dismiss`, record({ success: true })),
http.post(`${BASE}/merges`, record({ merge_id: 5, entry: null })),
http.post(`${BASE}/merges/5/undo`, record({ success: true }))
);
expect(await placeIdentitiesApi.duplicates({ limit: 10 })).toEqual({ duplicates: [], next_cursor: null });
expect(await placeIdentitiesApi.dismiss('trip:1', 'list:2')).toEqual({ success: true });
expect(await placeIdentitiesApi.merge('trip:1', 'list:2')).toEqual({ merge_id: 5, entry: null });
expect(await placeIdentitiesApi.undoMerge(5)).toEqual({ success: true });
expect(calls).toEqual([
['GET', `${BASE}/duplicates`, { limit: '10' }, null],
['POST', `${BASE}/duplicates/dismiss`, {}, { a: 'trip:1', b: 'list:2' }],
['POST', `${BASE}/merges`, {}, { leading: 'trip:1', other: 'list:2' }],
['POST', `${BASE}/merges/5/undo`, {}, null],
]);
});
});
+31
View File
@@ -0,0 +1,31 @@
import type {
PlacesLibraryDuplicatesQueryInput,
PlacesLibraryDuplicatesResponse,
PlacesLibraryEntryResponse,
PlacesLibraryLinkRequest,
PlacesLibraryMergeResponse,
PlacesLibraryUse,
SuccessResponse,
} from '@trek/shared';
import apiClient from './client';
const base = '/addons/collections/library';
/** D5/PLC-13: the user's own places in "All places", the duplicate suggestions and merges. */
export const placeIdentitiesApi = {
link: (body: PlacesLibraryLinkRequest): Promise<PlacesLibraryEntryResponse> =>
apiClient.post(`${base}/places/link`, body).then((r) => r.data),
unlink: (use: PlacesLibraryUse): Promise<PlacesLibraryEntryResponse> =>
apiClient.post(`${base}/places/unlink`, use).then((r) => r.data),
duplicates: (
query: PlacesLibraryDuplicatesQueryInput = {},
signal?: AbortSignal
): Promise<PlacesLibraryDuplicatesResponse> =>
apiClient.get(`${base}/duplicates`, { params: query, signal }).then((r) => r.data),
dismiss: (a: string, b: string): Promise<SuccessResponse> =>
apiClient.post(`${base}/duplicates/dismiss`, { a, b }).then((r) => r.data),
merge: (leading: string, other: string): Promise<PlacesLibraryMergeResponse> =>
apiClient.post(`${base}/merges`, { leading, other }).then((r) => r.data),
undoMerge: (mergeId: number): Promise<SuccessResponse> =>
apiClient.post(`${base}/merges/${mergeId}/undo`).then((r) => r.data),
};
+43
View File
@@ -0,0 +1,43 @@
// FE-API-TRIP-COLLECTIONS-001
//
// PLC-15/PLC-16: each method pins its verb, its path and its body.
import { http, HttpResponse } from 'msw';
import { beforeEach, describe, expect, it } from 'vitest';
import { server } from '../../tests/helpers/msw/server';
import { tripCollectionsApi } from './tripCollections';
const links = { links: [], can_link: true };
let calls: Array<[string, string, unknown]> = [];
beforeEach(() => {
calls = [];
});
function record(data: object) {
return async ({ request }: { request: Request }) => {
const text = request.method === 'POST' ? await request.text() : '';
calls.push([request.method, new URL(request.url).pathname, text ? JSON.parse(text) : null]);
return HttpResponse.json(data);
};
}
describe('tripCollectionsApi', () => {
it('FE-API-TRIP-COLLECTIONS-001: lists, maps, links and unlinks the lists of a trip', async () => {
server.use(
http.get('/api/trips/4/collections', record(links)),
http.get('/api/trips/4/collections/places', record({ places: [] })),
http.post('/api/trips/4/collections', record(links)),
http.delete('/api/trips/4/collections/9', record(links))
);
expect(await tripCollectionsApi.list(4)).toEqual(links);
expect(await tripCollectionsApi.layer(4)).toEqual({ places: [] });
expect(await tripCollectionsApi.link(4, 9)).toEqual(links);
expect(await tripCollectionsApi.unlink(4, 9)).toEqual(links);
expect(calls).toEqual([
['GET', '/api/trips/4/collections', null],
['GET', '/api/trips/4/collections/places', null],
['POST', '/api/trips/4/collections', { collection_id: 9 }],
['DELETE', '/api/trips/4/collections/9', null],
]);
});
});
+14
View File
@@ -0,0 +1,14 @@
import type { TripCollectionLayerResponse, TripCollectionLinksResponse } from '@trek/shared';
import apiClient from './client';
/** PLC-15/PLC-16: the lists a trip draws on, and their saved places for the trip map. */
export const tripCollectionsApi = {
list: (tripId: number, signal?: AbortSignal): Promise<TripCollectionLinksResponse> =>
apiClient.get(`/trips/${tripId}/collections`, { signal }).then((r) => r.data),
layer: (tripId: number, signal?: AbortSignal): Promise<TripCollectionLayerResponse> =>
apiClient.get(`/trips/${tripId}/collections/places`, { signal }).then((r) => r.data),
link: (tripId: number, collectionId: number): Promise<TripCollectionLinksResponse> =>
apiClient.post(`/trips/${tripId}/collections`, { collection_id: collectionId }).then((r) => r.data),
unlink: (tripId: number, collectionId: number): Promise<TripCollectionLinksResponse> =>
apiClient.delete(`/trips/${tripId}/collections/${collectionId}`).then((r) => r.data),
};
+3
View File
@@ -50,6 +50,9 @@ export const HANDLED_OUTSIDE_TRIP_STORE = [
// hooks/useTripAddonsSync) puts the trip's list into the store and the device's copy, so the
// tabs and panels a trip switched off disappear for every member at once.
'tripAddons:changed',
// Linked lists (PLC-15/PLC-16): store/tripCollectionsStore (watchTripCollections) reads the
// trip's linked lists and map layer again; content-free because each member's answer differs.
'tripCollections:changed',
// components/Roadtrip/useDayBoundaries reloads the shared boundaries.
'roadtripBoundary:changed',
// In-app notifications: hooks/useInAppNotificationListener, which also keeps