Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

refactor(vacay): the invite and share tools ask a kernel user lookup instead of injecting UsersRepository

Maurice0f6ae8b9285c
diff --git a/server/scripts/import-boundaries-baseline.json b/server/scripts/import-boundaries-baseline.json
index 07740cb1d..82c83cbe3 100644
--- a/server/scripts/import-boundaries-baseline.json
+++ b/server/scripts/import-boundaries-baseline.json
@@ -301,8 +301,7 @@
"trip-read-model -> Trips (owned by trips)",
"unsplash -> AppSettings (owned by settings)",
"unsplash -> Users (owned by auth)",
- "vacay -> SchoolHolidayRegions (owned by school-holidays)",
- "vacay -> Users (owned by auth)"
+ "vacay -> SchoolHolidayRegions (owned by school-holidays)"
],
"providersImportOrchestrator": []
}
diff --git a/server/src/nest/auth-core/user-lookup.module.ts b/server/src/nest/auth-core/user-lookup.module.ts
new file mode 100644
index 000000000..62b7b4ea8
--- /dev/null
+++ b/server/src/nest/auth-core/user-lookup.module.ts
@@ -0,0 +1,12 @@
+import { Users } from '../../db/entities/Users.entity';
+import { UserLookupService } from './user-lookup.service';
+import { MikroOrmModule } from '@mikro-orm/nestjs';
+import { Module } from '@nestjs/common';
+
+/** A leaf module handing UserLookupService out; it imports no domain. */
+@Module({
+ imports: [MikroOrmModule.forFeature([Users])],
+ providers: [UserLookupService],
+ exports: [UserLookupService],
+})
+export class UserLookupModule {}
diff --git a/server/src/nest/auth-core/user-lookup.service.ts b/server/src/nest/auth-core/user-lookup.service.ts
new file mode 100644
index 000000000..9aa837b16
--- /dev/null
+++ b/server/src/nest/auth-core/user-lookup.service.ts
@@ -0,0 +1,20 @@
+import { Users } from '../../db/entities/Users.entity';
+import type { UsersRepository } from '../../db/repositories/Users.repository';
+import { InjectRepository } from '@mikro-orm/nestjs';
+import { Injectable } from '@nestjs/common';
+
+/**
+ * Who an account is, for a domain that needs a user's name or address and
+ * nothing else of the auth domain. Reading the users table belongs to auth;
+ * this kernel service answers the narrow question, so such a domain imports
+ * neither AuthModule (and its graph) nor a repository it does not own.
+ */
+@Injectable()
+export class UserLookupService {
+ constructor(@InjectRepository(Users) private readonly users: UsersRepository) {}
+
+ /** The account's username and email, or undefined for an unknown id. */
+ async usernameAndEmail(userId: number): Promise<{ username: string; email: string } | undefined> {
+ return this.users.findUsernameEmail(userId);
+ }
+}
diff --git a/server/src/nest/vacay/vacay.mcp.ts b/server/src/nest/vacay/vacay.mcp.ts
index ba01311ed..9ffcc82bc 100644
--- a/server/src/nest/vacay/vacay.mcp.ts
+++ b/server/src/nest/vacay/vacay.mcp.ts
@@ -14,9 +14,7 @@ import {
idSchema,
} from '@trek/shared';
import type { VacayUpdatePlanRequest } from '@trek/shared';
-import { InjectRepository } from '@mikro-orm/nestjs';
-import { Users } from '../../db/entities/Users.entity';
-import type { UsersRepository } from '../../db/repositories/Users.repository';
+import { UserLookupService } from '../auth-core/user-lookup.service';
import { ADDON_IDS } from '../../addons';
import { VacayService } from './vacay.service';
import { addonGate } from '../addons/addon-gate';
@@ -84,7 +82,7 @@ export class VacayMcp {
constructor(
private readonly vacay: VacayService,
// The invite and share tools need the caller's username and email.
- @InjectRepository(Users) private readonly users: UsersRepository,
+ private readonly users: UserLookupService,
readonly addons: AddonsService,
) {}
@@ -185,7 +183,7 @@ export class VacayMcp {
})
async sendVacayInvite({ targetUserId }: { targetUserId: number }, ctx: McpContext) {
const planId = await this.vacay.getActivePlanId(ctx.userId);
- const me = await this.users.findUsernameEmail(ctx.userId);
+ const me = await this.users.usernameAndEmail(ctx.userId);
if (!me) return errorResult('User not found.');
await this.vacay.sendInvite(planId, ctx.userId, me.username, me.email, targetUserId);
return ok({ success: true });
@@ -558,7 +556,7 @@ export class VacayMcp {
access: { group: 'vacay', mode: 'write' },
})
async shareVacayCalendar({ targetUserId }: { targetUserId: number }, ctx: McpContext) {
- const me = await this.users.findUsernameEmail(ctx.userId);
+ const me = await this.users.usernameAndEmail(ctx.userId);
if (!me) return errorResult('User not found.');
await this.vacay.shareCalendar(ctx.userId, me.email, targetUserId);
return ok({ success: true });
diff --git a/server/src/nest/vacay/vacay.module.ts b/server/src/nest/vacay/vacay.module.ts
index 3237cbc37..52a81c853 100644
--- a/server/src/nest/vacay/vacay.module.ts
+++ b/server/src/nest/vacay/vacay.module.ts
@@ -5,7 +5,7 @@ import { VacayService } from './vacay.service';
import { VacayRpc } from './vacay.rpc';
import { PluginGuardsModule } from '../../nest-rpc/plugin-guards.module';
import { VacayMcp } from './vacay.mcp';
-import { Users } from '../../db/entities/Users.entity';
+import { UserLookupModule } from '../auth-core/user-lookup.module';
import { AddonsModule } from '../addons/addons.module';
import { NotificationsModule } from '../notifications/notifications.module';
import { VacayPlans } from '../../db/entities/VacayPlans.entity';
@@ -37,14 +37,14 @@ import { SchoolHolidayRegions } from '../../db/entities/SchoolHolidayRegions.ent
@Module({
imports: [
NotificationsModule,
+ // VacayMcp's invite and share tools read the caller's username/email.
+ UserLookupModule,
PluginGuardsModule,
AddonsModule,
MikroOrmModule.forFeature([
VacayPlans, VacayPlanMembers, VacayYears, VacayUserYears, VacayUserColors,
VacayEntries, VacayCompanyHolidays, VacayHolidayCalendars, VacayShares, VacayUserSettings,
SchoolHolidayRegions,
- // VacayMcp's invite and share tools read the caller's username/email.
- Users,
]),
],
controllers: [VacayController],
diff --git a/server/tests/unit/nest/auth-core/user-lookup.service.test.ts b/server/tests/unit/nest/auth-core/user-lookup.service.test.ts
new file mode 100644
index 000000000..eaf350973
--- /dev/null
+++ b/server/tests/unit/nest/auth-core/user-lookup.service.test.ts
@@ -0,0 +1,38 @@
+/**
+ * UserLookupService answers a user's name and address for domains that need
+ * nothing else of auth (VacayMcp's invite and share tools).
+ */
+import { UserLookupModule } from '../../../../src/nest/auth-core/user-lookup.module';
+import { UserLookupService } from '../../../../src/nest/auth-core/user-lookup.service';
+import { createSnapshotTestDb } from '../../../helpers/db-mock';
+import { makeUser } from '../../../helpers/factories/users';
+import { createTestModule, type TestModule } from '../../../helpers/test-module';
+import { MikroORM } from '@mikro-orm/core';
+
+import { afterAll, beforeAll, describe, expect, it } from 'vitest';
+
+const db = createSnapshotTestDb();
+let t: TestModule;
+
+beforeAll(async () => {
+ t = await createTestModule({ db, imports: [UserLookupModule] });
+});
+
+afterAll(async () => {
+ await t.close();
+ db.close();
+});
+
+describe('UserLookupService', () => {
+ it('USERLOOKUP-001: answers the username and email of an account', async () => {
+ const { user } = await makeUser(t.get(MikroORM), { username: 'lookup-me', email: '[email protected]' });
+ expect(await t.get(UserLookupService).usernameAndEmail(user.id)).toEqual({
+ username: 'lookup-me',
+ email: '[email protected]',
+ });
+ });
+
+ it('USERLOOKUP-002: answers undefined for an unknown id', async () => {
+ expect(await t.get(UserLookupService).usernameAndEmail(987654)).toBeUndefined();
+ });
+});