|
|
@@ -1,12 +1,7 @@
|
|
|
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
|
|
|
import { TeamService } from '../services/team.service'
|
|
|
-import {
|
|
|
- CreateTeamBody,
|
|
|
- UpdateTeamBody,
|
|
|
- ListTeamQuery,
|
|
|
- TeamParams,
|
|
|
- UpdateRevenueBody
|
|
|
-} from '../dto/team.dto'
|
|
|
+import { CreateTeamBody, UpdateTeamBody, ListTeamQuery, TeamParams, UpdateRevenueBody } from '../dto/team.dto'
|
|
|
+import { UserRole } from '../entities/user.entity'
|
|
|
|
|
|
export class TeamController {
|
|
|
private teamService: TeamService
|
|
|
@@ -36,6 +31,15 @@ export class TeamController {
|
|
|
|
|
|
async findAll(request: FastifyRequest<{ Querystring: ListTeamQuery }>, reply: FastifyReply) {
|
|
|
try {
|
|
|
+ const user = request.user
|
|
|
+ if (!user) {
|
|
|
+ return reply.code(403).send({ message: '用户未登录' })
|
|
|
+ }
|
|
|
+ if (user.role === UserRole.USER) {
|
|
|
+ return reply.code(403).send({ message: '用户无权限' })
|
|
|
+ } else if (user.role === UserRole.TEAM) {
|
|
|
+ request.query.userId = user.id
|
|
|
+ }
|
|
|
const result = await this.teamService.findAll(request.query)
|
|
|
return reply.send(result)
|
|
|
} catch (error) {
|
|
|
@@ -47,7 +51,7 @@ export class TeamController {
|
|
|
try {
|
|
|
const { id } = request.params
|
|
|
const updateData = { ...request.body, id }
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
await this.teamService.findById(id)
|
|
|
} catch (error) {
|
|
|
@@ -64,7 +68,7 @@ export class TeamController {
|
|
|
async delete(request: FastifyRequest<{ Params: TeamParams }>, reply: FastifyReply) {
|
|
|
try {
|
|
|
const { id } = request.params
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
await this.teamService.findById(id)
|
|
|
} catch (error) {
|
|
|
@@ -81,7 +85,7 @@ export class TeamController {
|
|
|
async updateRevenue(request: FastifyRequest<{ Body: UpdateRevenueBody }>, reply: FastifyReply) {
|
|
|
try {
|
|
|
const { id, amount, type } = request.body
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
await this.teamService.findById(id)
|
|
|
} catch (error) {
|
|
|
@@ -121,4 +125,20 @@ export class TeamController {
|
|
|
return reply.code(500).send({ message: '获取团队失败', error })
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ async findByUserId(request: FastifyRequest, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const user = request.user
|
|
|
+ if (!user) {
|
|
|
+ return reply.code(403).send({ message: '用户未登录' })
|
|
|
+ }
|
|
|
+ if (user.role === UserRole.USER) {
|
|
|
+ return reply.code(403).send({ message: '用户无权限' })
|
|
|
+ }
|
|
|
+ const team = await this.teamService.findByUserId(user.id)
|
|
|
+ return reply.send(team)
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(404).send({ message: '团队不存在' })
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|