From 8baa042255277a70f0d6f0ce568e958f8f5648b9 Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 21 Sep 2026 16:03:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=A0=A1=E9=AA=8C=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/package.json | 1 + apps/api/src/admin-workspace.controller.ts | 35 +++++++++++------- apps/api/src/workflows.ts | 20 ----------- apps/api/test/http.integration.test.js | 10 ++++++ packages/validation/package.json | 6 ++++ packages/validation/src/index.ts | 42 ++++++++++++++++++++++ pnpm-lock.yaml | 3 ++ 7 files changed, 85 insertions(+), 32 deletions(-) delete mode 100644 apps/api/src/workflows.ts diff --git a/apps/api/package.json b/apps/api/package.json index 618e82c..fc097ad 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -21,6 +21,7 @@ "@nestjs/core": "^11.0.11", "@nestjs/platform-fastify": "^11.0.11", "@personal-brand/config": "workspace:^", + "@personal-brand/validation": "workspace:^", "@prisma/client": "^6.2.1", "bcryptjs": "^2.4.3", "class-transformer": "^0.5.1", diff --git a/apps/api/src/admin-workspace.controller.ts b/apps/api/src/admin-workspace.controller.ts index af1d545..93fb15d 100644 --- a/apps/api/src/admin-workspace.controller.ts +++ b/apps/api/src/admin-workspace.controller.ts @@ -26,6 +26,7 @@ import { UpdateInquiryFormDto, } from "./admin.dto.js"; import { TemplateWorkspaceService } from "./template-workspace.service.js"; +import { validateFieldSnapshot, validateOfferingConfig } from "@personal-brand/validation"; @Controller("admin/workspace") class AdminWorkspaceController { @@ -62,16 +63,19 @@ class AdminWorkspaceController { ): Promise { this.requireAdmin(request); const { config, sections, media, cta, ...fields } = body; + const configValue = { + ...(config ?? {}), + ...(sections === undefined ? {} : { sections }), + ...(media === undefined ? {} : { media }), + ...(cta === undefined ? {} : { cta }), + }; + const configIssues = validateOfferingConfig(configValue); + if (configIssues.length) throw new BadRequestException("业务提供项配置无效"); return this.prisma.offering.create({ data: { ...fields, brandId: this.brandId(), - config: { - ...(config ?? {}), - ...(sections === undefined ? {} : { sections }), - ...(media === undefined ? {} : { media }), - ...(cta === undefined ? {} : { cta }), - } as Prisma.InputJsonValue, + config: configValue as Prisma.InputJsonValue, }, }); } @@ -86,6 +90,14 @@ class AdminWorkspaceController { const item = await this.prisma.offering.findFirst({ where: { id, brandId: this.brandId() } }); if (!item) throw new BadRequestException("业务提供项不存在"); const { config, sections, media, cta, ...fields } = body; + const configValue = { + ...(config ?? {}), + ...(sections === undefined ? {} : { sections }), + ...(media === undefined ? {} : { media }), + ...(cta === undefined ? {} : { cta }), + }; + if (validateOfferingConfig(configValue).length) + throw new BadRequestException("业务提供项配置无效"); return this.prisma.offering.update({ where: { id }, data: { @@ -96,12 +108,7 @@ class AdminWorkspaceController { cta === undefined ? {} : { - config: { - ...(config ?? {}), - ...(sections === undefined ? {} : { sections }), - ...(media === undefined ? {} : { media }), - ...(cta === undefined ? {} : { cta }), - } as Prisma.InputJsonValue, + config: configValue as Prisma.InputJsonValue, }), }, }); @@ -254,6 +261,8 @@ class AdminWorkspaceController { @Post("forms") createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise { this.requireAdmin(request); + if (validateFieldSnapshot(body.fieldsSnapshot).length) + throw new BadRequestException("咨询表单字段定义无效"); return this.prisma.inquiryForm.create({ data: { ...body, @@ -276,6 +285,8 @@ class AdminWorkspaceController { }); if (!item) throw new BadRequestException("咨询表单不存在"); const { fieldsSnapshot, ...fields } = body; + if (fieldsSnapshot !== undefined && validateFieldSnapshot(fieldsSnapshot).length) + throw new BadRequestException("咨询表单字段定义无效"); return this.prisma.inquiryForm.update({ where: { id }, data: { diff --git a/apps/api/src/workflows.ts b/apps/api/src/workflows.ts deleted file mode 100644 index 5735bde..0000000 --- a/apps/api/src/workflows.ts +++ /dev/null @@ -1,20 +0,0 @@ -export const BASIC_BOOKING_WORKFLOW = { - key: "booking.basic", - version: "1.0.0", - transitions: { - received: ["contacting", "cancelled"], - contacting: ["confirmed", "cancelled"], - confirmed: ["completed", "cancelled"], - completed: [], - cancelled: [], - }, -} as const; - -export type BasicBookingState = keyof typeof BASIC_BOOKING_WORKFLOW.transitions; - -export function canTransitionBasicBooking(from: string, to: string): boolean { - return ( - from === to || - BASIC_BOOKING_WORKFLOW.transitions[from as BasicBookingState]?.includes(to as never) === true - ); -} diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index 91cb61d..c2e2806 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -247,6 +247,16 @@ test("admin can manage offerings and public endpoint hides disabled offerings", payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD }, }); const token = JSON.parse(login.body).token; + const invalidConfig = await request("/admin/workspace/offerings", { + method: "POST", + headers: { authorization: `Bearer ${token}` }, + payload: { + kind: "product", + name: `无效配置-${Date.now()}`, + config: { sections: "必须是数组" }, + }, + }); + assert.equal(invalidConfig.statusCode, 400); const created = await request("/admin/workspace/offerings", { method: "POST", headers: { authorization: `Bearer ${token}` }, diff --git a/packages/validation/package.json b/packages/validation/package.json index cd22f7a..a06dd95 100644 --- a/packages/validation/package.json +++ b/packages/validation/package.json @@ -2,6 +2,12 @@ "name": "@personal-brand/validation", "private": true, "type": "module", + "exports": { + ".": { + "types": "./src/index.ts", + "import": "./src/index.ts" + } + }, "scripts": { "build": "tsc -p tsconfig.json", "lint": "eslint .", diff --git a/packages/validation/src/index.ts b/packages/validation/src/index.ts index d050cc2..e63301a 100644 --- a/packages/validation/src/index.ts +++ b/packages/validation/src/index.ts @@ -1 +1,43 @@ export const validationPackageName = "personal-brand-validation"; + +export type ValidationIssue = { path: string; message: string }; + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +export function validateFieldSnapshot(value: unknown): ValidationIssue[] { + if (!Array.isArray(value)) return [{ path: "fieldsSnapshot", message: "必须是数组" }]; + const issues: ValidationIssue[] = []; + const keys = new Set(); + for (const [index, field] of value.entries()) { + if (!isRecord(field)) { + issues.push({ path: `fieldsSnapshot.${index}`, message: "字段必须是对象" }); + continue; + } + for (const key of ["key", "type", "label"]) { + if (typeof field[key] !== "string" || !field[key]) + issues.push({ path: `fieldsSnapshot.${index}.${key}`, message: "必须是非空字符串" }); + } + if (typeof field.key === "string") { + if (keys.has(field.key)) + issues.push({ path: `fieldsSnapshot.${index}.key`, message: "字段 key 不能重复" }); + keys.add(field.key); + } + if (field.required !== undefined && typeof field.required !== "boolean") + issues.push({ path: `fieldsSnapshot.${index}.required`, message: "必须是布尔值" }); + } + return issues; +} + +export function validateOfferingConfig(value: unknown): ValidationIssue[] { + if (value === undefined || value === null) return []; + if (!isRecord(value)) return [{ path: "config", message: "必须是对象" }]; + if (value.schemaVersion !== undefined && typeof value.schemaVersion !== "string") + return [{ path: "config.schemaVersion", message: "必须是字符串" }]; + for (const key of ["sections", "media"]) { + if (value[key] !== undefined && !Array.isArray(value[key])) + return [{ path: `config.${key}`, message: "必须是数组" }]; + } + return []; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 215406d..fa38984 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -78,6 +78,9 @@ importers: '@personal-brand/config': specifier: workspace:^ version: link:../../packages/config + '@personal-brand/validation': + specifier: workspace:^ + version: link:../../packages/validation '@prisma/client': specifier: ^6.2.1 version: 6.19.3(prisma@6.19.3(typescript@5.9.3))(typescript@5.9.3)