mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
feat(tours): point a tour at its trip's packing list
A tour can now point at the packing list of its trip, as a whole or at one of its categories (say "Hiking gear" for a hike), and shows it in its detail. The new Packing list section reads the list live as the user may see it: the backpack tile with the item count on its corner and the category's own colour, how many are packed with a thin progress bar, the first items ticked off or not with their quantity, how many more there are, and a button that opens the trip's Lists tab. Who may change the tour links one, changes it or lets go through a dialog that lists the whole list and every category with its colour and item count, and writes only on its confirm. It is a reference, never a copy: the items stay in Packing, are ticked off and edited there, and each traveller sees in the tour only the items Packing shows them, so an item kept to oneself never shows through a tour. A category that is renamed, cleared or only holds items of others reads as missing, an empty one as empty, a link that could not be read says so with a retry, and while Lists are switched off the link stays, dimmed, and can still be let go. Removing the link, or the tour, leaves the list and every item as they are. Nothing suggests or changes an item by itself. On the server the link is a row of its own in the tours domain (tour_packing_links, migration 046110), one per tour and gone with it. GET, PUT and DELETE /api/trips/:tripId/tours/:placeId/packing-link read it with the trip and change it with place_edit; linking needs the Lists addon and, for a category, an item of it the user can see. The writes answer the tour with its link and tell the trip through tours:changed, so the offline client queues them as writes of the tour and replays them like one. A copied trip keeps its tours' links, a tour saved as new or taken over does not. The MCP tools get_tour_packing, link_tour_packing and unlink_tour_packing run over the same service and checks; the read also hands the tour's facts as context for suggesting what to pack, which only becomes an item through the packing tools. The empty category placeholder now lives in @trek/shared so both sides leave it out alike. All 27 locales carry the new strings, and the wiki describes the link and the tools.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* P2D-XD-01: a tour points at the packing list of its trip, as a whole or at
|
||||
* one of its categories (a sub-list such as "Hiking gear"). The link only
|
||||
* names what it points at; the items stay in Packing, which owns their
|
||||
* content, editing and lifecycle, and decides who sees which of them.
|
||||
*/
|
||||
|
||||
/** A category of the trip's packing list, exactly as its items carry it. */
|
||||
export const tourPackingCategorySchema = z.string().min(1).max(200);
|
||||
|
||||
/** What a tour points at: null for the whole packing list, otherwise one of its categories. */
|
||||
export const tourPackingLinkRequestSchema = z.object({
|
||||
category: tourPackingCategorySchema.nullable(),
|
||||
});
|
||||
export type TourPackingLinkRequest = z.infer<typeof tourPackingLinkRequestSchema>;
|
||||
|
||||
/** The link a tour's detail carries: what it points at and when it was made. */
|
||||
export const tourPackingLinkSchema = z.object({
|
||||
category: z.string().nullable(),
|
||||
linked_at: z.string(),
|
||||
});
|
||||
export type TourPackingLink = z.infer<typeof tourPackingLinkSchema>;
|
||||
|
||||
/** GET .../tours/:placeId/packing-link: what the tour points at, null while it points at nothing. */
|
||||
export const tourPackingLinkResponseSchema = z.object({
|
||||
packing_link: tourPackingLinkSchema.nullable(),
|
||||
});
|
||||
export type TourPackingLinkResponse = z.infer<typeof tourPackingLinkResponseSchema>;
|
||||
@@ -0,0 +1,76 @@
|
||||
import { PACKING_PLACEHOLDER_NAME } from '../packing/packing-placeholder';
|
||||
import { linkedPackingItems, packingCategoriesOf } from './tour-packing-link';
|
||||
import {
|
||||
tourPackingLinkRequestSchema,
|
||||
tourPackingLinkResponseSchema,
|
||||
tourPackingLinkSchema,
|
||||
} from './tour-packing-link.schema';
|
||||
import { tourCreateResponseSchema } from './tours.schema';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
const items = [
|
||||
{ id: 1, name: 'Boots', category: 'Hiking gear', checked: 1 },
|
||||
{ id: 2, name: 'Rain jacket', category: 'Hiking gear', checked: 0 },
|
||||
{ id: 3, name: 'Passport', category: 'Documents', checked: 0 },
|
||||
{ id: 4, name: PACKING_PLACEHOLDER_NAME, category: 'Snacks', checked: 0 },
|
||||
{ id: 5, name: 'Old charger', category: null, checked: true },
|
||||
];
|
||||
|
||||
describe('tour packing link contract (P2D-XD-01)', () => {
|
||||
it('TPL-SCHEMA-001: points at the whole list with null and at a category by its exact name', () => {
|
||||
expect(tourPackingLinkRequestSchema.parse({ category: null })).toEqual({ category: null });
|
||||
expect(tourPackingLinkRequestSchema.parse({ category: ' Hiking gear ' })).toEqual({ category: ' Hiking gear ' });
|
||||
expect(tourPackingLinkRequestSchema.safeParse({ category: '' }).success).toBe(false);
|
||||
expect(tourPackingLinkRequestSchema.safeParse({ category: 'x'.repeat(201) }).success).toBe(false);
|
||||
expect(tourPackingLinkRequestSchema.safeParse({}).success).toBe(false);
|
||||
});
|
||||
|
||||
it('TPL-SCHEMA-002: a link write answers the tour with its link, null without one; other answers carry none', () => {
|
||||
const link = { category: 'Hiking gear', linked_at: '2026-10-20 09:00:00' };
|
||||
expect(tourPackingLinkSchema.parse(link)).toEqual(link);
|
||||
expect(tourPackingLinkSchema.safeParse({ category: null }).success).toBe(false);
|
||||
const field = tourCreateResponseSchema.shape.packing_link;
|
||||
expect(field.parse(link)).toEqual(link);
|
||||
expect(field.parse(null)).toBeNull();
|
||||
expect(field.parse(undefined)).toBeUndefined();
|
||||
expect(tourPackingLinkResponseSchema.parse({ packing_link: null })).toEqual({ packing_link: null });
|
||||
expect(tourPackingLinkResponseSchema.safeParse({}).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('linkedPackingItems (P2D-XD-01)', () => {
|
||||
it('TPL-ITEMS-001: the whole list shows every item but an empty category placeholder', () => {
|
||||
const linked = linkedPackingItems({ category: null }, items);
|
||||
expect(linked.state).toBe('ready');
|
||||
expect(linked.items.map((item) => item.id)).toEqual([1, 2, 3, 5]);
|
||||
expect(linked.packed).toBe(2);
|
||||
});
|
||||
|
||||
it('TPL-ITEMS-002: a category shows its own items in their order and counts the ticked ones', () => {
|
||||
const linked = linkedPackingItems({ category: 'Hiking gear' }, items);
|
||||
expect(linked).toEqual({ state: 'ready', items: [items[0], items[1]], packed: 1 });
|
||||
});
|
||||
|
||||
it('TPL-ITEMS-003: an empty list and a category that holds only its placeholder read as empty', () => {
|
||||
expect(linkedPackingItems({ category: null }, []).state).toBe('empty');
|
||||
expect(linkedPackingItems({ category: 'Snacks' }, items)).toEqual({ state: 'empty', items: [], packed: 0 });
|
||||
});
|
||||
|
||||
it('TPL-ITEMS-004: a category without an item the viewer can see reads as missing, never as empty', () => {
|
||||
expect(linkedPackingItems({ category: 'Climbing' }, items)).toEqual({ state: 'missing', items: [], packed: 0 });
|
||||
// The name is compared as it is stored: another spelling is another category.
|
||||
expect(linkedPackingItems({ category: 'hiking gear' }, items).state).toBe('missing');
|
||||
});
|
||||
});
|
||||
|
||||
describe('packingCategoriesOf (P2D-XD-01)', () => {
|
||||
it('TPL-CATS-001: names each category once, in the order the list first shows it, with its items counted', () => {
|
||||
expect(packingCategoriesOf(items)).toEqual([
|
||||
{ name: 'Hiking gear', count: 2 },
|
||||
{ name: 'Documents', count: 1 },
|
||||
{ name: 'Snacks', count: 0 },
|
||||
]);
|
||||
expect(packingCategoriesOf([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
import { PACKING_PLACEHOLDER_NAME } from '../packing/packing-placeholder';
|
||||
import type { TourPackingLink } from './tour-packing-link.schema';
|
||||
|
||||
/** What a packing item has to say for a tour's link to show it. */
|
||||
export interface LinkablePackingItem {
|
||||
name: string;
|
||||
category?: string | null;
|
||||
checked?: number | boolean | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* How a tour's packing link reads against the items its viewer can see:
|
||||
* `ready` shows at least one item; `empty` points at something that is there
|
||||
* but holds no item yet (an empty packing list, a category just made);
|
||||
* `missing` names a category without one item the viewer can see, because it
|
||||
* was renamed or cleared, or its items are kept to other travellers.
|
||||
*/
|
||||
export type TourPackingLinkState = 'ready' | 'empty' | 'missing';
|
||||
|
||||
export interface LinkedPackingItems<T> {
|
||||
state: TourPackingLinkState;
|
||||
/** The items of the list or category, in the order Packing lists them. */
|
||||
items: T[];
|
||||
/** How many of them are ticked off. */
|
||||
packed: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The items a link shows, read from the packing items the viewer can see, so
|
||||
* what one traveller keeps to themselves never shows through a tour. Nothing
|
||||
* here writes: the list, its categories and its items stay Packing's.
|
||||
*/
|
||||
export function linkedPackingItems<T extends LinkablePackingItem>(
|
||||
link: Pick<TourPackingLink, 'category'>,
|
||||
visible: readonly T[],
|
||||
): LinkedPackingItems<T> {
|
||||
const inScope = link.category === null ? visible : visible.filter((item) => item.category === link.category);
|
||||
const items = inScope.filter((item) => item.name !== PACKING_PLACEHOLDER_NAME);
|
||||
let state: TourPackingLinkState = 'ready';
|
||||
if (items.length === 0) state = link.category === null || inScope.length > 0 ? 'empty' : 'missing';
|
||||
return { state, items, packed: items.filter((item) => Boolean(item.checked)).length };
|
||||
}
|
||||
|
||||
/** A category a tour can point at: its name and how many items it holds. */
|
||||
export interface PackingCategoryChoice {
|
||||
name: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The named categories of a packing list in the order the list first shows
|
||||
* them, each with its items counted (an empty category's placeholder is no
|
||||
* item). Items without a category sit in no category a tour can point at;
|
||||
* the whole list takes them in.
|
||||
*/
|
||||
export function packingCategoriesOf(visible: readonly LinkablePackingItem[]): PackingCategoryChoice[] {
|
||||
const counts = new Map<string, number>();
|
||||
for (const item of visible) {
|
||||
if (!item.category) continue;
|
||||
const counted = item.name === PACKING_PLACEHOLDER_NAME ? 0 : 1;
|
||||
counts.set(item.category, (counts.get(item.category) ?? 0) + counted);
|
||||
}
|
||||
return [...counts].map(([name, count]) => ({ name, count }));
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
import { runPaceSecondsPerKmSchema } from './run-pace';
|
||||
import { skiDisciplineSchema, skiTechniqueSchema } from './ski.schema';
|
||||
import { tourActivityRefSchema, tourActivitySchema } from './tour-activity.schema';
|
||||
import { tourPackingLinkSchema } from './tour-packing-link.schema';
|
||||
import { tourPoiCategorySchema } from './tour-poi-category';
|
||||
import { tourOriginSchema, tourProvenanceSchema } from './tour-provenance.schema';
|
||||
import { maxTourMinutesFor, TOUR_DAY_MINUTES, tourStageCountOf, tourStageIssues } from './tour-stages';
|
||||
@@ -728,6 +729,12 @@ export const tourCreateResponseSchema = z.object({
|
||||
obstacle_check: tourObstacleCheckSchema.optional().catch(undefined),
|
||||
/** ACT-01: the recorded tracks assigned to the tour; absent only in a detail cached before TREK sent them. */
|
||||
activities: z.array(tourActivitySchema).optional(),
|
||||
/**
|
||||
* P2D-XD-01: the packing list or category the tour points at, null for none.
|
||||
* The packing link writes answer with it; an answer without it says nothing
|
||||
* about the link, so a cache keeps the one it had.
|
||||
*/
|
||||
packing_link: tourPackingLinkSchema.nullable().optional(),
|
||||
});
|
||||
export type TourCreateResponse = z.infer<typeof tourCreateResponseSchema>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user