From aaeec1473345f459ae11a61b51c3a3110d783b57 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 14 Oct 2026 16:27:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=9D=E8=AF=81=E5=93=81=E7=89=8C?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E8=BA=AB=E4=BB=BD=E4=B8=8D=E8=A2=AB=E5=8F=B0?= =?UTF-8?q?=E8=B4=A6=E6=BC=82=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/src/admin.dto.ts | 2 - apps/api/src/brand-release.service.ts | 2 +- .../public-workspace.snapshot-assembler.ts | 2 +- apps/api/src/template-workspace.service.ts | 8 +-- apps/api/test/http.integration.test.js | 50 ++++++++++++++++++- packages/api-client/src/types/brand.ts | 2 +- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/apps/api/src/admin.dto.ts b/apps/api/src/admin.dto.ts index e8163fc..727ef48 100644 --- a/apps/api/src/admin.dto.ts +++ b/apps/api/src/admin.dto.ts @@ -13,10 +13,8 @@ import { } from "class-validator"; import { brandTemplateKeys } from "@personal-brand/config"; import type { BrandTemplateKey } from "@personal-brand/config"; - export class UpdateBrandDto { @IsOptional() @IsString() name?: string; - @IsOptional() @IsIn(brandTemplateKeys) templateKey?: BrandTemplateKey; @IsOptional() @IsString() tagline?: string; @IsOptional() @IsString() description?: string; @IsOptional() @IsString() avatarUrl?: string; diff --git a/apps/api/src/brand-release.service.ts b/apps/api/src/brand-release.service.ts index a204c20..9287515 100644 --- a/apps/api/src/brand-release.service.ts +++ b/apps/api/src/brand-release.service.ts @@ -45,7 +45,7 @@ export async function buildBrandReleaseSnapshot( database.trustContent.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }), database.capabilityInstallation.findMany({ where: { brandId } }), ]); - const templateKey = installation?.templateKey ?? brand?.templateKey ?? defaultBrandTemplateKey; + const templateKey = brand?.templateKey ?? installation?.templateKey ?? defaultBrandTemplateKey; const config = brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config) ? (brand.config as Record) diff --git a/apps/api/src/modules/public/public-workspace.snapshot-assembler.ts b/apps/api/src/modules/public/public-workspace.snapshot-assembler.ts index e741aef..7d9d1a1 100644 --- a/apps/api/src/modules/public/public-workspace.snapshot-assembler.ts +++ b/apps/api/src/modules/public/public-workspace.snapshot-assembler.ts @@ -95,9 +95,9 @@ export function assemblePublicWorkspace(input: { ) as Array>; const installationData = asRecord(releaseSnapshot.installation); const templateKey = + asString(brandData.templateKey) || asString(releaseSnapshot.templateKey) || asString(installationData.templateKey) || - asString(brandData.templateKey) || defaultBrandTemplateKey; const template = releaseSnapshot.templateDefinition ?? getBrandTemplate(templateKey); const capabilityData = asArray(releaseSnapshot.capabilities) as Array>; diff --git a/apps/api/src/template-workspace.service.ts b/apps/api/src/template-workspace.service.ts index 4fb2839..996c843 100644 --- a/apps/api/src/template-workspace.service.ts +++ b/apps/api/src/template-workspace.service.ts @@ -91,12 +91,6 @@ export class TemplateWorkspaceService { select: { config: true, templateKey: true }, }), ]); - if (existing) - throw new BadRequestException( - existing.templateKey === templateKey - ? "该模板已经安装" - : "品牌已经安装主模板,更换行业模板不在本版本范围内", - ); // 一个品牌只使用一个行业模板(BrandProfile.templateKey 即身份)。安装只允许补建当前模板的版本台账, // 不得换成别的行业模板,否则会出现「身份是 A、台账是 B」的漂移,并把发布快照接到不适配的视觉模板上。 const currentTemplate = brand?.templateKey ? getBrandTemplate(brand.templateKey) : undefined; @@ -104,6 +98,8 @@ export class TemplateWorkspaceService { throw new BadRequestException( `当前品牌已在使用「${currentTemplate.name}」行业模板,更换行业模板不在本版本范围内`, ); + if (existing && existing.templateKey === templateKey) + throw new BadRequestException("该模板已经安装"); const brandConfig = brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config) ? (brand.config as Record) diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index 704367d..12fca4f 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -1697,6 +1697,53 @@ test("installing another industry template is rejected without drifting brand id } }); +test("cross template requests are rejected even when a drifted ledger already exists", async () => { + const originalBrand = await prisma.brandProfile.findUniqueOrThrow({ where: { id: brandId } }); + await prisma.inquirySubmission.deleteMany({ where: { brandId } }); + await prisma.inquiryForm.deleteMany({ where: { brandId } }); + await prisma.brandFieldDefinition.deleteMany({ where: { brandId } }); + await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } }); + await prisma.brandTemplateInstallation.create({ + data: { + brandId, + templateKey: "doors", + templateVersion: "1.0.0", + configSnapshot: { schemaVersion: "1.0" }, + }, + }); + const login = await request("/admin/session/login", { + method: "POST", + payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD }, + }); + const headers = { + authorization: `Bearer ${JSON.parse(login.body).data?.token ?? JSON.parse(login.body).token}`, + }; + try { + const rejected = await request("/admin/workspace/install", { + method: "POST", + headers, + payload: { templateKey: "doors" }, + }); + assert.equal(rejected.statusCode, 400, rejected.body); + assert.match(errorMessage(rejected.body), /更换行业模板不在本版本范围内/); + + const brandAfter = await prisma.brandProfile.findUniqueOrThrow({ where: { id: brandId } }); + assert.equal(brandAfter.templateKey, originalBrand.templateKey); + assert.equal(await prisma.brandTemplateInstallation.count({ where: { brandId } }), 1); + + const workspace = await request("/public/workspace"); + assert.equal(workspace.statusCode, 200); + assert.equal(JSON.parse(workspace.body).template.key, originalBrand.templateKey); + } finally { + await prisma.brandFieldDefinition.deleteMany({ where: { brandId } }); + await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } }); + await prisma.brandProfile.update({ + where: { id: brandId }, + data: { templateKey: originalBrand.templateKey, config: originalBrand.config }, + }); + } +}); + test("admin can preview and import sample content idempotently", async () => { const originalBrand = await prisma.brandProfile.findUnique({ where: { id: brandId } }); const originalForms = await prisma.inquiryForm.findMany({ where: { brandId } }); @@ -2959,12 +3006,11 @@ test("admin can read and update brand and service configuration", async () => { payload: { description: "集成测试品牌简介", contactPhone: "13800000000", - templateKey: "personal", }, }); assert.equal(updatedBrand.statusCode, 200); assert.equal(JSON.parse(updatedBrand.body).description, "集成测试品牌简介"); - assert.equal(JSON.parse(updatedBrand.body).templateKey, "personal"); + assert.equal(JSON.parse(updatedBrand.body).templateKey, brandBefore.templateKey); const updatedConfig = JSON.parse(updatedBrand.body).config; assert.equal(updatedConfig.slogan, "集成测试品牌简介"); diff --git a/packages/api-client/src/types/brand.ts b/packages/api-client/src/types/brand.ts index dbef3f2..e7e2cf8 100644 --- a/packages/api-client/src/types/brand.ts +++ b/packages/api-client/src/types/brand.ts @@ -35,7 +35,7 @@ export interface AdminBrandMember { }; } -export type BrandUpdate = Partial>; +export type BrandUpdate = Partial>; export interface BrandReleaseSummary { id: string;