feat(places): read every place of the trips and lists in one view

A read-only overview of every place a user can reach: the saved places of
their lists and the places of their trips, viewer trips included, with tours,
tracks and the stops of a drive left out. A saved place and a trip place are
one entry only when the saved place came from that trip place, the trip is
still open to the user, and both agree (a provider id, or the same name at the
same spot); anything else stays apart. Every entry names where it comes from
and whether it may be changed there.

- GET /api/addons/collections/library/places: paged by cursor, search,
  category, kind, list, trip and status filters, counts per kind.
- GET /api/addons/collections/library/places/{trip|list}/🆔 one entry with
  what each origin keeps on its own (notes after the existing rights, labels,
  ratings, a trip's planned days, whether a stay or booking uses it).
- MCP tools list_all_places and get_library_place.

Nothing is written and nothing migrates; the rights are the existing list and
trip checks, read on every call.
This commit is contained in:
Maurice
2026-11-29 14:57:10 +01:00
parent 82165855c7
commit 0ecca2b46c
27 changed files with 2616 additions and 2 deletions
+1
View File
@@ -121,6 +121,7 @@ export * from './geo/haversine';
export * from './geo/longitude';
export * from './collection/collection.schema';
export * from './collection/collection-file.schema';
export * from './places-library/places-library.schema';
export * from './trip/trip.schema';
export * from './place/place-hours';
export * from './place/place-kind';
@@ -0,0 +1,132 @@
import {
PLACES_LIBRARY_PAGE_MAX,
placesLibraryDetailRequestSchema,
placesLibraryDetailResponseSchema,
placesLibraryPageResponseSchema,
placesLibraryQuerySchema,
} from './places-library.schema';
import { describe, expect, it } from 'vitest';
const tripOrigin = {
kind: 'trip',
trip_id: 5,
trip_title: 'Alps',
trip_start: '2026-07-01',
trip_end: null,
place_id: 12,
can_edit: true,
read_only: false,
} as const;
const listOrigin = {
kind: 'list',
collection_id: 3,
collection_name: 'Food',
collection_place_id: 40,
status: 'want',
can_edit: false,
} as const;
const entry = {
key: 'trip:12',
name: 'Café Central',
address: null,
lat: 48.2,
lng: 16.36,
category: { id: 1, name: 'Food', color: '#f00', icon: null },
image_url: null,
updated_at: '2026-11-01 10:00:00',
origins: [tripOrigin, listOrigin],
};
describe('placesLibraryQuerySchema', () => {
it('reads the ids and the limit from query strings', () => {
expect(placesLibraryQuerySchema.parse({ category_id: '4', trip_id: '7', limit: '20', q: ' wien ' })).toEqual({
category_id: 4,
trip_id: 7,
limit: 20,
q: 'wien',
});
});
it('refuses id spellings the server does not read as ids, and limits past the page size', () => {
for (const bad of ['0x5', '5e0', '5.0', '-1', '0']) {
expect(placesLibraryQuerySchema.safeParse({ collection_id: bad }).success).toBe(false);
}
expect(placesLibraryQuerySchema.safeParse({ limit: String(PLACES_LIBRARY_PAGE_MAX + 1) }).success).toBe(false);
expect(placesLibraryQuerySchema.safeParse({ limit: '0' }).success).toBe(false);
});
it('takes only the kinds, sorts and statuses it knows, and no other key', () => {
expect(placesLibraryQuerySchema.parse({ kind: 'list', sort: 'updated', status: 'visited' })).toEqual({
kind: 'list',
sort: 'updated',
status: 'visited',
});
expect(placesLibraryQuerySchema.safeParse({ kind: 'tour' }).success).toBe(false);
expect(placesLibraryQuerySchema.safeParse({ sort: 'distance' }).success).toBe(false);
expect(placesLibraryQuerySchema.safeParse({ status: 'maybe' }).success).toBe(false);
expect(placesLibraryQuerySchema.safeParse({ notes: 'x' }).success).toBe(false);
});
});
describe('placesLibraryDetailRequestSchema', () => {
it('names an entry by the kind and id of one of its origins', () => {
expect(placesLibraryDetailRequestSchema.parse({ kind: 'trip', id: 12 })).toEqual({ kind: 'trip', id: 12 });
expect(placesLibraryDetailRequestSchema.safeParse({ kind: 'trip', id: 1.5 }).success).toBe(false);
expect(placesLibraryDetailRequestSchema.safeParse({ kind: 'tour', id: 1 }).success).toBe(false);
});
});
describe('placesLibraryPageResponseSchema', () => {
it('describes a page with an entry from a trip and a list', () => {
const page = { entries: [entry], total: 1, counts: { all: 1, trip: 1, list: 1 }, next_cursor: null };
expect(placesLibraryPageResponseSchema.parse(page)).toEqual(page);
});
it('refuses an entry without an origin or with an origin of an unknown kind', () => {
const base = { total: 1, counts: { all: 1, trip: 0, list: 1 }, next_cursor: null };
expect(placesLibraryPageResponseSchema.safeParse({ ...base, entries: [{ ...entry, origins: [] }] }).success).toBe(
false,
);
expect(
placesLibraryPageResponseSchema.safeParse({
...base,
entries: [{ ...entry, origins: [{ ...listOrigin, kind: 'tour' }] }],
}).success,
).toBe(false);
});
});
describe('placesLibraryDetailResponseSchema', () => {
it('keeps what each origin holds apart', () => {
const info = {
name: 'Café Central',
address: null,
lat: 48.2,
lng: 16.36,
description: null,
website: null,
phone: null,
image_url: null,
category: null,
};
const rating = { average: 4.5, count: 2, mine: 5 };
const detail = {
entry,
details: [
{
kind: 'trip',
place_id: 12,
info,
notes: 'Book ahead',
rating,
days: [{ day_id: 1, day_number: 1, date: '2026-07-01', title: null, time: '09:00', end_time: null }],
has_accommodation: false,
has_reservation: true,
},
{ kind: 'list', collection_place_id: 40, info, notes: null, labels: [], rating },
],
};
expect(placesLibraryDetailResponseSchema.parse(detail)).toEqual(detail);
});
});
@@ -0,0 +1,221 @@
import { COLLECTION_STATUSES } from '../collection/collection.schema';
import { idParamSchema, idSchema } from '../common/primitives.schema';
import { z } from 'zod';
/**
* PLC-07/PLC-08: "All places", every saved place of the lists a user can open
* and every place of the trips they can open, read only, in one paged list,
* and one entry of it in detail. A saved place and a trip place are one entry
* only where the saved place names that trip place as its source, the user
* still reaches it, and the two agree (the same provider id, or the same name
* at the same spot); everything else stays an entry of its own.
*/
export const PLACES_LIBRARY_PAGE_DEFAULT = 50;
export const PLACES_LIBRARY_PAGE_MAX = 200;
/** Where an entry comes from: a trip, or a list of saved places. */
export const PLACES_LIBRARY_KINDS = ['trip', 'list'] as const;
export type PlacesLibraryKind = (typeof PLACES_LIBRARY_KINDS)[number];
/** By name (A to Z) or by the last change (newest first). */
export const PLACES_LIBRARY_SORTS = ['name', 'updated'] as const;
export type PlacesLibrarySort = (typeof PLACES_LIBRARY_SORTS)[number];
/** A count from a query string: its digits, or a number from a client that sends one. */
const pageLimitSchema = z
.union([z.number().int(), z.string().regex(/^\d+$/)])
.transform(Number)
.pipe(z.number().int().min(1).max(PLACES_LIBRARY_PAGE_MAX));
export const placesLibraryQuerySchema = z
.object({
q: z.string().trim().max(100).optional().describe('A part of the name or the address, case ignored'),
category_id: idParamSchema.optional().describe('Only places of this category'),
kind: z
.enum(PLACES_LIBRARY_KINDS)
.optional()
.describe('Only entries that come from a trip (trip) or from a list of saved places (list)'),
collection_id: idParamSchema.optional().describe('Only entries in this list'),
trip_id: idParamSchema.optional().describe('Only entries in this trip'),
status: z.enum(COLLECTION_STATUSES).optional().describe('Only entries with this status in one of the lists'),
sort: z
.enum(PLACES_LIBRARY_SORTS)
.optional()
.describe('name (A to Z, the default) or updated (last changed first)'),
cursor: z
.string()
// A cursor carries the sort value of the last entry, a place name among them.
.max(4000)
.optional()
.describe('Where to go on: the next_cursor of the page before. Leave it out for the first page.'),
limit: pageLimitSchema
.optional()
.describe(
`How many entries at most (default ${PLACES_LIBRARY_PAGE_DEFAULT}, at most ${PLACES_LIBRARY_PAGE_MAX})`,
),
})
.strict();
export type PlacesLibraryQuery = z.infer<typeof placesLibraryQuerySchema>;
export type PlacesLibraryQueryInput = z.input<typeof placesLibraryQuerySchema>;
/** One entry by the origin it was opened from: a trip place id or a saved place id. */
export const placesLibraryDetailRequestSchema = z
.object({
kind: z.enum(PLACES_LIBRARY_KINDS).describe('trip for a trip place id, list for a saved place id'),
id: idSchema.describe('The place_id of a trip origin or the collection_place_id of a list origin'),
})
.strict();
export type PlacesLibraryDetailRequest = z.infer<typeof placesLibraryDetailRequestSchema>;
export const placesLibraryCategorySchema = z.object({
id: z.number().int(),
name: z.string(),
color: z.string().nullable(),
icon: z.string().nullable(),
});
export type PlacesLibraryCategory = z.infer<typeof placesLibraryCategorySchema>;
/** The place as a list of saved places has it, and whether the user may change it there. */
export const placesLibraryListOriginSchema = z.object({
kind: z.literal('list'),
collection_id: z.number().int(),
collection_name: z.string(),
collection_place_id: z.number().int(),
status: z.enum(COLLECTION_STATUSES),
can_edit: z.boolean(),
});
export type PlacesLibraryListOrigin = z.infer<typeof placesLibraryListOriginSchema>;
/** The place as a trip has it; read_only where the trip lets the user see but not change its places. */
export const placesLibraryTripOriginSchema = z.object({
kind: z.literal('trip'),
trip_id: z.number().int(),
trip_title: z.string(),
trip_start: z.string().nullable(),
trip_end: z.string().nullable(),
place_id: z.number().int(),
can_edit: z.boolean(),
read_only: z.boolean(),
});
export type PlacesLibraryTripOrigin = z.infer<typeof placesLibraryTripOriginSchema>;
export const placesLibraryOriginSchema = z.discriminatedUnion('kind', [
placesLibraryTripOriginSchema,
placesLibraryListOriginSchema,
]);
export type PlacesLibraryOrigin = z.infer<typeof placesLibraryOriginSchema>;
/**
* One place of "All places". `key` stays the same across pages and sorts:
* `trip:<place_id>` when the entry has a trip origin, else
* `list:<collection_place_id>`. Name, address, position, category and picture
* are the trip place's where there is one; the trip origin comes first.
*/
export const placesLibraryEntrySchema = z.object({
key: z.string(),
name: z.string(),
address: z.string().nullable(),
lat: z.number().nullable(),
lng: z.number().nullable(),
category: placesLibraryCategorySchema.nullable(),
image_url: z.string().nullable(),
updated_at: z.string().nullable(),
origins: z.array(placesLibraryOriginSchema).min(1),
});
export type PlacesLibraryEntry = z.infer<typeof placesLibraryEntrySchema>;
/** How many entries the search and category reach, as a whole and by where they come from. */
export const placesLibraryCountsSchema = z.object({
all: z.number().int().nonnegative(),
trip: z.number().int().nonnegative(),
list: z.number().int().nonnegative(),
});
export type PlacesLibraryCounts = z.infer<typeof placesLibraryCountsSchema>;
export const placesLibraryPageResponseSchema = z.object({
entries: z.array(placesLibraryEntrySchema),
/** Entries every filter of the query matches, on every page. */
total: z.number().int().nonnegative(),
/** Entries `q` and `category_id` match, whatever the other filters say, for the kind tabs. */
counts: placesLibraryCountsSchema,
next_cursor: z.string().nullable(),
});
export type PlacesLibraryPageResponse = z.infer<typeof placesLibraryPageResponseSchema>;
/** The general facts of a place as one origin stores them; two origins of one entry may differ. */
export const placesLibraryInfoSchema = z.object({
name: z.string(),
address: z.string().nullable(),
lat: z.number().nullable(),
lng: z.number().nullable(),
description: z.string().nullable(),
website: z.string().nullable(),
phone: z.string().nullable(),
image_url: z.string().nullable(),
category: placesLibraryCategorySchema.nullable(),
});
export type PlacesLibraryInfo = z.infer<typeof placesLibraryInfoSchema>;
/** The votes on a place where it is: their mean, how many, and the user's own. */
export const placesLibraryRatingSchema = z.object({
average: z.number().nullable(),
count: z.number().int().nonnegative(),
mine: z.number().int().nullable(),
});
export type PlacesLibraryRating = z.infer<typeof placesLibraryRatingSchema>;
export const placesLibraryLabelSchema = z.object({
id: z.number().int(),
name: z.string(),
color: z.string().nullable(),
});
/** A day of the trip the place is planned on, with its time there when one is set. */
export const placesLibraryDaySchema = z.object({
day_id: z.number().int(),
day_number: z.number().int(),
date: z.string().nullable(),
title: z.string().nullable(),
time: z.string().nullable(),
end_time: z.string().nullable(),
});
export type PlacesLibraryDay = z.infer<typeof placesLibraryDaySchema>;
/** What a list keeps of the place: its facts, the list's notes and labels, and the members' votes. */
export const placesLibraryListDetailSchema = z.object({
kind: z.literal('list'),
collection_place_id: z.number().int(),
info: placesLibraryInfoSchema,
notes: z.string().nullable(),
labels: z.array(placesLibraryLabelSchema),
rating: placesLibraryRatingSchema,
});
export type PlacesLibraryListDetail = z.infer<typeof placesLibraryListDetailSchema>;
/** What a trip keeps of the place: its facts, the trip's notes and votes, the days, and whether a stay or booking uses it. */
export const placesLibraryTripDetailSchema = z.object({
kind: z.literal('trip'),
place_id: z.number().int(),
info: placesLibraryInfoSchema,
notes: z.string().nullable(),
rating: placesLibraryRatingSchema,
days: z.array(placesLibraryDaySchema),
has_accommodation: z.boolean(),
has_reservation: z.boolean(),
});
export type PlacesLibraryTripDetail = z.infer<typeof placesLibraryTripDetailSchema>;
export const placesLibraryDetailSchema = z.discriminatedUnion('kind', [
placesLibraryTripDetailSchema,
placesLibraryListDetailSchema,
]);
export type PlacesLibraryDetail = z.infer<typeof placesLibraryDetailSchema>;
/** One entry with what each of its origins holds, in the order of `entry.origins`. */
export const placesLibraryDetailResponseSchema = z.object({
entry: placesLibraryEntrySchema,
details: z.array(placesLibraryDetailSchema),
});
export type PlacesLibraryDetailResponse = z.infer<typeof placesLibraryDetailResponseSchema>;