fix: 保证品牌模板身份不被台账漂移
This commit is contained in:
1 parent
6073e72940
commit
aaeec14733
6 files changed
+53
-13
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<string, unknown>)
|
||||
|
||||
@@ -95,9 +95,9 @@ export function assemblePublicWorkspace(input: {
|
||||
) as Array<Record<string, unknown>>;
|
||||
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<Record<string, unknown>>;
|
||||
|
||||
@@ -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<string, unknown>)
|
||||
|
||||
@@ -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, "集成测试品牌简介");
|
||||
|
||||
|
||||
Reference in new issue
Block a user