mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(server): let a trip owner switch trip addons for one trip
A trip can now turn a trip addon (lists, costs, documents, collab, road trip, tours) on or off for itself, whatever the instance switch says. No row in the new trip_addon_overrides table means the trip follows the instance; a row is the owner's choice. Going back to the instance setting deletes the row, and no addon data is touched either way. - GET/PUT /api/trips/:tripId/addons[/:addonId] (every member reads, the owner writes) and the MCP tools get_trip_addons and set_trip_addon, on the shared tripAddons contract; tripAddons:changed tells the members. - Every trip-scoped addon surface asks the trip: REST through TripAddonGuard, MCP through a per-call gate on the named or pinned trip, plugin RPC and events, the share link, the offline bundle, reminders, doc sync, the assistant (tools, keep_attached_file), booking costs, the trip summary prompt, and the cross-trip tour reads (library, Atlas stats, collection save). - Routes without a trip open while the addon is on in any of the user's trips. Switching an addon on that the instance has off ends the members' MCP sessions so they reconnect with its tools. - A copied trip takes its owner's choices along. Behaviour changes: an anonymous call to an addon route that is off now gets 401 instead of 404, and with an instance switch off the packing, todo and files plugin RPC methods, the files MCP tools, todo reminder mails, core plugin events, plugin view contributions, the share link's lists, costs and chat sections and the offline bundle's lists follow it.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { tripAddonIdSchema, tripAddonUpdateRequestSchema, tripAddonsResponseSchema } from './trip-addons.schema';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('trip addon contracts', () => {
|
||||
it('TRIPADDON-SCHEMA-001: the update body takes on, off or null and nothing else', () => {
|
||||
expect(tripAddonUpdateRequestSchema.parse({ enabled: true })).toEqual({ enabled: true });
|
||||
expect(tripAddonUpdateRequestSchema.parse({ enabled: false })).toEqual({ enabled: false });
|
||||
expect(tripAddonUpdateRequestSchema.parse({ enabled: null })).toEqual({ enabled: null });
|
||||
expect(tripAddonUpdateRequestSchema.safeParse({}).success).toBe(false);
|
||||
expect(tripAddonUpdateRequestSchema.safeParse({ enabled: 1 }).success).toBe(false);
|
||||
expect(tripAddonUpdateRequestSchema.safeParse({ enabled: 'true' }).success).toBe(false);
|
||||
expect(tripAddonUpdateRequestSchema.safeParse({ enabled: true, extra: 1 }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('TRIPADDON-SCHEMA-002: an addon id is a lower-case word', () => {
|
||||
expect(tripAddonIdSchema.safeParse('packing').success).toBe(true);
|
||||
expect(tripAddonIdSchema.safeParse('naver_list_import').success).toBe(true);
|
||||
expect(tripAddonIdSchema.safeParse('').success).toBe(false);
|
||||
expect(tripAddonIdSchema.safeParse('Packing').success).toBe(false);
|
||||
expect(tripAddonIdSchema.safeParse('../x').success).toBe(false);
|
||||
expect(tripAddonIdSchema.safeParse('a'.repeat(65)).success).toBe(false);
|
||||
});
|
||||
|
||||
it('TRIPADDON-SCHEMA-003: the response lists each addon with its effective and instance state', () => {
|
||||
const body = {
|
||||
tripId: 4,
|
||||
addons: [
|
||||
{ id: 'budget', name: 'Costs', icon: 'Wallet', enabled: false, instanceEnabled: true, overridden: true },
|
||||
],
|
||||
};
|
||||
expect(tripAddonsResponseSchema.parse(body)).toEqual(body);
|
||||
expect(tripAddonsResponseSchema.safeParse({ tripId: 4, addons: [{ id: 'budget' }] }).success).toBe(false);
|
||||
expect(tripAddonsResponseSchema.safeParse({ tripId: 0, addons: [] }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Per-trip addons: the trip owner switches a trip addon (lists, costs, files,
|
||||
* collab, road trip, tours) on or off for one trip, whatever the instance says.
|
||||
*
|
||||
* Routes: `GET /api/trips/:tripId/addons` (every member) and
|
||||
* `PUT /api/trips/:tripId/addons/:addonId` (the owner only). The MCP tools
|
||||
* `get_trip_addons` and `set_trip_addon` answer with the same list.
|
||||
*/
|
||||
|
||||
/** An addon id as the addons table spells it (`packing`, `budget`, ...). */
|
||||
export const tripAddonIdSchema = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(64)
|
||||
.regex(/^[a-z][a-z0-9_]*$/, 'Expected an addon id such as packing or budget');
|
||||
|
||||
/**
|
||||
* One trip addon as this trip sees it.
|
||||
*
|
||||
* `enabled` is what applies here, `instanceEnabled` the admin's switch for the
|
||||
* whole instance, and `overridden` says the trip owner chose `enabled`
|
||||
* themselves instead of following the instance.
|
||||
*/
|
||||
export const tripAddonSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
icon: z.string().nullable(),
|
||||
enabled: z.boolean(),
|
||||
instanceEnabled: z.boolean(),
|
||||
overridden: z.boolean(),
|
||||
});
|
||||
export type TripAddon = z.infer<typeof tripAddonSchema>;
|
||||
|
||||
export const tripAddonListSchema = z.array(tripAddonSchema);
|
||||
|
||||
/** The body of `PUT /api/trips/:tripId/addons/:addonId`: on, off, or null for the instance setting. */
|
||||
export const tripAddonUpdateRequestSchema = z.object({ enabled: z.boolean().nullable() }).strict();
|
||||
export type TripAddonUpdateRequest = z.infer<typeof tripAddonUpdateRequestSchema>;
|
||||
|
||||
/** What both routes answer: every trip addon of the trip, in the admin's order. */
|
||||
export const tripAddonsResponseSchema = z.object({
|
||||
tripId: z.number().int().positive(),
|
||||
addons: tripAddonListSchema,
|
||||
});
|
||||
export type TripAddonsResponse = z.infer<typeof tripAddonsResponseSchema>;
|
||||
@@ -299,3 +299,5 @@ export * from './demo/demo-account';
|
||||
export * from './ferratas/ferratas.schema';
|
||||
// The 0.05 degree tile grid both ends load, cache and persist the layer on.
|
||||
export * from './ferratas/ferrataGrid';
|
||||
// Per-trip addons: GET/PUT /api/trips/:tripId/addons and the tripAddons:changed event.
|
||||
export * from './addons/trip-addons.schema';
|
||||
|
||||
@@ -16,6 +16,9 @@ import { describe, it, expect } from 'vitest';
|
||||
*/
|
||||
const FIXTURES: Record<TrekWsEventName, Record<string, unknown>> = {
|
||||
'roadtripPreferences:changed': { preferences: { roadtrip_day_start: '08:00' } },
|
||||
'tripAddons:changed': {
|
||||
addons: [{ id: 'budget', name: 'Costs', icon: 'Wallet', enabled: false, instanceEnabled: true, overridden: true }],
|
||||
},
|
||||
'place:created': { place: { id: 3, name: 'Louvre' } },
|
||||
'place:updated': { place: { id: 3, name: 'Louvre' } },
|
||||
'place:deleted': { placeId: 3 },
|
||||
@@ -200,7 +203,7 @@ const DRIFT_VARIANTS: Partial<Record<TrekWsEventName, Record<string, unknown>[]>
|
||||
};
|
||||
|
||||
describe('@trek/shared realtime event registry', () => {
|
||||
it('WSEVT-REG-001: pins the authoritative inventory counts (79 trip + 43 user = 122)', () => {
|
||||
it('WSEVT-REG-001: pins the authoritative inventory counts (82 trip + 43 user = 125)', () => {
|
||||
// 67th to 69th trip event: the three collab:link:* a shared link emits.
|
||||
// 70th and 71st: the road trip's vias and tracks, which used to be written silently.
|
||||
// 74th: docsync:changed, so a sync run that moved documents refreshes the
|
||||
@@ -222,9 +225,11 @@ describe('@trek/shared realtime event registry', () => {
|
||||
// inbox of forwarded booking mails and its settings card.
|
||||
// 81st trip event: tripTags:changed, so a tag a member adds to the trip,
|
||||
// renames or deletes shows the same for everyone on it.
|
||||
expect(TREK_WS_TRIP_EVENT_NAMES).toHaveLength(81);
|
||||
// 82nd trip event: tripAddons:changed, so a trip addon its owner switches on
|
||||
// or off for the trip shows or hides for every member at once.
|
||||
expect(TREK_WS_TRIP_EVENT_NAMES).toHaveLength(82);
|
||||
expect(TREK_WS_USER_EVENT_NAMES).toHaveLength(43);
|
||||
expect(TREK_WS_EVENT_NAMES).toHaveLength(124);
|
||||
expect(TREK_WS_EVENT_NAMES).toHaveLength(125);
|
||||
});
|
||||
|
||||
it('WSEVT-REG-002: every name is domain:action shaped and outside the reserved plugin: namespace', () => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { tripAddonListSchema } from '../addons/trip-addons.schema';
|
||||
import { flightLiveLegSchema } from '../flight-live/flight-live.schema';
|
||||
import { roadtripDayBoundaryListSchema } from '../roadtrip/day-boundary.schema';
|
||||
import { roadtripPreferencesSchema } from '../roadtrip/preferences.schema';
|
||||
@@ -293,6 +294,10 @@ export const TREK_WS_EVENTS = {
|
||||
|
||||
'roadtripPreferences:changed': { scope: 'trip', payload: z.object({ preferences: roadtripPreferencesSchema }) },
|
||||
|
||||
// The trip owner switched a trip addon on or off for this trip, or back to the
|
||||
// instance setting: every trip addon as the trip now sees it.
|
||||
'tripAddons:changed': { scope: 'trip', payload: z.object({ addons: tripAddonListSchema }) },
|
||||
|
||||
// ── Notifications (user-scoped) ──────────────────────────────────────────
|
||||
'notification:new': { scope: 'user', payload: z.object({ notification: entity }) },
|
||||
'notification:updated': { scope: 'user', payload: z.object({ notification: entity }) },
|
||||
|
||||
Reference in New Issue
Block a user