import { Body, Controller, Delete, ForbiddenException, Get, Param, Patch, Post, Req, UnauthorizedException, } from "@nestjs/common"; import type { FastifyRequest } from "fastify"; import { readIdentity } from "../../auth.js"; import { CreateTrustContentDto, UpdateBrandDto, UpdateBrandPresentationDto, UpdateTrustContentDto, } from "../../admin.dto.js"; import { AdminAccessService } from "../../admin-access.service.js"; import { TemplateWorkspaceService } from "../../template-workspace.service.js"; import { AdminBrandConfigService } from "./admin-brand-config.service.js"; @Controller("admin/config") export class AdminConfigController { constructor( private readonly access: AdminAccessService, private readonly templateWorkspace: TemplateWorkspaceService, private readonly brandConfig: AdminBrandConfigService, ) {} private brandId(request: FastifyRequest): string { const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); if (!token) throw new UnauthorizedException("缺少管理员登录态"); const identity = readIdentity(token); if (identity.role !== "admin") throw new ForbiddenException("无管理员权限"); return identity.brandId; } private requireAdmin(request: FastifyRequest): void { const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); if (!token) throw new UnauthorizedException("缺少管理员登录态"); let identity; try { identity = readIdentity(token); } catch { throw new UnauthorizedException("管理员登录态无效或已过期"); } if (identity.role !== "admin") throw new ForbiddenException("无管理员权限"); } @Get("brand") getBrand(@Req() request: FastifyRequest): Promise { this.requireAdmin(request); return this.brandConfig.getBrand(this.brandId(request)); } @Patch("brand") async updateBrand( @Req() request: FastifyRequest, @Body() body: UpdateBrandDto, ): Promise { this.requireAdmin(request); return this.brandConfig.updateBrand(this.brandId(request), body); } @Patch("presentation") async updatePresentation( @Req() request: FastifyRequest, @Body() body: UpdateBrandPresentationDto, ): Promise { this.requireAdmin(request); const brandId = this.brandId(request); return this.brandConfig.updatePresentation(brandId, body); } @Get("trust-content") listTrustContent(@Req() request: FastifyRequest): Promise { this.requireAdmin(request); return this.templateWorkspace.listTrustContent(this.brandId(request)); } @Post("trust-content") createTrustContent( @Req() request: FastifyRequest, @Body() body: CreateTrustContentDto, ): Promise { this.requireAdmin(request); const brandId = this.brandId(request); return this.templateWorkspace.createTrustContent(brandId, body); } @Patch("trust-content/:id") updateTrustContent( @Req() request: FastifyRequest, @Param("id") id: string, @Body() body: UpdateTrustContentDto, ): Promise { this.requireAdmin(request); return this.templateWorkspace.updateTrustContent(this.brandId(request), id, body); } @Delete("trust-content/:id") async deleteTrustContent( @Req() request: FastifyRequest, @Param("id") id: string, ): Promise { this.requireAdmin(request); return this.templateWorkspace.deleteTrustContent(this.brandId(request), id); } }