mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
3f38621024
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.
294 lines
14 KiB
TypeScript
294 lines
14 KiB
TypeScript
import { AppSettings } from '../db/entities/AppSettings.entity';
|
|
import { IdempotencyKeys } from '../db/entities/IdempotencyKeys.entity';
|
|
import { Users } from '../db/entities/Users.entity';
|
|
import { WebauthnCredentials } from '../db/entities/WebauthnCredentials.entity';
|
|
import {
|
|
trekDemoToolGate,
|
|
trekMcpAccessPolicy,
|
|
trekMcpErrorMapper,
|
|
trekMcpInputMapper,
|
|
trekMcpValidateAccess,
|
|
} from '../mcp/nest-mcp-policy';
|
|
import mikroOrmConfig from '../mikro-orm.config';
|
|
import { combineToolGates, McpModule } from '../nest-mcp';
|
|
import { AccommodationsModule } from './accommodations/accommodations.module';
|
|
import { AddonsModule } from './addons/addons.module';
|
|
import { AddonsService } from './addons/addons.service';
|
|
import { trekTripAddonToolGate } from './addons/trip-addon-tool-gate';
|
|
import { AdminModule } from './admin/admin.module';
|
|
import { AiConnectionsModule } from './ai-connections/ai-connections.module';
|
|
import { AirportsModule } from './airports/airports.module';
|
|
import { AppConfigModule } from './app-config/app-config.module';
|
|
import { AssignmentsModule } from './assignments/assignments.module';
|
|
import { AssistantModule } from './assistant/assistant.module';
|
|
import { AtlasModule } from './atlas/atlas.module';
|
|
import { AuditModule } from './audit/audit.module';
|
|
import { GlobalAuthGuard } from './auth-core/global-auth.guard';
|
|
import { MfaPolicyGuard } from './auth-core/mfa-policy.guard';
|
|
import { AuthModule } from './auth/auth.module';
|
|
import { SessionRenewalInterceptor } from './auth/session-renewal.interceptor';
|
|
import { BackupModule } from './backup/backup.module';
|
|
import { BookingImportModule } from './booking-import/booking-import.module';
|
|
import { BudgetModule } from './budget/budget.module';
|
|
import { CategoriesModule } from './categories/categories.module';
|
|
import { CollabModule } from './collab/collab.module';
|
|
import { CollectionsModule } from './collections/collections.module';
|
|
import { DemoModule } from './common/demo.module';
|
|
import { DemoService } from './common/demo.service';
|
|
import { IdempotencyCleanupJob } from './common/idempotency-cleanup.job';
|
|
import { IdempotencyInterceptor } from './common/idempotency.interceptor';
|
|
import { ManagedGuard } from './common/managed.guard';
|
|
import { NulFreeInputPipe } from './common/nul-free-input';
|
|
import { TrekExceptionFilter } from './common/trek-exception.filter';
|
|
import { ZodValidationPipe } from './common/zod-validation.pipe';
|
|
import { ConfigModule } from './config/config.module';
|
|
import { DatabaseLifecycleModule } from './database/database-lifecycle.module';
|
|
import { OrmModule } from './database/orm.module';
|
|
import { DayNotesModule } from './day-notes/day-notes.module';
|
|
import { DaysModule } from './days/days.module';
|
|
import { DocSyncModule } from './doc-sync/doc-sync.module';
|
|
import { FeedsModule } from './feeds/feeds.module';
|
|
import { FerratasModule } from './ferratas/ferratas.module';
|
|
import { FilesModule } from './files/files.module';
|
|
import { FlightLiveModule } from './flight-live/flight-live.module';
|
|
import { GeoModule } from './geo/geo.module';
|
|
import { GoogleQuotaModule } from './google-quota/google-quota.module';
|
|
import { HealthModule } from './health/health.module';
|
|
import { HelpModule } from './help/help.module';
|
|
import { AirtrailModule } from './integrations/airtrail.module';
|
|
import { DawarichModule } from './integrations/dawarich.module';
|
|
import { JourneyRecordingsModule } from './journey-recordings/journey-recordings.module';
|
|
import { JourneyAccessModule } from './journey/journey-access.module';
|
|
import { JourneyModule } from './journey/journey.module';
|
|
import { TripJournalLinkModule } from './journey/trip-journal-link.module';
|
|
import { LlmParseModule } from './llm-parse/llm-parse.module';
|
|
import { MailIngestModule } from './mail-ingest/mail-ingest.module';
|
|
import { ManagedExtModule } from './managed/managed-ext.module';
|
|
import { MapsModule } from './maps/maps.module';
|
|
import { McpTransportModule } from './mcp-transport/mcp-transport.module';
|
|
import { MemoriesModule } from './memories/memories.module';
|
|
import { NativeAuthModule } from './native-auth/native-auth.module';
|
|
import { NotificationsModule } from './notifications/notifications.module';
|
|
import { OauthModule } from './oauth/oauth.module';
|
|
import { OidcModule } from './oidc/oidc.module';
|
|
import { PackingModule } from './packing/packing.module';
|
|
import { PaddleObstaclesModule } from './paddle-obstacles/paddle-obstacles.module';
|
|
import { PaddleRoutingModule } from './paddle-routing/paddle-routing.module';
|
|
import { PermissionsModule } from './permissions/permissions.module';
|
|
import { PhotosModule } from './photos/photos.module';
|
|
import { PlaceContribModule } from './place-contrib/place-contrib.module';
|
|
import { PlaceEnrichmentModule } from './place-enrichment/place-enrichment.module';
|
|
import { PlaceImportModule } from './place-import/place-import.module';
|
|
import { PlaceShadowModule } from './place-shadow/place-shadow.module';
|
|
import { PlacesModule } from './places/places.module';
|
|
import { PlatformModule } from './platform/platform.module';
|
|
import { SpaFallbackFilter } from './platform/spa-fallback.filter';
|
|
import { PluginsModule } from './plugins/plugins.module';
|
|
import { PublicApiModule } from './public-api/public-api.module';
|
|
import { RealtimeGatewayModule } from './realtime/realtime-gateway.module';
|
|
import { RealtimeModule } from './realtime/realtime.module';
|
|
import { ReceiptScanModule } from './receipt-scan/receipt-scan.module';
|
|
import { ReservationImportModule } from './reservation-import/reservation-import.module';
|
|
import { ReservationsModule } from './reservations/reservations.module';
|
|
import { RoadtripModule } from './roadtrip/roadtrip.module';
|
|
import { RouteUsageModule } from './route-usage/route-usage.module';
|
|
import { RoutingRelayModule } from './routing-relay/routing-relay.module';
|
|
import { SchedulingModule } from './scheduling/scheduling.module';
|
|
import { SchoolHolidaysModule } from './school-holidays/school-holidays.module';
|
|
import { ServiceIdentityModule } from './service-identity/service-identity.module';
|
|
import { SessionsModule } from './sessions/sessions.module';
|
|
import { SettingsModule } from './settings/settings.module';
|
|
import { ShareModule } from './share/share.module';
|
|
import { SkiRoutingModule } from './ski-routing/ski-routing.module';
|
|
import { StorageModule } from './storage/storage.module';
|
|
import { SystemNoticesModule } from './system-notices/system-notices.module';
|
|
import { TagsModule } from './tags/tags.module';
|
|
import { TodoModule } from './todo/todo.module';
|
|
import { ToursModule } from './tours/tours.module';
|
|
import { TransitModule } from './transit/transit.module';
|
|
import { TripInviteModule } from './trip-invite/trip-invite.module';
|
|
import { TripAccessService } from './trip-membership/trip-access.service';
|
|
import { TripMembershipModule } from './trip-membership/trip-membership.module';
|
|
import { TripsModule } from './trips/trips.module';
|
|
import { VacayModule } from './vacay/vacay.module';
|
|
import { WalletPassModule } from './wallet-pass/wallet-pass.module';
|
|
import { WandererModule } from './wanderer/wanderer.module';
|
|
import { WeatherModule } from './weather/weather.module';
|
|
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
|
import { Module } from '@nestjs/common';
|
|
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
|
|
|
|
/**
|
|
* Root NestJS module for the incremental migration. Domain modules
|
|
* (weather, notifications, integrations, ...) get registered here as they are
|
|
* migrated.
|
|
*/
|
|
@Module({
|
|
imports: [
|
|
AppConfigModule,
|
|
DemoModule,
|
|
RealtimeModule,
|
|
RealtimeGatewayModule,
|
|
JourneyAccessModule,
|
|
SchedulingModule,
|
|
McpModule.forRoot({
|
|
accessPolicy: trekMcpAccessPolicy,
|
|
validateAccess: trekMcpValidateAccess,
|
|
// The demo write block first, then whether the addon of a tool applies
|
|
// to the trip the call names (trekTripAddonToolGate).
|
|
toolGate: {
|
|
imports: [AddonsModule, TripMembershipModule],
|
|
inject: [DemoService, AddonsService, TripAccessService],
|
|
useFactory: (demo: DemoService, addons: AddonsService, trips: TripAccessService) =>
|
|
combineToolGates(
|
|
trekDemoToolGate((id) => demo.isDemoUserId(id)),
|
|
trekTripAddonToolGate(addons, trips),
|
|
),
|
|
},
|
|
errorMapper: trekMcpErrorMapper,
|
|
inputMapper: trekMcpInputMapper,
|
|
}),
|
|
HealthModule,
|
|
PlatformModule,
|
|
McpTransportModule,
|
|
WeatherModule,
|
|
PublicApiModule,
|
|
HelpModule,
|
|
AirportsModule,
|
|
ConfigModule,
|
|
SystemNoticesModule,
|
|
GeoModule,
|
|
MapsModule,
|
|
PlaceImportModule,
|
|
GoogleQuotaModule,
|
|
PlaceContribModule,
|
|
PlaceEnrichmentModule,
|
|
PlaceShadowModule,
|
|
RouteUsageModule,
|
|
CategoriesModule,
|
|
TagsModule,
|
|
NotificationsModule,
|
|
AtlasModule,
|
|
VacayModule,
|
|
SchoolHolidaysModule,
|
|
PackingModule,
|
|
TodoModule,
|
|
BudgetModule,
|
|
ReservationsModule,
|
|
DaysModule,
|
|
DayNotesModule,
|
|
AccommodationsModule,
|
|
AssignmentsModule,
|
|
PlacesModule,
|
|
RoadtripModule,
|
|
ToursModule,
|
|
WandererModule,
|
|
FerratasModule,
|
|
PaddleObstaclesModule,
|
|
PaddleRoutingModule,
|
|
SkiRoutingModule,
|
|
TripsModule,
|
|
CollabModule,
|
|
FilesModule,
|
|
DocSyncModule,
|
|
PhotosModule,
|
|
MemoriesModule,
|
|
AirtrailModule,
|
|
FlightLiveModule,
|
|
DawarichModule,
|
|
JourneyModule,
|
|
JourneyRecordingsModule,
|
|
TripJournalLinkModule,
|
|
CollectionsModule,
|
|
ShareModule,
|
|
TripInviteModule,
|
|
TransitModule,
|
|
FeedsModule,
|
|
SettingsModule,
|
|
ServiceIdentityModule,
|
|
RoutingRelayModule,
|
|
WalletPassModule,
|
|
StorageModule,
|
|
BackupModule,
|
|
AuthModule,
|
|
SessionsModule,
|
|
OidcModule,
|
|
NativeAuthModule,
|
|
OauthModule,
|
|
AdminModule,
|
|
AddonsModule,
|
|
AuditModule,
|
|
PermissionsModule,
|
|
PluginsModule,
|
|
BookingImportModule,
|
|
ReservationImportModule,
|
|
ReceiptScanModule,
|
|
LlmParseModule,
|
|
MailIngestModule,
|
|
AiConnectionsModule,
|
|
AssistantModule,
|
|
ManagedExtModule,
|
|
// registerRequestContext off: bootstrap.ts mounts the per-request fork as a
|
|
// pathless middleware instead, because the module's own '{*all}' route
|
|
// turned a malformed %-escape into a 400 before any handler ran (UPLOADS-P16).
|
|
// Partial e2e harnesses keep the default; see tests/helpers/test-orm.ts.
|
|
MikroOrmModule.forRoot({ ...mikroOrmConfig, registerRequestContext: false }),
|
|
// `DatabaseModule` (the raw better-sqlite3 `db` Proxy + `DatabaseService`
|
|
// facade) is gone — Plan 4 Task 4 deleted it once every domain moved onto
|
|
// repositories/`UnitOfWork`. `OrmModule` is the module that survives.
|
|
OrmModule,
|
|
// The connection's open/close/reopen, resolved by buildApp() and index.ts.
|
|
DatabaseLifecycleModule,
|
|
// MfaPolicyGuard (Plan 3b Task 1) is registered directly below as this
|
|
// module's own APP_GUARD, never per-controller — so it is the one place
|
|
// that needs `forFeature` for its three repositories; `Users` is not
|
|
// otherwise global (JwtAuthGuard and friends inject `EntityManager`
|
|
// instead precisely to avoid needing this everywhere — see their own
|
|
// docstrings). IdempotencyKeys: Plan 4 Task 1 — IdempotencyInterceptor/
|
|
// IdempotencyCleanupJob are ALSO registered directly here (common/ has
|
|
// no module of its own), so this is the one place their repository's
|
|
// `forFeature` needs to live too.
|
|
MikroOrmModule.forFeature([AppSettings, Users, WebauthnCredentials, IdempotencyKeys]),
|
|
],
|
|
providers: [
|
|
// Default-deny: a route is authenticated unless it carries @Public() or
|
|
// @OptionalAuth(), or declares its own @UseGuards chain. Protection used to
|
|
// be opt-in, which made a forgotten guard a silent bypass instead of an error.
|
|
{ provide: APP_GUARD, useClass: GlobalAuthGuard },
|
|
// Second, and only second: it reads the user the guard above resolved
|
|
// instead of verifying the token a second time, which is what the Express
|
|
// middleware it replaces did on every /api request.
|
|
{ provide: APP_GUARD, useClass: MfaPolicyGuard },
|
|
// Third, and only third: a stranger gets the 401 the two above would have
|
|
// given them rather than a 403 that confirms the route exists. Inert unless
|
|
// the instance is centrally administered AND the route carries the marker.
|
|
{ provide: APP_GUARD, useClass: ManagedGuard },
|
|
// Global error-envelope normaliser (DI-registered so it also catches
|
|
// framework-level exceptions like the not-found handler).
|
|
{ provide: APP_FILTER, useClass: TrekExceptionFilter },
|
|
// SPA fallback: serves index.html for unmatched GETs in production (the Nest
|
|
// equivalent of the legacy Express app.get('*') catch-all). @Catch(NotFoundException)
|
|
// is more specific than TrekExceptionFilter, so Nest routes 404s here.
|
|
{ provide: APP_FILTER, useClass: SpaFallbackFilter },
|
|
// Replays the X-Idempotency-Key the client sends on every write, so retried
|
|
// mutations don't double-apply.
|
|
{ provide: APP_INTERCEPTOR, useClass: IdempotencyInterceptor },
|
|
// Sliding session renewal: re-issues the trek_session cookie once a
|
|
// cookie-authenticated token is past half its lifetime (#1927).
|
|
{ provide: APP_INTERCEPTOR, useClass: SessionRenewalInterceptor },
|
|
// Its nightly TTL purge — a provider here because common/ has no module.
|
|
IdempotencyCleanupJob,
|
|
// First of the global pipes: every handler argument without NUL, which
|
|
// after the nulFreeRequest middleware only changes what a route parsed
|
|
// itself, a multipart body (common/nul-free-input.ts).
|
|
{ provide: APP_PIPE, useClass: NulFreeInputPipe },
|
|
// Global Zod validation: any parameter typed with a createZodDto class
|
|
// (the <domain>.dto.ts wrappers over @trek/shared schemas) is validated;
|
|
// everything else passes through untouched. Paired with the boot gate in
|
|
// common/validate-body-contracts.ts so unvalidated mutation bodies refuse
|
|
// to boot instead of shipping silently.
|
|
{ provide: APP_PIPE, useClass: ZodValidationPipe },
|
|
],
|
|
})
|
|
export class AppModule {}
|