feat: 收敛行业模板身份为品牌单一真相

- BrandProfile.templateKey 为唯一真相,安装记录降级为同模板的版本台账
- 安装只允许补建当前模板台账,跨行业模板请求返回 400,杜绝身份漂移
- preview 不再强制要求台账存在,台账缺失时以模板目录版本为准
- seed 按品牌身份补齐模板台账,消除「身份有、台账无」
- 记录 ADR 0026
This commit is contained in:
que01 committed 2026-10-14 13:55:54 +08:00
1 parent 896f3240f9
commit 6073e72940
4 files changed
+298 -40

No files matched your search

+49 -1
View File
@@ -1,5 +1,10 @@
import { PrismaClient, type Prisma } from "@prisma/client";
import { getTemplatePackageManifestForVisualKey } from "@personal-brand/config";
import {
getBrandTemplate,
getDefaultVisualTemplateKey,
getTemplatePackageManifestForVisualKey,
getVisualTemplateManifest,
} from "@personal-brand/config";
import bcrypt from "bcryptjs";
import { buildBrandReleaseSnapshot } from "../src/brand-release.service.js";
@@ -768,6 +773,47 @@ async function ensureSeedRelease(brandId: string, actorId: string): Promise<void
});
}
/**
* 行业的模板台账只能有一个,且必须与品牌身份(BrandProfile.templateKey)一致。
* seed 之前只写品牌身份、不建台账,导致后台「尚未安装模板」而公开运行时已在跑该模板;
* 这里按品牌身份补齐台账,让「版本与升级」「回滚」有据可依。
*/
async function ensureTemplateInstallation(brandId: string): Promise<void> {
const brand = await prisma.brandProfile.findUniqueOrThrow({
where: { id: brandId },
select: { templateKey: true, config: true },
});
const template = getBrandTemplate(brand.templateKey);
if (!template) throw new Error("Development brand has an unknown industry template");
const config =
brand.config && typeof brand.config === "object" && !Array.isArray(brand.config)
? (brand.config as Record<string, unknown>)
: {};
const configuredVisual =
typeof config.visualTemplateKey === "string" ? config.visualTemplateKey : undefined;
const visualKey = configuredVisual ?? getDefaultVisualTemplateKey(template.key);
if (!getVisualTemplateManifest(visualKey))
throw new Error("Development brand has an unknown visual template");
const configSnapshot = {
schemaVersion: "1.0",
version: template.version,
industryTemplate: template,
visualTemplate: getVisualTemplateManifest(visualKey),
capabilities: template.capabilities,
} as unknown as Prisma.InputJsonValue;
const existing = await prisma.brandTemplateInstallation.findFirst({ where: { brandId } });
if (existing) {
await prisma.brandTemplateInstallation.update({
where: { id: existing.id },
data: { templateKey: template.key, templateVersion: template.version, configSnapshot },
});
return;
}
await prisma.brandTemplateInstallation.create({
data: { brandId, templateKey: template.key, templateVersion: template.version, configSnapshot },
});
}
async function main(): Promise<void> {
const brand = await prisma.brandProfile.upsert({
where: { id: "00000000-0000-0000-0000-000000000001" },
@@ -795,6 +841,7 @@ async function main(): Promise<void> {
});
await ensureRuntimeContent(brand.id, runtimeSeedTemplates.food);
await ensureTemplateInstallation(brand.id);
for (const name of ["普通套餐", "私厨", "配送", "自取", "熟客", "重点跟进"]) {
await prisma.tag.upsert({
@@ -851,6 +898,7 @@ async function main(): Promise<void> {
create: { adminUserId: admin.id, brandId: doorsBrand.id, role: "owner" },
});
await ensureRuntimeContent(doorsBrand.id, runtimeSeedTemplates.doors);
await ensureTemplateInstallation(doorsBrand.id);
await ensureSeedRelease(brand.id, admin.id);
await ensureSeedRelease(doorsBrand.id, admin.id);
}