mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +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:
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user