diff --git a/apps/api/src/admin-workspace.controller.ts b/apps/api/src/admin-workspace.controller.ts index 9f036e3..2731e32 100644 --- a/apps/api/src/admin-workspace.controller.ts +++ b/apps/api/src/admin-workspace.controller.ts @@ -150,8 +150,7 @@ class AdminWorkspaceController { } @Get() async get(@Req() request: FastifyRequest): Promise { - this.requireAdmin(request); - const brandId = this.brandId(); + const brandId = this.requireAdmin(request).brandId; const [brand, installations, modules, fields, offerings, forms] = await Promise.all([ this.prisma.brandProfile.findUnique({ where: { id: brandId } }), this.prisma.brandTemplateInstallation.findMany({ where: { brandId } }), @@ -357,9 +356,9 @@ class AdminWorkspaceController { @Param("id") id: string, @Body() body: UpdateModuleDto, ): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; const item = await this.prisma.brandModule.findFirst({ - where: { id, brandId: this.brandId() }, + where: { id, brandId }, }); if (!item) throw new BadRequestException("品牌模块不存在"); const { config, ...fields } = body; @@ -377,11 +376,11 @@ class AdminWorkspaceController { @Req() request: FastifyRequest, @Body() body: CreateFieldDefinitionDto, ): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; return this.prisma.brandFieldDefinition.create({ data: { ...body, - brandId: this.brandId(), + brandId, source: "custom", options: body.options as Prisma.InputJsonValue, visibleWhen: body.visibleWhen as Prisma.InputJsonValue, @@ -395,9 +394,9 @@ class AdminWorkspaceController { @Param("id") id: string, @Body() body: UpdateFieldDefinitionDto, ): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; const item = await this.prisma.brandFieldDefinition.findFirst({ - where: { id, brandId: this.brandId() }, + where: { id, brandId }, }); if (!item) throw new BadRequestException("品牌字段不存在"); const { options, visibleWhen, ...fields } = body; @@ -413,9 +412,9 @@ class AdminWorkspaceController { @Delete("fields/:id") async deleteField(@Req() request: FastifyRequest, @Param("id") id: string): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; const item = await this.prisma.brandFieldDefinition.findFirst({ - where: { id, brandId: this.brandId(), source: "custom" }, + where: { id, brandId, source: "custom" }, }); if (!item) throw new BadRequestException("自定义字段不存在或不可删除"); await this.prisma.brandFieldDefinition.delete({ where: { id } }); @@ -424,18 +423,18 @@ class AdminWorkspaceController { @Get("forms") listForms(@Req() request: FastifyRequest): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; return this.prisma.inquiryForm.findMany({ - where: { brandId: this.brandId() }, + where: { brandId }, orderBy: { updatedAt: "desc" }, }); } @Get("submissions") listSubmissions(@Req() request: FastifyRequest): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; return this.prisma.inquirySubmission.findMany({ - where: { brandId: this.brandId() }, + where: { brandId }, include: { inquiryForm: { select: { name: true } }, offering: { select: { id: true, name: true } }, @@ -450,9 +449,9 @@ class AdminWorkspaceController { @Param("id") id: string, @Body() body: UpdateInquirySubmissionDto, ): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; const item = await this.prisma.inquirySubmission.findFirst({ - where: { id, brandId: this.brandId() }, + where: { id, brandId }, }); if (!item) throw new BadRequestException("咨询记录不存在"); return this.prisma.$transaction(async (tx) => { @@ -479,13 +478,13 @@ class AdminWorkspaceController { @Post("forms") createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; if (validateFieldSnapshot(body.fieldsSnapshot).length) throw new BadRequestException("咨询表单字段定义无效"); return this.prisma.inquiryForm.create({ data: { ...body, - brandId: this.brandId(), + brandId, version: 1, fieldsSnapshot: body.fieldsSnapshot as Prisma.InputJsonValue, }, @@ -498,9 +497,9 @@ class AdminWorkspaceController { @Param("id") id: string, @Body() body: UpdateInquiryFormDto, ): Promise { - this.requireAdmin(request); + const brandId = this.requireAdmin(request).brandId; const item = await this.prisma.inquiryForm.findFirst({ - where: { id, brandId: this.brandId() }, + where: { id, brandId }, }); if (!item) throw new BadRequestException("咨询表单不存在"); const { fieldsSnapshot, ...fields } = body;