feat: 增加品牌工作空间模块与字段配置接口

This commit is contained in:
que01 committed 2026-09-20 09:27:28 +08:00
1 parent 02aa698fb9
commit 6e1aca3e27
3 files changed
+162

No files matched your search

+34
View File
@@ -128,3 +128,37 @@ export class UpdateOfferingDto {
@IsOptional() media?: Array<{ url: string; alt?: string; type?: string }>;
@IsOptional() cta?: { type: string; label: string; config?: Record<string, unknown> };
}
export class UpdateModuleDto {
@IsOptional() @IsString() title?: string;
@IsOptional() @IsBoolean() isEnabled?: boolean;
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
@IsOptional() config?: Record<string, unknown>;
}
export class CreateFieldDefinitionDto {
@IsString() scope!: string;
@IsString() key!: string;
@IsString() label!: string;
@IsIn(["text", "textarea", "number", "date", "select", "multi-select", "image", "url", "phone"])
type!: string;
@IsOptional() @IsBoolean() required?: boolean;
@IsOptional() @IsBoolean() isVisible?: boolean;
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
@IsOptional() options?: unknown;
@IsOptional() visibleWhen?: unknown;
}
export class UpdateFieldDefinitionDto {
@IsOptional() @IsString() scope?: string;
@IsOptional() @IsString() key?: string;
@IsOptional() @IsString() label?: string;
@IsOptional()
@IsIn(["text", "textarea", "number", "date", "select", "multi-select", "image", "url", "phone"])
type?: string;
@IsOptional() @IsBoolean() required?: boolean;
@IsOptional() @IsBoolean() isVisible?: boolean;
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
@IsOptional() options?: unknown;
@IsOptional() visibleWhen?: unknown;
}
+74
View File
@@ -35,6 +35,9 @@ import {
InstallTemplateDto,
CreateOfferingDto,
UpdateOfferingDto,
UpdateModuleDto,
CreateFieldDefinitionDto,
UpdateFieldDefinitionDto,
} from "./admin.dto.js";
import { AdminAuthService } from "./admin-auth.service.js";
import { AdminLoginDto } from "./admin-auth.dto.js";
@@ -319,6 +322,77 @@ export class AdminWorkspaceController {
return { installation, template };
});
}
@Patch("modules/:id")
async updateModule(
@Req() request: FastifyRequest,
@Param("id") id: string,
@Body() body: UpdateModuleDto,
): Promise<unknown> {
this.requireAdmin(request);
const item = await this.prisma.brandModule.findFirst({
where: { id, brandId: this.brandId() },
});
if (!item) throw new BadRequestException("品牌模块不存在");
const { config, ...fields } = body;
return this.prisma.brandModule.update({
where: { id },
data: {
...fields,
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
},
});
}
@Post("fields")
createField(
@Req() request: FastifyRequest,
@Body() body: CreateFieldDefinitionDto,
): Promise<unknown> {
this.requireAdmin(request);
return this.prisma.brandFieldDefinition.create({
data: {
...body,
brandId: this.brandId(),
source: "custom",
options: body.options as Prisma.InputJsonValue,
visibleWhen: body.visibleWhen as Prisma.InputJsonValue,
},
});
}
@Patch("fields/:id")
async updateField(
@Req() request: FastifyRequest,
@Param("id") id: string,
@Body() body: UpdateFieldDefinitionDto,
): Promise<unknown> {
this.requireAdmin(request);
const item = await this.prisma.brandFieldDefinition.findFirst({
where: { id, brandId: this.brandId() },
});
if (!item) throw new BadRequestException("品牌字段不存在");
const { options, visibleWhen, ...fields } = body;
return this.prisma.brandFieldDefinition.update({
where: { id },
data: {
...fields,
...(options === undefined ? {} : { options: options as Prisma.InputJsonValue }),
...(visibleWhen === undefined ? {} : { visibleWhen: visibleWhen as Prisma.InputJsonValue }),
},
});
}
@Delete("fields/:id")
async deleteField(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
this.requireAdmin(request);
const item = await this.prisma.brandFieldDefinition.findFirst({
where: { id, brandId: this.brandId(), source: "custom" },
});
if (!item) throw new BadRequestException("自定义字段不存在或不可删除");
await this.prisma.brandFieldDefinition.delete({ where: { id } });
return { deleted: true };
}
}
@Controller("customer/session")
+54
View File
@@ -78,6 +78,60 @@ test("admin can install a template and public workspace exposes enabled configur
}
});
test("admin can customize workspace modules and fields with brand isolation", async () => {
const login = await request("/admin/session/login", {
method: "POST",
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
});
const token = JSON.parse(login.body).token;
const install = await request("/admin/workspace/install", {
method: "POST",
headers: { authorization: `Bearer ${token}` },
payload: { templateKey: "food" },
});
assert.equal(install.statusCode, 201);
const workspace = await request("/admin/workspace", {
headers: { authorization: `Bearer ${token}` },
});
const moduleId = JSON.parse(workspace.body).modules[0].id;
const updatedModule = await request(`/admin/workspace/modules/${moduleId}`, {
method: "PATCH",
headers: { authorization: `Bearer ${token}` },
payload: { title: "我的品牌介绍", isEnabled: false },
});
assert.equal(updatedModule.statusCode, 200);
const createdField = await request("/admin/workspace/fields", {
method: "POST",
headers: { authorization: `Bearer ${token}` },
payload: {
scope: "profile",
key: `region-${Date.now()}`,
label: "服务区域",
type: "text",
required: false,
},
});
assert.equal(createdField.statusCode, 201);
const fieldId = JSON.parse(createdField.body).id;
try {
const updatedField = await request(`/admin/workspace/fields/${fieldId}`, {
method: "PATCH",
headers: { authorization: `Bearer ${token}` },
payload: { label: "主要服务区域", visibleWhen: { mode: "all", rules: [] } },
});
assert.equal(updatedField.statusCode, 200);
const deletedField = await request(`/admin/workspace/fields/${fieldId}`, {
method: "DELETE",
headers: { authorization: `Bearer ${token}` },
});
assert.equal(deletedField.statusCode, 200);
} finally {
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
await prisma.brandModule.deleteMany({ where: { brandId } });
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
}
});
test("admin can manage offerings and public endpoint hides disabled offerings", async () => {
const login = await request("/admin/session/login", {
method: "POST",