Explorar el Código

更新系统配置控制器,移除不必要的注释,新增获取用户团队配置的功能,并在服务层实现相应逻辑。同时,调整实体中的字段约束以简化配置管理。

wuyi hace 3 meses
padre
commit
33e5bf66d6

+ 10 - 2
src/controllers/sys-config.controller.ts

@@ -116,7 +116,6 @@ export class SysConfigController {
       const userId = request.user.id
       const userRole = request.user.role
       
-      // 管理员查询:teamId为可选,不传则查询所有团队
       const config = await this.sysConfigService.getTeamConfig(name, userId, userRole, teamId ? Number(teamId) : undefined)
       return reply.send(config)
     } catch (error) {
@@ -130,11 +129,20 @@ export class SysConfigController {
       const userId = request.user.id
       const userRole = request.user.role
       
-      // 管理员查询:teamId为可选,不传则查询所有团队
       const configs = await this.sysConfigService.listTeamConfigs(query, userId, userRole, teamId ? Number(teamId) : undefined)
       return reply.send(configs)
     } catch (error) {
       return reply.code(500).send(error)
     }
   }
+
+  async getUserTeamConfigs(request: FastifyRequest, reply: FastifyReply) {
+    try {
+      const userId = request.user.id
+      const configs = await this.sysConfigService.getUserTeamConfigs(userId)
+      return reply.send(configs)
+    } catch (error) {
+      return reply.code(500).send(error)
+    }
+  }
 } 

+ 1 - 1
src/entities/sys-config.entity.ts

@@ -19,7 +19,7 @@ export class SysConfig {
   @Column({ default: 0 })
   teamId: number
 
-  @Column({ length: 100, unique: true })
+  @Column({ length: 100 })
   name: string
 
   @Column({ type: 'text' })

+ 15 - 2
src/routes/sys-config.routes.ts

@@ -2,7 +2,14 @@ import { FastifyInstance } from 'fastify'
 import { authenticate, hasRole, hasAnyRole } from '../middlewares/auth.middleware'
 import { UserRole } from '../entities/user.entity'
 import { SysConfigController } from '../controllers/sys-config.controller'
-import { CreateSysConfigBody, UpdateSysConfigBody, ListSysConfigQuery, CreateTeamConfigBody, UpdateTeamConfigBody, ListTeamConfigQuery } from '../dto/sys-config.dto'
+import {
+  CreateSysConfigBody,
+  UpdateSysConfigBody,
+  ListSysConfigQuery,
+  CreateTeamConfigBody,
+  UpdateTeamConfigBody,
+  ListTeamConfigQuery
+} from '../dto/sys-config.dto'
 
 export default async function sysConfigRoutes(fastify: FastifyInstance) {
   const sysConfigController = new SysConfigController(fastify)
@@ -35,7 +42,7 @@ export default async function sysConfigRoutes(fastify: FastifyInstance) {
 
   fastify.get('/types/all', { onRequest: [authenticate] }, sysConfigController.getConfigTypes.bind(sysConfigController))
 
-  // 团队配置相关路由
+  // 团队配置
   fastify.post<{ Body: CreateTeamConfigBody & { teamId?: number } }>(
     '/team',
     { onRequest: [hasAnyRole(UserRole.ADMIN, UserRole.TEAM)] },
@@ -65,4 +72,10 @@ export default async function sysConfigRoutes(fastify: FastifyInstance) {
     { onRequest: [hasAnyRole(UserRole.ADMIN, UserRole.TEAM, UserRole.PROMOTER)] },
     sysConfigController.listTeamConfigs.bind(sysConfigController)
   )
+
+  fastify.get(
+    '/team/user',
+    { onRequest: [hasRole(UserRole.USER)] },
+    sysConfigController.getUserTeamConfigs.bind(sysConfigController)
+  )
 }

+ 19 - 2
src/services/sys-config.service.ts

@@ -245,7 +245,6 @@ export class SysConfigService {
 
   async getTeamConfig(name: string, userId: number, userRole: UserRole, adminTeamId?: number) {
     if (userRole === UserRole.ADMIN) {
-      // 管理员查询:如果指定了teamId则查询特定团队,否则查询所有团队
       return await this.getSysConfig(name, adminTeamId)
     } else {
       const teamId = await this.getUserTeamId(userId, userRole)
@@ -255,7 +254,6 @@ export class SysConfigService {
 
   async listTeamConfigs(query: ListTeamConfigQuery, userId: number, userRole: UserRole, adminTeamId?: number) {
     if (userRole === UserRole.ADMIN) {
-      // 管理员查询:如果指定了teamId则查询特定团队,否则查询所有团队
       const { page = 0, size = 20, name, type } = query
       return await this.list(page, size, name, type, adminTeamId)
     } else {
@@ -264,4 +262,23 @@ export class SysConfigService {
       return await this.list(page, size, name, type, teamId)
     }
   }
+
+  async getUserTeamConfigs(userId: number) {
+    let teamId = 0
+    const user = await this.userRepository.findOne({ where: { id: userId } })
+    if (user) {
+      const team = await this.teamRepository.findOne({ where: { userId: user.parentId } })
+      if (team) {
+        teamId = team.id
+      }
+    }
+
+    const configs = await this.sysConfigRepository.find({
+      where: { teamId },
+      select: ['name', 'value'],
+      order: { id: 'ASC' }
+    })
+
+    return configs
+  }
 }