feat(demo): a fixed addon set on every boot and after every hourly reset

A demo instance now runs with one explicit addon policy (demo-addon-policy.ts)
instead of whatever its admin last switched: every trip, global and
integration addon on, and off only what reaches a server of the visitor's
own, the five document sync providers, wanderer under Tours, the Immich and
Synology Photos providers, AirTrail and Dawarich. The switches under the
addons (Tours maps, piste routing, bag tracking, the collab features) are on.

The demo seed applies it, together with the demo account's English
language, on every boot and after every reopen, and the hourly reset applies
it once more after the baseline swap, in one transaction each. A test pins
that every seeded addon and provider is named in exactly one list, so a new
addon has to be classified before it ships.
This commit is contained in:
Maurice
2026-10-24 23:33:57 +02:00
parent eae74c5605
commit 03493d4f79
5 changed files with 313 additions and 1 deletions
@@ -1,5 +1,9 @@
import { currentTimestampKysely } from '../dialect/kysely-functions';
import { Addons } from '../entities/Addons.entity';
import { AppSettings } from '../entities/AppSettings.entity';
import { DocumentProviders } from '../entities/DocumentProviders.entity';
import { PhotoProviders } from '../entities/PhotoProviders.entity';
import { Settings } from '../entities/Settings.entity';
import { Trips } from '../entities/Trips.entity';
import { Users } from '../entities/Users.entity';
import type { DB } from '../kysely/db';
@@ -36,6 +40,20 @@ export interface DemoInstanceKeyRow {
value: string | null;
}
/** One shelf of the demo addon policy (`demo/demo-addon-policy.ts`): the rows it switches on and off. */
export interface DemoShelfStates {
readonly on: readonly string[];
readonly off: readonly string[];
}
/** The states `applyAddonStates` writes: three shelves and the switches under them in `app_settings`. */
export interface DemoAddonStates {
readonly addons: DemoShelfStates;
readonly photoProviders: DemoShelfStates;
readonly documentProviders: DemoShelfStates;
readonly appSettings: Readonly<Record<string, string>>;
}
/**
* DMS9's 13-column `places` insert tuple — kept as a tuple, not an object,
* so `demo-seed.ts`'s three ~30-row example-trip data arrays (Tokyo/Kyoto,
@@ -400,4 +418,36 @@ export class DemoRepository {
await repo.upsert({ key: row.key, value: row.value }, { onConflictFields: ['key'], onConflictAction: 'merge' });
}
}
// ---------------------------------------------------------------------
// The fixed demo set (demo-addon-policy.ts), applied by the seed and the reset
// ---------------------------------------------------------------------
/**
* Writes the demo's addon states in one transaction, so a reset that fails
* halfway does not leave half the shelf switched. A named row the database
* does not hold is an update that matches nothing, never an insert: the
* seeders own which rows exist.
*/
async applyAddonStates(states: DemoAddonStates): Promise<void> {
this.validateRequestContext();
await this.em.transactional(async () => {
const addons = this.em.getRepository(Addons);
for (const id of states.addons.on) await addons.setEnabled(id, true);
for (const id of states.addons.off) await addons.setEnabled(id, false);
const photos = this.em.getRepository(PhotoProviders);
for (const id of states.photoProviders.on) await photos.setEnabled(id, 1);
for (const id of states.photoProviders.off) await photos.setEnabled(id, 0);
const documents = this.em.getRepository(DocumentProviders);
for (const id of states.documentProviders.on) await documents.setEnabled(id, 1);
for (const id of states.documentProviders.off) await documents.setEnabled(id, 0);
const settings = this.em.getRepository(AppSettings);
for (const [key, value] of Object.entries(states.appSettings)) await settings.setValue(key, value);
});
}
/** A user's own settings row, written as given (the demo account's fixed language). Composes `SettingsRepository#upsertForUser`. */
async setUserSetting(userId: number, key: string, value: string): Promise<void> {
await this.em.getRepository(Settings).upsertForUser(userId, key, value);
}
}
+77
View File
@@ -0,0 +1,77 @@
import type { DemoAddonStates, DemoRepository } from '../db/repositories/DemoRepository';
/**
* What a demo instance offers, whatever its admin switched during the hour.
*
* The demo shows everything TREK can do on its own: every trip, global and
* integration addon is on. What stays off is what needs a server of the
* visitor's own, which a public demo has no address for and no business
* reaching: the document sync providers, the wanderer connection under Tours,
* the photo providers under Journey, AirTrail and Dawarich.
*
* Each list names its rows outright instead of "everything but", so a release
* that adds an addon has to decide where it goes: the policy test fails on a
* seeded row that is in neither list. The demo seed applies this on every boot
* and after every reopen, and the hourly reset applies it once more after the
* swap, so the baseline cannot bring back an admin's toggle.
*/
export const DEMO_ADDON_POLICY = {
/** Rows of the `addons` table. */
addons: {
on: [
// Trip addons
'packing',
'budget',
'documents',
'collab',
'roadtrip',
'tours',
// Global addons
'vacay',
'atlas',
'journey',
'collections',
// Integrations TREK runs by itself
'mcp',
'naver_list_import',
'llm_parsing',
'ai_assistant',
],
off: ['airtrail', 'dawarich'],
},
/** Rows of `photo_providers`, Journey's shelf. */
photoProviders: {
on: [],
off: ['immich', 'synologyphotos'],
},
/** Rows of `document_providers`, the Documents addon's shelf. */
documentProviders: {
on: [],
off: ['paperless', 'papra', 'nextcloud', 'opencloud', 'synologydrive'],
},
/**
* The switches that sit under an addon in the admin panel, stored in
* `app_settings`. wanderer is the one under Tours that reaches a server of
* the visitor's own; the rest are on.
*/
appSettings: {
tours_wanderer_enabled: 'false',
tours_hiking_map_enabled: 'true',
tours_cycle_map_enabled: 'true',
ski_routing_enabled: 'true',
bag_tracking_enabled: 'true',
collab_chat_enabled: 'true',
collab_notes_enabled: 'true',
collab_links_enabled: 'true',
collab_polls_enabled: 'true',
collab_whatsnext_enabled: 'true',
},
} as const satisfies DemoAddonStates;
/** Puts the instance's addon states to the demo set, in one transaction. A row the database does not hold is skipped. */
export async function applyDemoAddonPolicy(
demo: DemoRepository,
policy: DemoAddonStates = DEMO_ADDON_POLICY,
): Promise<void> {
await demo.applyAddonStates(policy);
}
+10
View File
@@ -9,6 +9,7 @@ import {
import type { CarriedUserSessionRow } from '../db/repositories/UserSessions.repository';
import { dbNow } from '../db/types';
import { DatabaseConnectionLostError, type DatabaseBackupStrategy } from '../nest/database/database-backup.interface';
import { applyDemoAddonPolicy } from './demo-addon-policy';
import { RequestContext, type EntityManager } from '@mikro-orm/core';
import fs from 'fs';
@@ -155,6 +156,15 @@ async function resetDemoUser(database: DatabaseBackupStrategy): Promise<void> {
console.error('[Demo Reset] Failed to restore the active sessions:', e instanceof Error ? e.message : e);
}
}
// The reopen's schema bootstrap ran the demo seed, which applies the demo
// addon set already. Applied here once more so the reset itself states
// what the demo offers after the hour, whatever the baseline holds.
try {
await applyDemoAddonPolicy(demoFresh);
} catch (e: unknown) {
console.error('[Demo Reset] Failed to apply the demo addon set:', e instanceof Error ? e.message : e);
}
});
console.log('[Demo Reset] Database restored from baseline');
+9 -1
View File
@@ -1,6 +1,7 @@
import { readEnv } from '../app-config';
import { DemoRepository, type NewDemoPlaceRow } from '../db/repositories/DemoRepository';
import { DEMO_PASS } from '../nest/common/demo';
import { DEMO_ACCOUNT_SETTINGS, DEMO_PASS } from '../nest/common/demo';
import { applyDemoAddonPolicy } from './demo-addon-policy';
// Static like in demo-reset.job.ts: the module top is inert, everything that
// touches the database happens inside the functions.
import { markExampleTripsSeeded, requireEntityManager } from './demo-reset';
@@ -53,6 +54,13 @@ async function seedDemoData(): Promise<{ adminId: number; demoId: number }> {
// Disable registration in demo mode
await demo.setAllowRegistrationFalse();
// The fixed demo set: the addons every visitor can try, and the demo
// account's language. This runs on every boot and again after every reopen
// (the hourly reset included), so neither an admin's toggle nor an old
// baseline outlasts the hour.
await applyDemoAddonPolicy(demo);
for (const [key, value] of Object.entries(DEMO_ACCOUNT_SETTINGS)) await demo.setUserSetting(demoId, key, value);
// Check if admin already has example trips
const adminTripCount = await demo.countTripsByUser(adminId);
if (adminTripCount > 0) {