sys-config.controller.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { SysConfigService } from '../services/sys-config.service'
  3. import { CreateSysConfigBody, UpdateSysConfigBody, ListSysConfigQuery, CreateTeamConfigBody, UpdateTeamConfigBody, ListTeamConfigQuery, GetTeamConfigParams } from '../dto/sys-config.dto'
  4. import { UserRole } from '../entities/user.entity'
  5. export class SysConfigController {
  6. private sysConfigService: SysConfigService
  7. constructor(app: FastifyInstance) {
  8. this.sysConfigService = new SysConfigService(app)
  9. }
  10. async create(request: FastifyRequest<{ Body: CreateSysConfigBody }>, reply: FastifyReply) {
  11. try {
  12. const { name } = request.body
  13. const config = await this.sysConfigService.create(request.body)
  14. return reply.code(201).send(config)
  15. } catch (error) {
  16. return reply.code(500).send(error)
  17. }
  18. }
  19. async update(request: FastifyRequest<{ Params: { name: string }, Body: UpdateSysConfigBody }>, reply: FastifyReply) {
  20. try {
  21. const config = await this.sysConfigService.update(request.params.name, request.body, request.body.teamId)
  22. return reply.send(config)
  23. } catch (error) {
  24. return reply.code(500).send(error)
  25. }
  26. }
  27. async delete(request: FastifyRequest<{ Params: { name: string } }>, reply: FastifyReply) {
  28. try {
  29. await this.sysConfigService.delete(request.params.name)
  30. return reply.send({ success: true })
  31. } catch (error) {
  32. return reply.code(500).send(error)
  33. }
  34. }
  35. async getByName(request: FastifyRequest<{ Params: { name: string } }>, reply: FastifyReply) {
  36. try {
  37. const config = await this.sysConfigService.getSysConfig(request.params.name)
  38. return reply.send(config)
  39. } catch (error) {
  40. return reply.code(500).send(error)
  41. }
  42. }
  43. async list(request: FastifyRequest<{ Querystring: ListSysConfigQuery }>, reply: FastifyReply) {
  44. try {
  45. const { page = 0, size = 20, name, type, teamId } = request.query
  46. const configs = await this.sysConfigService.list(Number(page), Number(size), name, type, teamId)
  47. return reply.send(configs)
  48. } catch (error) {
  49. return reply.code(500).send(error)
  50. }
  51. }
  52. async getConfigTypes(request: FastifyRequest, reply: FastifyReply) {
  53. try {
  54. const types = await this.sysConfigService.getConfigTypes()
  55. return reply.send(types)
  56. } catch (error) {
  57. return reply.code(500).send(error)
  58. }
  59. }
  60. // 团队配置相关接口
  61. async createTeamConfig(request: FastifyRequest<{ Body: CreateTeamConfigBody & { teamId?: number } }>, reply: FastifyReply) {
  62. try {
  63. const { teamId, ...body } = request.body
  64. const userId = request.user.id
  65. const userRole = request.user.role
  66. const config = await this.sysConfigService.createTeamConfig(body, userId, userRole, teamId)
  67. return reply.code(201).send(config)
  68. } catch (error) {
  69. return reply.code(500).send(error)
  70. }
  71. }
  72. async updateTeamConfig(request: FastifyRequest<{ Params: { name: string }, Body: UpdateTeamConfigBody & { teamId?: number } }>, reply: FastifyReply) {
  73. try {
  74. const { name } = request.params
  75. const { teamId, ...body } = request.body
  76. const userId = request.user.id
  77. const userRole = request.user.role
  78. const config = await this.sysConfigService.updateTeamConfig(name, body, userId, userRole, teamId)
  79. return reply.send(config)
  80. } catch (error) {
  81. return reply.code(500).send(error)
  82. }
  83. }
  84. async deleteTeamConfig(request: FastifyRequest<{ Params: { name: string }, Body: { teamId?: number } }>, reply: FastifyReply) {
  85. try {
  86. const { name } = request.params
  87. const { teamId } = request.body
  88. const userId = request.user.id
  89. const userRole = request.user.role
  90. await this.sysConfigService.deleteTeamConfig(name, userId, userRole, teamId)
  91. return reply.send({ success: true })
  92. } catch (error) {
  93. return reply.code(500).send(error)
  94. }
  95. }
  96. async getTeamConfig(request: FastifyRequest<{ Params: { name: string }, Querystring: { teamId?: string } }>, reply: FastifyReply) {
  97. try {
  98. const { name } = request.params
  99. const { teamId } = request.query
  100. const userId = request.user.id
  101. const userRole = request.user.role
  102. // 管理员查询:teamId为可选,不传则查询所有团队
  103. const config = await this.sysConfigService.getTeamConfig(name, userId, userRole, teamId ? Number(teamId) : undefined)
  104. return reply.send(config)
  105. } catch (error) {
  106. return reply.code(500).send(error)
  107. }
  108. }
  109. async listTeamConfigs(request: FastifyRequest<{ Querystring: ListTeamConfigQuery & { teamId?: string } }>, reply: FastifyReply) {
  110. try {
  111. const { teamId, ...query } = request.query
  112. const userId = request.user.id
  113. const userRole = request.user.role
  114. // 管理员查询:teamId为可选,不传则查询所有团队
  115. const configs = await this.sysConfigService.listTeamConfigs(query, userId, userRole, teamId ? Number(teamId) : undefined)
  116. return reply.send(configs)
  117. } catch (error) {
  118. return reply.code(500).send(error)
  119. }
  120. }
  121. }