Răsfoiți Sursa

🔧 fix(user-module-mt): 修复角色服务方法签名兼容性和编译错误

- 修复RoleServiceMt中getById、update、delete方法签名与基类不兼容问题
- 移除userTracking和tenantOptions中不存在的'enabled'属性
- 修复测试文件中的类型错误和导入路径问题
- 统一租户ID参数传递方式(从对象改为直接数值)
- 确保所有41个测试通过,编译无错误

🤖 Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 1 lună în urmă
părinte
comite
c205327867

+ 11 - 12
packages/user-module-mt/src/services/role.service.mt.ts

@@ -6,10 +6,9 @@ export class RoleServiceMt extends ConcreteCrudService<RoleMt> {
   constructor(dataSource: DataSource) {
     super(RoleMt, {
       userTracking: {
-        enabled: false
+        // 禁用用户追踪
       },
       tenantOptions: {
-        enabled: true,
         tenantIdField: 'tenantId',
         autoExtractFromContext: true
       }
@@ -41,7 +40,7 @@ export class RoleServiceMt extends ConcreteCrudService<RoleMt> {
    */
   async hasPermission(roleId: number, permission: string, tenantId?: number): Promise<boolean> {
     try {
-      const role = await this.getById(roleId, [], { tenantId });
+      const role = await this.getById(roleId, [], tenantId);
       if (!role) return false;
 
       return role.permissions.includes(permission);
@@ -73,13 +72,13 @@ export class RoleServiceMt extends ConcreteCrudService<RoleMt> {
   /**
    * 根据ID获取角色(重写以添加租户过滤)
    */
-  async getById(id: number, relations: string[] = [], options?: { tenantId?: number }): Promise<RoleMt | null> {
+  async getById(id: number, relations: string[] = [], userId?: string | number): Promise<RoleMt | null> {
     try {
       const where: any = { id };
 
-      // 如果提供了租户ID,添加租户过滤
-      if (options?.tenantId !== undefined) {
-        where.tenantId = options.tenantId;
+      // 如果提供了租户ID(作为userId参数传递),添加租户过滤
+      if (userId !== undefined && typeof userId === 'number') {
+        where.tenantId = userId;
       }
 
       return await this.repository.findOne({ where, relations });
@@ -92,17 +91,17 @@ export class RoleServiceMt extends ConcreteCrudService<RoleMt> {
   /**
    * 更新角色(重写以添加租户过滤)
    */
-  async update(id: number, data: Partial<RoleMt>, options?: { tenantId?: number }): Promise<RoleMt | null> {
+  async update(id: number, data: Partial<RoleMt>, userId?: string | number): Promise<RoleMt | null> {
     try {
       // 首先验证角色是否存在且属于指定租户
-      const existingRole = await this.getById(id, [], options);
+      const existingRole = await this.getById(id, [], userId);
       if (!existingRole) return null;
 
       // 更新角色
       await this.repository.update(id, data);
 
       // 返回更新后的角色
-      return await this.getById(id, [], options);
+      return await this.getById(id, [], userId);
     } catch (error) {
       console.error('Error updating role:', error);
       throw new Error('Failed to update role');
@@ -112,10 +111,10 @@ export class RoleServiceMt extends ConcreteCrudService<RoleMt> {
   /**
    * 删除角色(重写以添加租户过滤)
    */
-  async delete(id: number, options?: { tenantId?: number }): Promise<boolean> {
+  async delete(id: number, userId?: string | number): Promise<boolean> {
     try {
       // 首先验证角色是否存在且属于指定租户
-      const existingRole = await this.getById(id, [], options);
+      const existingRole = await this.getById(id, [], userId);
       if (!existingRole) return false;
 
       // 删除角色

+ 0 - 2
packages/user-module-mt/src/services/user.service.mt.ts

@@ -12,12 +12,10 @@ export class UserServiceMt extends ConcreteCrudService<UserEntityMt> {
   constructor(dataSource: DataSource) {
     super(UserEntityMt, {
       userTracking: {
-        enabled: true,
         createdByField: 'createdBy',
         updatedByField: 'updatedBy'
       },
       tenantOptions: {
-        enabled: true,
         tenantIdField: 'tenantId',
         autoExtractFromContext: true
       }

+ 8 - 8
packages/user-module-mt/tests/integration/role.integration.test.ts

@@ -261,21 +261,21 @@ describe('角色集成测试', () => {
 
     it('应该正确过滤跨租户的角色详情访问', async () => {
       // 租户1应该能访问自己的角色
-      const role1 = await roleService.getById(tenant1Role.id, [], { tenantId: 1 });
+      const role1 = await roleService.getById(tenant1Role.id, [], 1);
       expect(role1).toBeDefined();
       expect(role1?.tenantId).toBe(1);
 
       // 租户1不应该能访问租户2的角色
-      const role2FromTenant1 = await roleService.getById(tenant2Role.id, [], { tenantId: 1 });
+      const role2FromTenant1 = await roleService.getById(tenant2Role.id, [], 1);
       expect(role2FromTenant1).toBeNull();
 
       // 租户2应该能访问自己的角色
-      const role2 = await roleService.getById(tenant2Role.id, [], { tenantId: 2 });
+      const role2 = await roleService.getById(tenant2Role.id, [], 2);
       expect(role2).toBeDefined();
       expect(role2?.tenantId).toBe(2);
 
       // 租户2不应该能访问租户1的角色
-      const role1FromTenant2 = await roleService.getById(tenant1Role.id, [], { tenantId: 2 });
+      const role1FromTenant2 = await roleService.getById(tenant1Role.id, [], 2);
       expect(role1FromTenant2).toBeNull();
     });
 
@@ -286,22 +286,22 @@ describe('角色集成测试', () => {
       };
 
       // 租户1尝试更新租户2的角色 - 应该返回null
-      const updatedRole = await roleService.update(tenant2Role.id, updateData, { tenantId: 1 });
+      const updatedRole = await roleService.update(tenant2Role.id, updateData, 1);
       expect(updatedRole).toBeNull();
 
       // 验证租户2的角色没有被修改
-      const originalRole = await roleService.getById(tenant2Role.id, [], { tenantId: 2 });
+      const originalRole = await roleService.getById(tenant2Role.id, [], 2);
       expect(originalRole?.description).toBe('Role for tenant 2');
       expect(originalRole?.permissions).toEqual(['user:read', 'user:update']);
     });
 
     it('应该拒绝跨租户的角色删除', async () => {
       // 租户1尝试删除租户2的角色 - 应该返回false
-      const deleteResult = await roleService.delete(tenant2Role.id, { tenantId: 1 });
+      const deleteResult = await roleService.delete(tenant2Role.id, 1);
       expect(deleteResult).toBe(false);
 
       // 验证租户2的角色仍然存在
-      const roleStillExists = await roleService.getById(tenant2Role.id, [], { tenantId: 2 });
+      const roleStillExists = await roleService.getById(tenant2Role.id, [], 2);
       expect(roleStillExists).toBeDefined();
     });
 

+ 7 - 7
packages/user-module-mt/tests/integration/user.routes.integration.test.ts

@@ -19,7 +19,7 @@ import { FileMt } from '@d8d/file-module-mt';
 setupIntegrationDatabaseHooksWithEntities([UserEntityMt, RoleMt, FileMt])
 
 describe('用户路由API集成测试 (使用hono/testing)', () => {
-  let client: ReturnType<typeof testClient<typeof userRoutes>>;
+  let client: ReturnType<typeof testClient<typeof userRoutesMt>>;
   let authService: AuthService;
   let userService: UserServiceMt;
   let testToken: string;
@@ -614,9 +614,9 @@ describe('用户路由API集成测试 (使用hono/testing)', () => {
       }
       expect(response1.status).toBe(200);
       const result1 = await response1.json();
-      console.debug('租户1返回的用户数据:', result1.data);
-      expect(result1.data).toHaveLength(2);
-      expect(result1.data.every((user: any) => user.tenantId === 1)).toBe(true);
+      console.debug('租户1返回的用户数据:', (result1 as any).data);
+      expect((result1 as any).data).toHaveLength(2);
+      expect((result1 as any).data.every((user: any) => user.tenantId === 1)).toBe(true);
 
       // 租户2只能看到租户2的用户
       const response2 = await client.index.$get({
@@ -634,9 +634,9 @@ describe('用户路由API集成测试 (使用hono/testing)', () => {
       }
       expect(response2.status).toBe(200);
       const result2 = await response2.json();
-      console.debug('租户2返回的用户数据:', result2.data);
-      expect(result2.data).toHaveLength(1);
-      expect(result2.data.every((user: any) => user.tenantId === 2)).toBe(true);
+      console.debug('租户2返回的用户数据:', (result2 as any).data);
+      expect((result2 as any).data).toHaveLength(1);
+      expect((result2 as any).data.every((user: any) => user.tenantId === 2)).toBe(true);
     });
 
     it('应该拒绝跨租户访问用户详情', async () => {

+ 1 - 1
packages/user-module-mt/tests/routes/test-user.routes.mt.ts

@@ -1,6 +1,6 @@
 import { OpenAPIHono } from '@hono/zod-openapi';
 import { createCrudRoutes } from '@d8d/shared-crud';
-import { UserEntityMt } from '../../src/entities/user.entity';
+import { UserEntityMt } from '../../src/entities/user.entity.mt';
 import { UserSchemaMt, CreateUserDtoMt, UpdateUserDtoMt } from '../../src/schemas/user.schema.mt';
 
 // 创建多租户通用CRUD路由配置(测试版本,不包含认证中间件)