refactor: 接入品牌配置管理员上下文
This commit is contained in:
1 parent
9ef91194df
commit
6835a2b0fd
1 file changed
+9
-4
@@ -686,8 +686,13 @@ export class AdminBookingController {
|
|||||||
@Controller("admin/config")
|
@Controller("admin/config")
|
||||||
export class AdminConfigController {
|
export class AdminConfigController {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
private brandId(): string {
|
private brandId(request?: FastifyRequest): string {
|
||||||
return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
|
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 {
|
private requireAdmin(request: FastifyRequest): void {
|
||||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||||
@@ -703,7 +708,7 @@ export class AdminConfigController {
|
|||||||
@Get("brand")
|
@Get("brand")
|
||||||
getBrand(@Req() request: FastifyRequest): Promise<unknown> {
|
getBrand(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
this.requireAdmin(request);
|
||||||
return this.prisma.brandProfile.findUnique({ where: { id: this.brandId() } });
|
return this.prisma.brandProfile.findUnique({ where: { id: this.brandId(request) } });
|
||||||
}
|
}
|
||||||
@Patch("brand")
|
@Patch("brand")
|
||||||
async updateBrand(
|
async updateBrand(
|
||||||
@@ -713,7 +718,7 @@ export class AdminConfigController {
|
|||||||
this.requireAdmin(request);
|
this.requireAdmin(request);
|
||||||
const { config, ...fields } = body;
|
const { config, ...fields } = body;
|
||||||
const current = await this.prisma.brandProfile.findUnique({
|
const current = await this.prisma.brandProfile.findUnique({
|
||||||
where: { id: this.brandId() },
|
where: { id: this.brandId(request) },
|
||||||
select: { config: true },
|
select: { config: true },
|
||||||
});
|
});
|
||||||
const currentConfig =
|
const currentConfig =
|
||||||
|
|||||||
Reference in new issue
Block a user