refactor: 统一模板契约并修复预览版本

This commit is contained in:
que01 committed 2026-10-18 17:09:09 +08:00
1 parent cd2992c00a
commit 50b87e48de
56 files changed
+7849 -5756

No files matched your search

+52 -41
View File
@@ -1,10 +1,5 @@
import { PrismaClient, type Prisma } from "@prisma/client";
import {
getBrandTemplate,
getDefaultVisualTemplateKey,
getTemplatePackageManifestForVisualKey,
getVisualTemplateManifest,
} from "@personal-brand/config";
import type { TemplatePackageManifest } from "@personal-brand/config";
import bcrypt from "bcryptjs";
import { buildBrandReleaseSnapshot } from "../src/brand-release.service.js";
@@ -28,6 +23,7 @@ type OfferingKind = "service" | "product" | "package" | "subscription";
interface RuntimeSeedTemplate {
templateKey: "personal" | "food" | "doors";
visualTemplateKey: string;
brandConfig: Record<string, unknown>;
retiredOfferingNames?: string[];
offerings: Array<{
@@ -52,8 +48,8 @@ interface RuntimeSeedTemplate {
const runtimeSeedTemplates: Record<"personal" | "food" | "doors", RuntimeSeedTemplate> = {
personal: {
templateKey: "personal",
visualTemplateKey: "linjing-editorial",
brandConfig: {
visualTemplateKey: "linjing-editorial",
templateData: { designPhilosophy: "先厘清问题,再设计表达" },
title: "品牌策略顾问",
slogan: "用清晰的定位与可执行的表达,让专业被看见。",
@@ -166,8 +162,8 @@ const runtimeSeedTemplates: Record<"personal" | "food" | "doors", RuntimeSeedTem
},
food: {
templateKey: "food",
visualTemplateKey: "acheng-campus-food",
brandConfig: {
visualTemplateKey: "acheng-campus-food",
founder: "阿成",
title: "私房菜主理人",
slogan: "认真做好每一顿家常饭。",
@@ -293,9 +289,9 @@ const runtimeSeedTemplates: Record<"personal" | "food" | "doors", RuntimeSeedTem
},
doors: {
templateKey: "doors",
visualTemplateKey: "linjing-editorial",
retiredOfferingNames: ["别墅入户大门", "院门与围合系统", "室内户内门系统"],
brandConfig: {
visualTemplateKey: "linjing-editorial",
templateData: { designPhilosophy: "让门与空间建立长期、克制的关系" },
founder: "林叙安 (Lin XUAN)",
title: "高端门系统建筑研造主理人",
@@ -645,11 +641,22 @@ const runtimeSeedTemplates: Record<"personal" | "food" | "doors", RuntimeSeedTem
async function ensureRuntimeContent(brandId: string, template: RuntimeSeedTemplate): Promise<void> {
// 这里只初始化明确的开发演示品牌;业务内容统一来自视觉包 sampleContent,
// 新增生产模板无需添加 seed 分支,品牌样例内容仍须由管理员显式导入。
const visualKey = template.templateKey === "food" ? "acheng-campus-food" : "linjing-editorial";
const packageSample =
getTemplatePackageManifestForVisualKey(visualKey)?.sampleContent?.[template.templateKey];
if (!packageSample)
throw new Error(`Development sample is missing for ${visualKey}/${template.templateKey}`);
const installation = await prisma.brandTemplateInstallation.findFirst({
where: { brandId },
include: { templatePackageVersion: true },
});
const manifest = installation?.templatePackageVersion?.manifestSnapshot as unknown as
TemplatePackageManifest | undefined;
const packageSample = manifest?.sampleContent?.[template.templateKey];
if (
!installation?.templatePackageVersion ||
manifest?.visualTemplateKey !== template.visualTemplateKey ||
manifest.packageKey !== template.visualTemplateKey ||
!packageSample
)
throw new Error(
`Development package sample is missing for ${template.visualTemplateKey}/${template.templateKey}`,
);
const offerings = packageSample.offerings.map((item) => ({
name: item.name,
summary: item.summary,
@@ -787,36 +794,40 @@ async function ensureSeedRelease(brandId: string, actorId: string): Promise<void
async function ensureTemplateInstallation(brandId: string): Promise<void> {
const brand = await prisma.brandProfile.findUniqueOrThrow({
where: { id: brandId },
select: { templateKey: true, config: true },
select: { templateKey: true },
});
const template = runtimeSeedTemplates[brand.templateKey as keyof typeof runtimeSeedTemplates];
if (!template) throw new Error("Development brand has no explicit template fixture");
const visualKey = template.visualTemplateKey;
const existing = await prisma.brandTemplateInstallation.findUnique({
where: { brandId },
include: { templatePackageVersion: 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 },
});
if (
existing.templateKey !== template.templateKey ||
!existing.templatePackageVersionId ||
!existing.templatePackageVersion
)
throw new Error("Development brand installation does not satisfy the template contract");
return;
}
const packageVersion = await prisma.templatePackageVersion.findFirst({
where: { packageKey: visualKey, status: "published" },
orderBy: { importedAt: "desc" },
});
if (!packageVersion)
throw new Error(`Development brand requires a published package version: ${visualKey}`);
const configSnapshot = packageVersion.manifestSnapshot as Prisma.InputJsonValue;
await prisma.brandTemplateInstallation.create({
data: { brandId, templateKey: template.key, templateVersion: template.version, configSnapshot },
data: {
brandId,
templateKey: template.templateKey,
templateVersion: packageVersion.version,
templatePackageVersionId: packageVersion.id,
contentChecksum: packageVersion.contentChecksum,
configSnapshot,
},
});
}
@@ -846,8 +857,8 @@ async function main(): Promise<void> {
},
});
await ensureRuntimeContent(brand.id, runtimeSeedTemplates.food);
await ensureTemplateInstallation(brand.id);
await ensureRuntimeContent(brand.id, runtimeSeedTemplates.food);
for (const name of ["普通套餐", "私厨", "配送", "自取", "熟客", "重点跟进"]) {
await prisma.tag.upsert({
@@ -904,8 +915,8 @@ async function main(): Promise<void> {
update: { isEnabled: true, role: "owner" },
create: { adminUserId: admin.id, brandId: doorsBrand.id, role: "owner" },
});
await ensureRuntimeContent(doorsBrand.id, runtimeSeedTemplates.doors);
await ensureTemplateInstallation(doorsBrand.id);
await ensureRuntimeContent(doorsBrand.id, runtimeSeedTemplates.doors);
await ensureSeedRelease(brand.id, admin.id);
await ensureSeedRelease(doorsBrand.id, admin.id);
}