|
|
@@ -0,0 +1,126 @@
|
|
|
+import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
|
|
|
+import { TeamDomainService } from '../services/team-domain.service'
|
|
|
+import {
|
|
|
+ CreateTeamDomainBody,
|
|
|
+ UpdateTeamDomainBody,
|
|
|
+ ListTeamDomainQuery,
|
|
|
+ TeamDomainParams
|
|
|
+} from '../dto/team-domain.dto'
|
|
|
+import { UserRole } from '../entities/user.entity'
|
|
|
+import { TeamMembersService } from '../services/team-members.service'
|
|
|
+import { TeamService } from '../services/team.service'
|
|
|
+
|
|
|
+export class TeamDomainController {
|
|
|
+ private teamDomainService: TeamDomainService
|
|
|
+ private teamMembersService: TeamMembersService
|
|
|
+ private teamService: TeamService
|
|
|
+
|
|
|
+ constructor(app: FastifyInstance) {
|
|
|
+ this.teamDomainService = new TeamDomainService(app)
|
|
|
+ this.teamMembersService = new TeamMembersService(app)
|
|
|
+ this.teamService = new TeamService(app)
|
|
|
+ }
|
|
|
+
|
|
|
+ async create(request: FastifyRequest<{ Body: CreateTeamDomainBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const teamDomain = await this.teamDomainService.create(request.body)
|
|
|
+ return reply.code(201).send(teamDomain)
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '创建团队域名失败' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async findById(request: FastifyRequest<{ Params: TeamDomainParams }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id } = request.params
|
|
|
+ const teamDomain = await this.teamDomainService.findById(id)
|
|
|
+ return reply.send(teamDomain)
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(404).send({ message: '团队域名不存在' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async findAll(request: FastifyRequest<{ Querystring: ListTeamDomainQuery }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const user = request.user
|
|
|
+ if (!user) {
|
|
|
+ return reply.code(403).send({ message: '用户未登录' })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (user.role === UserRole.PROMOTER) {
|
|
|
+ const teamMembers = await this.teamMembersService.findByUserId(user.id)
|
|
|
+ request.query.teamId = teamMembers.teamId
|
|
|
+ } else if (user.role === UserRole.TEAM) {
|
|
|
+ const team = await this.teamService.findByUserId(user.id)
|
|
|
+ request.query.teamId = team.id
|
|
|
+ }
|
|
|
+
|
|
|
+ const result = await this.teamDomainService.findAll(request.query)
|
|
|
+ return reply.send(result)
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error)
|
|
|
+ return reply.code(500).send({ message: '获取团队域名列表失败' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async update(request: FastifyRequest<{ Params: TeamDomainParams; Body: UpdateTeamDomainBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id } = request.params
|
|
|
+ const updateData = { ...request.body, id }
|
|
|
+ const user = request.user
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ return reply.code(403).send({ message: '用户未登录' })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (user.role !== UserRole.ADMIN) {
|
|
|
+ return reply.code(403).send({ message: '无权限' })
|
|
|
+ }
|
|
|
+
|
|
|
+ const updatedTeamDomain = await this.teamDomainService.update(updateData)
|
|
|
+ return reply.send(updatedTeamDomain)
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '更新团队域名失败' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async delete(request: FastifyRequest<{ Params: TeamDomainParams }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id } = request.params
|
|
|
+ const user = request.user
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ return reply.code(403).send({ message: '用户未登录' })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (user.role !== UserRole.ADMIN) {
|
|
|
+ return reply.code(403).send({ message: '无权限' })
|
|
|
+ }
|
|
|
+
|
|
|
+ await this.teamDomainService.delete(id)
|
|
|
+ return reply.send({ message: '团队域名已删除' })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '删除团队域名失败' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async findByTeamId(request: FastifyRequest<{ Params: { teamId: number } }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { teamId } = request.params
|
|
|
+ const user = request.user
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ return reply.code(403).send({ message: '用户未登录' })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (user.role !== UserRole.ADMIN) {
|
|
|
+ return reply.code(403).send({ message: '无权限' })
|
|
|
+ }
|
|
|
+
|
|
|
+ const teamDomains = await this.teamDomainService.findByTeamId(Number(teamId))
|
|
|
+ return reply.send(teamDomains)
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '获取团队域名失败' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|