refactor: 统一模板契约并修复预览版本
This commit is contained in:
1 parent
cd2992c00a
commit
50b87e48de
56 files changed
+7849
-5756
No files matched your search
+107
@@ -0,0 +1,107 @@
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM "BrandTemplateInstallation"
|
||||
GROUP BY "brandId"
|
||||
HAVING COUNT(*) > 1
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Template contract migration stopped: a brand has multiple template installations';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM "BrandTemplateInstallation" installation
|
||||
LEFT JOIN "TemplatePackageVersion" package_version
|
||||
ON package_version."id" = installation."templatePackageVersionId"
|
||||
WHERE installation."templatePackageVersionId" IS NULL
|
||||
OR package_version."id" IS NULL
|
||||
OR package_version."status" <> 'published'
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Template contract migration stopped: an installation has no exact package version';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM "BrandTemplateInstallation" installation
|
||||
JOIN "TemplatePackageVersion" package_version
|
||||
ON package_version."id" = installation."templatePackageVersionId"
|
||||
JOIN "BrandProfile" brand ON brand."id" = installation."brandId"
|
||||
WHERE installation."visualTemplateKey" IS DISTINCT FROM package_version."visualTemplateKey"
|
||||
OR package_version."visualTemplateKey" IS DISTINCT FROM package_version."packageKey"
|
||||
OR package_version."manifestSnapshot"->>'visualTemplateKey' IS DISTINCT FROM package_version."visualTemplateKey"
|
||||
OR package_version."manifestSnapshot"->>'packageKey' IS DISTINCT FROM package_version."packageKey"
|
||||
OR package_version."manifestSnapshot"->>'version' IS DISTINCT FROM package_version."version"
|
||||
OR NOT COALESCE(package_version."manifestSnapshot"->'supportedIndustryTemplates' ? brand."templateKey", false)
|
||||
OR installation."templateKey" IS DISTINCT FROM brand."templateKey"
|
||||
OR (installation."contentChecksum" IS NOT NULL AND installation."contentChecksum" IS DISTINCT FROM package_version."contentChecksum")
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Template contract migration stopped: installation, brand, package row, or manifest disagree';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM "BrandProfile" brand
|
||||
LEFT JOIN "BrandTemplateInstallation" installation ON installation."brandId" = brand."id"
|
||||
WHERE installation."id" IS NULL
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Template contract migration stopped: a brand has no explicit template installation';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM "BrandProfile" brand
|
||||
JOIN "BrandTemplateInstallation" installation ON installation."brandId" = brand."id"
|
||||
JOIN "TemplatePackageVersion" package_version
|
||||
ON package_version."id" = installation."templatePackageVersionId"
|
||||
WHERE brand."config" ? 'visualTemplateKey'
|
||||
AND brand."config"->>'visualTemplateKey' IS DISTINCT FROM package_version."visualTemplateKey"
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Template contract migration stopped: brand config visualTemplateKey conflicts with its installation';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM "BrandProfile" brand
|
||||
LEFT JOIN "BrandTemplateInstallation" installation ON installation."brandId" = brand."id"
|
||||
WHERE brand."config" ? 'visualTemplateKey'
|
||||
AND installation."id" IS NULL
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Template contract migration stopped: brand config visualTemplateKey has no installation to verify against';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM "TemplatePackageVersion" package_version
|
||||
WHERE package_version."visualTemplateKey" IS DISTINCT FROM package_version."packageKey"
|
||||
OR package_version."manifestSnapshot"->>'visualTemplateKey' IS DISTINCT FROM package_version."packageKey"
|
||||
OR package_version."manifestSnapshot"->>'packageKey' IS DISTINCT FROM package_version."packageKey"
|
||||
OR package_version."manifestSnapshot"->>'version' IS DISTINCT FROM package_version."version"
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Template contract migration stopped: package metadata conflicts with its manifest';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
UPDATE "BrandTemplateInstallation" installation
|
||||
SET "templateVersion" = package_version."version",
|
||||
"contentChecksum" = package_version."contentChecksum"
|
||||
FROM "TemplatePackageVersion" package_version
|
||||
WHERE package_version."id" = installation."templatePackageVersionId";
|
||||
|
||||
UPDATE "BrandProfile"
|
||||
SET "config" = "config" - 'visualTemplateKey'
|
||||
WHERE "config" ? 'visualTemplateKey';
|
||||
|
||||
ALTER TABLE "BrandTemplateInstallation"
|
||||
DROP CONSTRAINT "BrandTemplateInstallation_templatePackageVersionId_fkey",
|
||||
DROP COLUMN "visualTemplateKey",
|
||||
ALTER COLUMN "templatePackageVersionId" SET NOT NULL,
|
||||
ADD CONSTRAINT "BrandTemplateInstallation_templatePackageVersionId_fkey"
|
||||
FOREIGN KEY ("templatePackageVersionId") REFERENCES "TemplatePackageVersion"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "TemplatePackageVersion" DROP COLUMN "visualTemplateKey";
|
||||
|
||||
DROP INDEX "BrandTemplateInstallation_brandId_templateKey_key";
|
||||
CREATE UNIQUE INDEX "BrandTemplateInstallation_brandId_key" ON "BrandTemplateInstallation"("brandId");
|
||||
@@ -223,22 +223,19 @@ model CapabilityAuditLog {
|
||||
}
|
||||
|
||||
model BrandTemplateInstallation {
|
||||
id String @id @default(uuid())
|
||||
brandId String
|
||||
brand BrandProfile @relation(fields: [brandId], references: [id], onDelete: Cascade)
|
||||
id String @id @default(uuid())
|
||||
brandId String @unique
|
||||
brand BrandProfile @relation(fields: [brandId], references: [id], onDelete: Cascade)
|
||||
templateKey String
|
||||
templateVersion String @default("1.0.0")
|
||||
visualTemplateKey String?
|
||||
templatePackageVersionId String?
|
||||
templatePackageVersion TemplatePackageVersion? @relation("BrandTemplateInstallationPackageVersion", fields: [templatePackageVersionId], references: [id], onDelete: SetNull)
|
||||
templateVersion String @default("1.0.0")
|
||||
templatePackageVersionId String
|
||||
templatePackageVersion TemplatePackageVersion @relation("BrandTemplateInstallationPackageVersion", fields: [templatePackageVersionId], references: [id], onDelete: Restrict)
|
||||
contentChecksum String?
|
||||
configSnapshot Json
|
||||
previousSnapshots Json @default("[]")
|
||||
installedAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
previousSnapshots Json @default("[]")
|
||||
installedAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@unique([brandId, templateKey])
|
||||
@@index([brandId])
|
||||
@@index([templatePackageVersionId])
|
||||
}
|
||||
|
||||
@@ -484,7 +481,6 @@ model TemplatePackageVersion {
|
||||
packageKey String
|
||||
version String
|
||||
displayName String
|
||||
visualTemplateKey String?
|
||||
status TemplatePackageVersionStatus @default(imported)
|
||||
manifestSnapshot Json
|
||||
validationReport Json
|
||||
|
||||
+52
-41
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user