mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +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:
@@ -1,3 +1,6 @@
|
||||
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import { REQUIRE_ADDON } from '../../../src/nest/addons/require-addon.decorator';
|
||||
import { TripAddonGuard } from '../../../src/nest/addons/trip-addon.guard';
|
||||
import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
import { JwtAuthGuard } from '../../../src/nest/auth-core/jwt-auth.guard';
|
||||
import {
|
||||
@@ -54,8 +57,12 @@ const storageStub = {
|
||||
* Build the controller over its stubs. The env is the live one, so a case about
|
||||
* demo mode stubs DEMO_MODE and every other case runs outside it.
|
||||
*/
|
||||
function ctl(service: CollabService, storage: StorageService = storageStub): CollabController {
|
||||
return new CollabController(service, storage, new RuntimeEnvService());
|
||||
/** Whether Collab applies to the trip: on, unless a test switches it off for its trip. */
|
||||
const addonsOn = { isEnabledForTrip: vi.fn(async () => true) } as unknown as AddonsService;
|
||||
const addonsOff = { isEnabledForTrip: vi.fn(async () => false) } as unknown as AddonsService;
|
||||
|
||||
function ctl(service: CollabService, storage: StorageService = storageStub, addons = addonsOn): CollabController {
|
||||
return new CollabController(service, storage, new RuntimeEnvService(), addons);
|
||||
}
|
||||
|
||||
async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
|
||||
@@ -346,6 +353,18 @@ describe('CollabController (parity with the legacy /api/trips/:tripId/collab rou
|
||||
).toEqual({ status: 403, body: { error: 'No permission' } });
|
||||
});
|
||||
|
||||
it('refuses a trip that has Collab off for itself, after the trip check, with the addon text', async () => {
|
||||
const s = svc({ verifyTripAccess: vi.fn().mockResolvedValue({ id: 5, user_id: 1 }) } as Partial<CollabService>);
|
||||
expect(
|
||||
await thrownAsync(() => ctl(s, storageStub, addonsOff).createMessage(user, '5', { text: 'hi' }, undefined)),
|
||||
).toEqual({
|
||||
status: 404,
|
||||
body: { error: 'Collab addon is not enabled' },
|
||||
});
|
||||
expect(addonsOff.isEnabledForTrip).toHaveBeenCalledWith(5, 'collab');
|
||||
expect(s.postMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('react 404 unknown, else broadcasts reactions (empty emoji now 400s in the Zod pipe)', async () => {
|
||||
expect(
|
||||
await thrownAsync(() =>
|
||||
@@ -667,8 +686,24 @@ describe('CollabController (parity with the legacy /api/trips/:tripId/collab rou
|
||||
for (const handler of [CollabController.prototype.createMessage, CollabController.prototype.addNoteFile]) {
|
||||
expect(Reflect.getMetadata(TRIP_PERMISSION_KEY, handler)).toBeUndefined();
|
||||
expect(guardsOn(handler)).not.toContain(TripAccessGuard);
|
||||
expect(guardsOn(handler)).not.toContain(TripAddonGuard);
|
||||
}
|
||||
});
|
||||
|
||||
it('every other route asks the trip and then the Collab addon of that trip', async () => {
|
||||
expect(Reflect.getMetadata(REQUIRE_ADDON, CollabController)).toEqual({ addonId: 'collab', label: 'Collab' });
|
||||
const multipart = new Set<unknown>([
|
||||
CollabController.prototype.createMessage,
|
||||
CollabController.prototype.addNoteFile,
|
||||
]);
|
||||
const proto = CollabController.prototype as unknown as Record<string, unknown>;
|
||||
const routes = Object.getOwnPropertyNames(CollabController.prototype)
|
||||
.map((name) => proto[name])
|
||||
.filter((fn) => typeof fn === 'function' && Reflect.getMetadata('method', fn as object) !== undefined)
|
||||
.filter((fn) => !multipart.has(fn));
|
||||
expect(routes.length).toBeGreaterThan(15);
|
||||
for (const handler of routes) expect(guardsOn(handler as object)).toEqual([TripAccessGuard, TripAddonGuard]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('link preview', () => {
|
||||
|
||||
Reference in New Issue
Block a user