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
@@ -0,0 +1,84 @@
// FE-STORE-TRIPCOLLECTIONS-001 to FE-STORE-TRIPCOLLECTIONS-003: the lists a trip draws on,
// read per account, linked and unlinked, and read again on a push.
import { waitFor } from '@testing-library/react';
import type { TripCollectionLayerPlace, TripCollectionLink } from '@trek/shared';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { buildUser } from '../../tests/helpers/factories';
import { resetAllStores, seedStore } from '../../tests/helpers/store';
import { addListener } from '../api/websocket';
import { tripCollectionsRepo } from '../repo/tripCollectionsRepo';
import { useAuthStore } from './authStore';
import { tripCollectionsEntry, useTripCollectionsStore, watchTripCollections } from './tripCollectionsStore';
const link: TripCollectionLink = {
collection_id: 9,
name: 'Food',
color: null,
role: 'owner',
place_count: 2,
linked_by: 1,
created_at: null,
};
const place: TripCollectionLayerPlace = {
id: 3,
collection_id: 9,
name: 'Café',
lat: 1,
lng: 2,
category: null,
status: 'want',
in_trip: false,
trip_place_id: null,
};
const entry = (tripId: number) => tripCollectionsEntry(useTripCollectionsStore.getState(), tripId);
beforeEach(() => {
resetAllStores();
vi.restoreAllMocks();
vi.mocked(addListener).mockClear();
useTripCollectionsStore.setState({ trips: {} });
seedStore(useAuthStore, { user: buildUser({ id: 1 }), isAuthenticated: true });
});
describe('tripCollectionsStore', () => {
it('FE-STORE-TRIPCOLLECTIONS-001: reads the links and the layer for the signed-in account', async () => {
vi.spyOn(tripCollectionsRepo, 'list').mockResolvedValue({ links: [link], can_link: true });
vi.spyOn(tripCollectionsRepo, 'layer').mockResolvedValue({ places: [place] });
await useTripCollectionsStore.getState().load(4);
expect(entry(4)).toEqual({ links: [link], canLink: true, layer: null });
expect(await useTripCollectionsStore.getState().loadLayer(4)).toEqual([place]);
expect(entry(4)?.layer).toEqual([place]);
seedStore(useAuthStore, { user: buildUser({ id: 2 }) });
expect(entry(4)).toBeUndefined();
expect(tripCollectionsEntry(useTripCollectionsStore.getState(), null)).toBeUndefined();
});
it('FE-STORE-TRIPCOLLECTIONS-002: links and unlinks, reading the map layer again once it was asked for', async () => {
vi.spyOn(tripCollectionsRepo, 'link').mockResolvedValue({ links: [link], can_link: true });
vi.spyOn(tripCollectionsRepo, 'unlink').mockResolvedValue({ links: [], can_link: true });
const layer = vi.spyOn(tripCollectionsRepo, 'layer').mockResolvedValue({ places: [place] });
await useTripCollectionsStore.getState().link(4, 9);
expect(entry(4)?.links).toEqual([link]);
expect(layer).not.toHaveBeenCalled();
await useTripCollectionsStore.getState().loadLayer(4);
layer.mockResolvedValue({ places: [] });
await useTripCollectionsStore.getState().unlink(4, 9);
expect(entry(4)).toEqual({ links: [], canLink: true, layer: [] });
});
it('FE-STORE-TRIPCOLLECTIONS-003: a push reads a held trip again and leaves others alone', async () => {
const list = vi.spyOn(tripCollectionsRepo, 'list').mockResolvedValue({ links: [], can_link: false });
vi.spyOn(tripCollectionsRepo, 'layer').mockResolvedValue({ places: [place] });
useTripCollectionsStore.setState({ trips: { '1:4': { links: [link], canLink: true, layer: [] } } });
const stop = watchTripCollections();
const listener = vi.mocked(addListener).mock.calls[0][0] as (event: Record<string, unknown>) => void;
listener({ type: 'tripCollections:changed', tripId: 5 });
listener({ type: 'tripCollections:changed', tripId: 'x' });
listener({ type: 'tripAddons:changed', tripId: 4 });
expect(list).not.toHaveBeenCalled();
listener({ type: 'tripCollections:changed', tripId: 4 });
await waitFor(() => expect(entry(4)).toEqual({ links: [], canLink: false, layer: [place] }));
expect(list).toHaveBeenCalledWith(4);
stop();
});
});
+110
View File
@@ -0,0 +1,110 @@
import type { TripCollectionLayerPlace, TripCollectionLink } from '@trek/shared';
import { create } from 'zustand';
import { addListener, removeListener } from '../api/websocket';
import { tripCollectionsRepo } from '../repo/tripCollectionsRepo';
import { useAuthStore } from './authStore';
/**
* The lists each trip draws on (PLC-15) and their saved places for the trip
* map (PLC-16), as the server answered for the signed-in account. Keyed by
* account and trip, because who sees which linked list is per person. A
* `tripCollections:changed` push reads again whatever the store holds for that
* trip, so a list linked or unlinked by another member shows up or goes away.
*/
interface TripCollectionsEntry {
links: TripCollectionLink[];
canLink: boolean;
/** The saved places of the linked lists for the map, or null while the map layer was never asked for. */
layer: TripCollectionLayerPlace[] | null;
}
interface TripCollectionsState {
trips: Record<string, TripCollectionsEntry>;
/** Reads the trip's linked lists. Online only; throws when the server refuses or there is no connection. */
load: (tripId: number) => Promise<TripCollectionsEntry>;
/** Reads the saved places of the linked lists for the map. Online only. */
loadLayer: (tripId: number) => Promise<TripCollectionLayerPlace[]>;
link: (tripId: number, collectionId: number) => Promise<void>;
unlink: (tripId: number, collectionId: number) => Promise<void>;
}
function keyOf(tripId: number): string {
return `${useAuthStore.getState().user?.id ?? 'anon'}:${tripId}`;
}
const EMPTY: TripCollectionsEntry = { links: [], canLink: false, layer: null };
export const useTripCollectionsStore = create<TripCollectionsState>((set, get) => {
const put = (tripId: number, change: Partial<TripCollectionsEntry>): TripCollectionsEntry => {
const key = keyOf(tripId);
const entry = { ...(get().trips[key] ?? EMPTY), ...change };
set((state) => ({ trips: { ...state.trips, [key]: entry } }));
return entry;
};
/** After a link or unlink the map layer is read again, if the map asked for it. */
const refreshLayer = async (tripId: number) => {
if (get().trips[keyOf(tripId)]?.layer) await get().loadLayer(tripId);
};
return {
trips: {},
load: async (tripId) => {
const { links, can_link } = await tripCollectionsRepo.list(tripId);
return put(tripId, { links, canLink: can_link });
},
loadLayer: async (tripId) => {
const { places } = await tripCollectionsRepo.layer(tripId);
put(tripId, { layer: places });
return places;
},
link: async (tripId, collectionId) => {
const { links, can_link } = await tripCollectionsRepo.link(tripId, collectionId);
put(tripId, { links, canLink: can_link });
await refreshLayer(tripId);
},
unlink: async (tripId, collectionId) => {
const { links, can_link } = await tripCollectionsRepo.unlink(tripId, collectionId);
put(tripId, { links, canLink: can_link });
await refreshLayer(tripId);
},
};
});
/** The trip's entry for the signed-in account, if the store holds one. */
export function tripCollectionsEntry(
state: TripCollectionsState,
tripId: number | null | undefined
): TripCollectionsEntry | undefined {
return tripId == null ? undefined : state.trips[keyOf(tripId)];
}
/**
* Applies `tripCollections:changed`: reads again the links (and the map layer,
* when it was read) of a trip the store holds. The push carries nothing, since
* the answer differs per member. Returns the function that stops listening.
*/
export function watchTripCollections(): () => void {
const onEvent = (event: Record<string, unknown>) => {
if (event?.type !== 'tripCollections:changed') return;
const tripId = Number(event.tripId);
if (!Number.isSafeInteger(tripId) || tripId < 1) return;
const entry = tripCollectionsEntry(useTripCollectionsStore.getState(), tripId);
if (!entry) return;
const store = useTripCollectionsStore.getState();
void store
.load(tripId)
.then(() => (entry.layer ? store.loadLayer(tripId) : undefined))
.catch(() => {
// Offline or refused: the store keeps the last answer until the next read.
return undefined;
});
};
addListener(onEvent);
return () => removeListener(onEvent);
}