浏览代码

更新推广链接控制器,新增用户登录验证逻辑,支持团队角色用户在创建推广链接时自动关联团队ID,同时调整DTO以使teamId为可选字段,更新路由以支持团队和管理员角色的访问权限。

wuyi 3 月之前
父节点
当前提交
7d0a428987
共有 3 个文件被更改,包括 10 次插入2 次删除
  1. 8 0
      src/controllers/promotion-link.controller.ts
  2. 1 1
      src/dto/promotion-link.dto.ts
  3. 1 1
      src/routes/promotion-link.routes.ts

+ 8 - 0
src/controllers/promotion-link.controller.ts

@@ -24,6 +24,14 @@ export class PromotionLinkController {
 
   async create(request: FastifyRequest<{ Body: CreatePromotionLinkBody }>, reply: FastifyReply) {
     try {
+      const user = request.user
+      if (!user) {
+        return reply.code(403).send({ message: '用户未登录' })
+      }
+      if (user.role === UserRole.TEAM) {
+        const team = await this.teamService.findByUserId(user.id)
+        request.body.teamId = team.id
+      }
       const promotionLink = await this.promotionLinkService.create(request.body)
       return reply.code(201).send(promotionLink)
     } catch (error) {

+ 1 - 1
src/dto/promotion-link.dto.ts

@@ -3,7 +3,7 @@ import { LinkType } from '../entities/promotion-link.entity'
 import { Pagination } from './common.dto'
 
 export interface CreatePromotionLinkBody {
-  teamId: number
+  teamId?: number
   name: string
   image: string
   link: string

+ 1 - 1
src/routes/promotion-link.routes.ts

@@ -16,7 +16,7 @@ export default async function promotionLinkRoutes(fastify: FastifyInstance) {
   // 创建推广链接
   fastify.post<{ Body: CreatePromotionLinkBody }>(
     '/',
-    { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
+    { onRequest: [authenticate, hasAnyRole(UserRole.ADMIN, UserRole.TEAM)] },
     promotionLinkController.create.bind(promotionLinkController)
   )