refactor: 接入品牌配置管理员上下文

This commit is contained in:
que01 committed 2026-09-23 02:21:21 +08:00
1 parent 9ef91194df
commit 6835a2b0fd
1 file changed
+9 -4
+9 -4
View File
@@ -686,8 +686,13 @@ export class AdminBookingController {
@Controller("admin/config")
export class AdminConfigController {
constructor(private readonly prisma: PrismaService) {}
private brandId(): string {
return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
private brandId(request?: FastifyRequest): string {
if (!request) return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
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, "");
@@ -703,7 +708,7 @@ export class AdminConfigController {
@Get("brand")
getBrand(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request);
return this.prisma.brandProfile.findUnique({ where: { id: this.brandId() } });
return this.prisma.brandProfile.findUnique({ where: { id: this.brandId(request) } });
}
@Patch("brand")
async updateBrand(
@@ -713,7 +718,7 @@ export class AdminConfigController {
this.requireAdmin(request);
const { config, ...fields } = body;
const current = await this.prisma.brandProfile.findUnique({
where: { id: this.brandId() },
where: { id: this.brandId(request) },
select: { config: true },
});
const currentConfig =