mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +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>;
|
||||
Reference in New Issue
Block a user