fix: 完成工作区接口品牌隔离

This commit is contained in:
que01 committed 2026-09-23 02:19:33 +08:00
1 parent f790e7d707
commit 9ef91194df
1 file changed
+19 -20
+19 -20
View File
@@ -150,8 +150,7 @@ class AdminWorkspaceController {
} }
@Get() @Get()
async get(@Req() request: FastifyRequest): Promise<unknown> { async get(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
const brandId = this.brandId();
const [brand, installations, modules, fields, offerings, forms] = await Promise.all([ const [brand, installations, modules, fields, offerings, forms] = await Promise.all([
this.prisma.brandProfile.findUnique({ where: { id: brandId } }), this.prisma.brandProfile.findUnique({ where: { id: brandId } }),
this.prisma.brandTemplateInstallation.findMany({ where: { brandId } }), this.prisma.brandTemplateInstallation.findMany({ where: { brandId } }),
@@ -357,9 +356,9 @@ class AdminWorkspaceController {
@Param("id") id: string, @Param("id") id: string,
@Body() body: UpdateModuleDto, @Body() body: UpdateModuleDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
const item = await this.prisma.brandModule.findFirst({ const item = await this.prisma.brandModule.findFirst({
where: { id, brandId: this.brandId() }, where: { id, brandId },
}); });
if (!item) throw new BadRequestException("品牌模块不存在"); if (!item) throw new BadRequestException("品牌模块不存在");
const { config, ...fields } = body; const { config, ...fields } = body;
@@ -377,11 +376,11 @@ class AdminWorkspaceController {
@Req() request: FastifyRequest, @Req() request: FastifyRequest,
@Body() body: CreateFieldDefinitionDto, @Body() body: CreateFieldDefinitionDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
return this.prisma.brandFieldDefinition.create({ return this.prisma.brandFieldDefinition.create({
data: { data: {
...body, ...body,
brandId: this.brandId(), brandId,
source: "custom", source: "custom",
options: body.options as Prisma.InputJsonValue, options: body.options as Prisma.InputJsonValue,
visibleWhen: body.visibleWhen as Prisma.InputJsonValue, visibleWhen: body.visibleWhen as Prisma.InputJsonValue,
@@ -395,9 +394,9 @@ class AdminWorkspaceController {
@Param("id") id: string, @Param("id") id: string,
@Body() body: UpdateFieldDefinitionDto, @Body() body: UpdateFieldDefinitionDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
const item = await this.prisma.brandFieldDefinition.findFirst({ const item = await this.prisma.brandFieldDefinition.findFirst({
where: { id, brandId: this.brandId() }, where: { id, brandId },
}); });
if (!item) throw new BadRequestException("品牌字段不存在"); if (!item) throw new BadRequestException("品牌字段不存在");
const { options, visibleWhen, ...fields } = body; const { options, visibleWhen, ...fields } = body;
@@ -413,9 +412,9 @@ class AdminWorkspaceController {
@Delete("fields/:id") @Delete("fields/:id")
async deleteField(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> { async deleteField(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
const item = await this.prisma.brandFieldDefinition.findFirst({ const item = await this.prisma.brandFieldDefinition.findFirst({
where: { id, brandId: this.brandId(), source: "custom" }, where: { id, brandId, source: "custom" },
}); });
if (!item) throw new BadRequestException("自定义字段不存在或不可删除"); if (!item) throw new BadRequestException("自定义字段不存在或不可删除");
await this.prisma.brandFieldDefinition.delete({ where: { id } }); await this.prisma.brandFieldDefinition.delete({ where: { id } });
@@ -424,18 +423,18 @@ class AdminWorkspaceController {
@Get("forms") @Get("forms")
listForms(@Req() request: FastifyRequest): Promise<unknown> { listForms(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
return this.prisma.inquiryForm.findMany({ return this.prisma.inquiryForm.findMany({
where: { brandId: this.brandId() }, where: { brandId },
orderBy: { updatedAt: "desc" }, orderBy: { updatedAt: "desc" },
}); });
} }
@Get("submissions") @Get("submissions")
listSubmissions(@Req() request: FastifyRequest): Promise<unknown> { listSubmissions(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
return this.prisma.inquirySubmission.findMany({ return this.prisma.inquirySubmission.findMany({
where: { brandId: this.brandId() }, where: { brandId },
include: { include: {
inquiryForm: { select: { name: true } }, inquiryForm: { select: { name: true } },
offering: { select: { id: true, name: true } }, offering: { select: { id: true, name: true } },
@@ -450,9 +449,9 @@ class AdminWorkspaceController {
@Param("id") id: string, @Param("id") id: string,
@Body() body: UpdateInquirySubmissionDto, @Body() body: UpdateInquirySubmissionDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
const item = await this.prisma.inquirySubmission.findFirst({ const item = await this.prisma.inquirySubmission.findFirst({
where: { id, brandId: this.brandId() }, where: { id, brandId },
}); });
if (!item) throw new BadRequestException("咨询记录不存在"); if (!item) throw new BadRequestException("咨询记录不存在");
return this.prisma.$transaction(async (tx) => { return this.prisma.$transaction(async (tx) => {
@@ -479,13 +478,13 @@ class AdminWorkspaceController {
@Post("forms") @Post("forms")
createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise<unknown> { createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
if (validateFieldSnapshot(body.fieldsSnapshot).length) if (validateFieldSnapshot(body.fieldsSnapshot).length)
throw new BadRequestException("咨询表单字段定义无效"); throw new BadRequestException("咨询表单字段定义无效");
return this.prisma.inquiryForm.create({ return this.prisma.inquiryForm.create({
data: { data: {
...body, ...body,
brandId: this.brandId(), brandId,
version: 1, version: 1,
fieldsSnapshot: body.fieldsSnapshot as Prisma.InputJsonValue, fieldsSnapshot: body.fieldsSnapshot as Prisma.InputJsonValue,
}, },
@@ -498,9 +497,9 @@ class AdminWorkspaceController {
@Param("id") id: string, @Param("id") id: string,
@Body() body: UpdateInquiryFormDto, @Body() body: UpdateInquiryFormDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const brandId = this.requireAdmin(request).brandId;
const item = await this.prisma.inquiryForm.findFirst({ const item = await this.prisma.inquiryForm.findFirst({
where: { id, brandId: this.brandId() }, where: { id, brandId },
}); });
if (!item) throw new BadRequestException("咨询表单不存在"); if (!item) throw new BadRequestException("咨询表单不存在");
const { fieldsSnapshot, ...fields } = body; const { fieldsSnapshot, ...fields } = body;