Эх сурвалжийг харах

新增团队ID字段,更新推广链接控制器以支持用户角色权限验证,并在查询中根据团队ID过滤结果。

wuyi 4 сар өмнө
parent
commit
fde85f34f7

+ 20 - 3
src/controllers/promotion-link.controller.ts

@@ -7,12 +7,16 @@ import {
   PromotionLinkParams
 } from '../dto/promotion-link.dto'
 import { LinkType } from '../entities/promotion-link.entity'
+import { UserRole } from '../entities/user.entity'
+import { TeamService } from '../services/team.service'
 
 export class PromotionLinkController {
   private promotionLinkService: PromotionLinkService
+  private teamService: TeamService
 
   constructor(app: FastifyInstance) {
     this.promotionLinkService = new PromotionLinkService(app)
+    this.teamService = new TeamService(app)
   }
 
   async create(request: FastifyRequest<{ Body: CreatePromotionLinkBody }>, reply: FastifyReply) {
@@ -36,6 +40,16 @@ export class PromotionLinkController {
 
   async findAll(request: FastifyRequest<{ Querystring: ListPromotionLinkQuery }>, 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) {
+        const team = await this.teamService.findByUserId(user.id)
+        request.query.teamId = team.id
+      }
       const result = await this.promotionLinkService.findAll(request.query)
       return reply.send(result)
     } catch (error) {
@@ -43,11 +57,14 @@ export class PromotionLinkController {
     }
   }
 
-  async update(request: FastifyRequest<{ Params: PromotionLinkParams; Body: UpdatePromotionLinkBody }>, reply: FastifyReply) {
+  async update(
+    request: FastifyRequest<{ Params: PromotionLinkParams; Body: UpdatePromotionLinkBody }>,
+    reply: FastifyReply
+  ) {
     try {
       const { id } = request.params
       const updateData = { ...request.body, id }
-      
+
       try {
         await this.promotionLinkService.findById(id)
       } catch (error) {
@@ -64,7 +81,7 @@ export class PromotionLinkController {
   async delete(request: FastifyRequest<{ Params: PromotionLinkParams }>, reply: FastifyReply) {
     try {
       const { id } = request.params
-      
+
       try {
         await this.promotionLinkService.findById(id)
       } catch (error) {

+ 3 - 0
src/dto/promotion-link.dto.ts

@@ -3,6 +3,7 @@ import { LinkType } from '../entities/promotion-link.entity'
 import { Pagination } from './common.dto'
 
 export interface CreatePromotionLinkBody {
+  teamId: number
   name: string
   image: string
   link: string
@@ -11,6 +12,7 @@ export interface CreatePromotionLinkBody {
 
 export interface UpdatePromotionLinkBody {
   id: number
+  teamId?: number
   name?: string
   image?: string
   link?: string
@@ -20,6 +22,7 @@ export interface UpdatePromotionLinkBody {
 export interface ListPromotionLinkQuery extends Pagination {
   name?: string
   type?: LinkType
+  teamId?: number
 }
 
 export interface PromotionLinkParams {

+ 3 - 0
src/entities/promotion-link.entity.ts

@@ -11,6 +11,9 @@ export class PromotionLink {
   @PrimaryGeneratedColumn()
   id: number
 
+  @Column()
+  teamId: number
+
   @Column()
   name: string
 

+ 8 - 3
src/routes/promotion-link.routes.ts

@@ -1,8 +1,13 @@
 import { FastifyInstance } from 'fastify'
 import { PromotionLinkController } from '../controllers/promotion-link.controller'
-import { authenticate, hasRole } from '../middlewares/auth.middleware'
+import { authenticate, hasAnyRole, hasRole } from '../middlewares/auth.middleware'
 import { UserRole } from '../entities/user.entity'
-import { CreatePromotionLinkBody, UpdatePromotionLinkBody, ListPromotionLinkQuery, PromotionLinkParams } from '../dto/promotion-link.dto'
+import {
+  CreatePromotionLinkBody,
+  UpdatePromotionLinkBody,
+  ListPromotionLinkQuery,
+  PromotionLinkParams
+} from '../dto/promotion-link.dto'
 import { LinkType } from '../entities/promotion-link.entity'
 
 export default async function promotionLinkRoutes(fastify: FastifyInstance) {
@@ -18,7 +23,7 @@ export default async function promotionLinkRoutes(fastify: FastifyInstance) {
   // 获取推广链接列表
   fastify.get<{ Querystring: ListPromotionLinkQuery }>(
     '/',
-    { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
+    { onRequest: [authenticate, hasAnyRole(UserRole.ADMIN, UserRole.TEAM)] },
     promotionLinkController.findAll.bind(promotionLinkController)
   )
 

+ 5 - 1
src/services/promotion-link.service.ts

@@ -72,7 +72,7 @@ export class PromotionLinkService {
   }
 
   async findAll(query: ListPromotionLinkQuery): Promise<PaginationResponse<PromotionLink>> {
-    const { page, size, name, type } = query
+    const { page, size, name, type, teamId } = query
     
     const where: any = {}
     
@@ -84,6 +84,10 @@ export class PromotionLinkService {
       where.type = type
     }
 
+    if (teamId) {
+      where.teamId = teamId
+    }
+
     const [links, total] = await this.promotionLinkRepository.findAndCount({
       where,
       skip: (Number(page) || 0) * (Number(size) || 20),