mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
refactor(vacay): the invite and share tools ask a kernel user lookup instead of injecting UsersRepository
VacayMcp read the caller's username and email straight from UsersRepository, a table the auth domain owns. UserLookupService in auth-core answers exactly that, so vacay neither imports AuthModule nor injects a foreign repository, and its foreignRepositories entry goes.
This commit is contained in:
@@ -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": []
|
||||
}
|
||||
|
||||
@@ -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 {}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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: 'lookup@example.com' });
|
||||
expect(await t.get(UserLookupService).usernameAndEmail(user.id)).toEqual({
|
||||
username: 'lookup-me',
|
||||
email: 'lookup@example.com',
|
||||
});
|
||||
});
|
||||
|
||||
it('USERLOOKUP-002: answers undefined for an unknown id', async () => {
|
||||
expect(await t.get(UserLookupService).usernameAndEmail(987654)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user