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:
Maurice
2026-11-21 13:23:02 +01:00
parent 2f7b8357ce
commit 3f38621024
203 changed files with 3710 additions and 498 deletions
+1
View File
@@ -160,6 +160,7 @@ async function deleteTripAndWatchTempFiles(db: Database.Database): Promise<{
new UnitOfWork(t.em, new SqliteEngine()),
t.em,
none,
none,
);
await trips.remove(trip.id, user.id, 'user');
return {
+4 -3
View File
@@ -59,14 +59,15 @@ describe('ALL_ENTITIES', () => {
* + trip_member_removals, who another member took off a trip, so its invite link refuses them
* + assistant_attachments, the files attached in the trip chat, kept with their conversation
* + mail_ingest_addresses, _prefs, _cursor, _messages and _candidates, the forwarded
* booking mails, the bookings read from them and the switches and addresses behind them)
* booking mails, the bookings read from them and the switches and addresses behind them
* + trip_addon_overrides, the owner's per-trip on or off for a trip addon)
* is registered, and no two share a class name (a silent duplicate would
* shadow one of them in `orm.getMetadata()`, exactly the kind of drift
* `entity-schema-parity.test.ts` can't see — it iterates the metadata map,
* which de-duplicates by class name).
*/
it('ENT-003: exactly 173 entities, every class name unique', () => {
expect(ALL_ENTITIES.length).toBe(173);
it('ENT-003: exactly 174 entities, every class name unique', () => {
expect(ALL_ENTITIES.length).toBe(174);
const names = ALL_ENTITIES.map((s) => s.meta.className);
expect(new Set(names).size).toBe(names.length);
});
@@ -66,7 +66,8 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
// + mail_ingest_addresses: id, token_hash, token_enc, user_id; mail_ingest_prefs: id, user_id; mail_ingest_cursor:
// id, transport, mailbox; mail_ingest_messages: id, transport, dedupe_hash, user_id, size; mail_ingest_candidates:
// id, message_id, user_id (each row is written whole by the mail import, which always knows all of them).
const KNOWN_UNINITIALISED_NOT_NULL_SCALARS = 632;
// + trip_addon_overrides: trip_id, addon_id, enabled (an override is only ever written whole, by the trip owner).
const KNOWN_UNINITIALISED_NOT_NULL_SCALARS = 635;
describe('entity class fields never carry a NaN initialiser (task-4-review-shape.md, Important 1)', () => {
it('CLASSFIELD-001: new X() has no own property whose value is NaN, across all 133 entities', () => {
@@ -115,6 +115,7 @@ const UNNUMBERED = [
'Migration20200101046450_a_carried_over_half_day_stays_half',
'Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped',
'Migration20200101046480_an_address_changed_after_an_admin_made_the_account',
'Migration20200101046490_a_trip_can_switch_its_own_addons',
];
// The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it.
@@ -0,0 +1,133 @@
/**
* `TripAddonOverridesRepository`: a trip's own on or off for a trip addon.
* Seeded and read through the ORM factories on the migrated snapshot, whose
* seeded addon rows (packing, budget, tours, ...) the foreign key points at.
*/
import { TripAddonOverrides } from '../../../../src/db/entities/TripAddonOverrides.entity';
import { Trips } from '../../../../src/db/entities/Trips.entity';
import type { TripAddonOverridesRepository } from '../../../../src/db/repositories/TripAddonOverrides.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { countRows, deleteRows, findRow } from '../../../helpers/factories/rows';
import { addTripMember, makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: TripAddonOverridesRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(TripAddonOverrides);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
async function ownerAndTrip() {
const { user } = await makeUser(t);
const trip = await makeTrip(t, user.id);
return { user, trip };
}
describe('TripAddonOverridesRepository', () => {
it('TAOREPO-001: a trip without choices lists nothing and follows the instance for every addon', async () => {
const { trip } = await ownerAndTrip();
expect(await repo.listForTrip(trip.id)).toEqual([]);
expect(await repo.findForTrip(trip.id, 'budget')).toBeNull();
});
it('TAOREPO-002: upsertOverride writes on and off as booleans and replaces an earlier choice in place', async () => {
const { trip } = await ownerAndTrip();
await repo.upsertOverride(trip.id, 'budget', false);
await repo.upsertOverride(trip.id, 'packing', true);
expect(await repo.findForTrip(trip.id, 'budget')).toBe(false);
expect(await repo.findForTrip(trip.id, 'packing')).toBe(true);
await repo.upsertOverride(trip.id, 'budget', true);
expect(await repo.listForTrip(trip.id)).toEqual([
{ addon_id: 'budget', enabled: true },
{ addon_id: 'packing', enabled: true },
]);
expect(await countRows(t, TripAddonOverrides, { trip: trip.id })).toBe(2);
const row = await findRow(t, TripAddonOverrides, { trip: trip.id, addon: 'budget' });
expect(row?.updated_at).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
});
it('TAOREPO-003: deleteOverride drops one choice and leaves the others', async () => {
const { trip } = await ownerAndTrip();
await repo.upsertOverride(trip.id, 'budget', false);
await repo.upsertOverride(trip.id, 'tours', true);
await repo.deleteOverride(trip.id, 'budget');
expect(await repo.listForTrip(trip.id)).toEqual([{ addon_id: 'tours', enabled: true }]);
await repo.deleteOverride(trip.id, 'budget');
expect(await repo.listForTrip(trip.id)).toEqual([{ addon_id: 'tours', enabled: true }]);
});
it('TAOREPO-004: the choices of one trip never show on another', async () => {
const { user, trip } = await ownerAndTrip();
const other = await makeTrip(t, user.id);
await repo.upsertOverride(trip.id, 'budget', false);
expect(await repo.listForTrip(other.id)).toEqual([]);
expect(await repo.findForTrip(other.id, 'budget')).toBeNull();
});
it('TAOREPO-005: copyForTrip gives the new trip the same choices and leaves the source alone', async () => {
const { user, trip } = await ownerAndTrip();
const copy = await makeTrip(t, user.id);
await repo.upsertOverride(trip.id, 'budget', false);
await repo.upsertOverride(trip.id, 'roadtrip', true);
await repo.copyForTrip(trip.id, copy.id);
await repo.copyForTrip(trip.id, copy.id);
const expected = [
{ addon_id: 'budget', enabled: false },
{ addon_id: 'roadtrip', enabled: true },
];
expect(await repo.listForTrip(copy.id)).toEqual(expected);
expect(await repo.listForTrip(trip.id)).toEqual(expected);
});
it('TAOREPO-006: copyForTrip of a trip without choices copies nothing', async () => {
const { user, trip } = await ownerAndTrip();
const copy = await makeTrip(t, user.id);
await repo.copyForTrip(trip.id, copy.id);
expect(await repo.listForTrip(copy.id)).toEqual([]);
});
it('TAOREPO-007: anyEnabledForUser sees a trip the user owns or belongs to that switched the addon on', async () => {
const { user: owner, trip } = await ownerAndTrip();
const { user: member } = await makeUser(t);
const { user: stranger } = await makeUser(t);
await addTripMember(t, trip.id, member.id, owner.id);
expect(await repo.anyEnabledForUser(owner.id, 'roadtrip')).toBe(false);
await repo.upsertOverride(trip.id, 'roadtrip', true);
expect(await repo.anyEnabledForUser(owner.id, 'roadtrip')).toBe(true);
expect(await repo.anyEnabledForUser(member.id, 'roadtrip')).toBe(true);
expect(await repo.anyEnabledForUser(stranger.id, 'roadtrip')).toBe(false);
expect(await repo.anyEnabledForUser(owner.id, 'tours')).toBe(false);
});
it('TAOREPO-008: a trip that switched the addon off counts for nobody', async () => {
const { user, trip } = await ownerAndTrip();
await repo.upsertOverride(trip.id, 'roadtrip', false);
expect(await repo.anyEnabledForUser(user.id, 'roadtrip')).toBe(false);
});
it('TAOREPO-009: the choices go with their trip', async () => {
const { trip } = await ownerAndTrip();
await repo.upsertOverride(trip.id, 'budget', false);
await deleteRows(t, Trips, { id: trip.id });
expect(await countRows(t, TripAddonOverrides, { trip: trip.id })).toBe(0);
});
});