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

@@ -1,9 +1,6 @@
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { Prisma, type TemplatePackageVersion } from "@prisma/client";
import {
getDefaultVisualTemplateKey,
getTemplatePackageManifestForVisualKey,
getTemplateSampleContent,
getVisualTemplate,
isVisualTemplateCompatible,
validateTemplatePackageManifest,
@@ -15,10 +12,10 @@ import { PrismaService } from "../../prisma.service.js";
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
type BootstrapSource = {
kind: "package_version" | "registry";
kind: "package_version";
manifest: TemplatePackageManifest;
sample: TemplateSampleContent;
version?: TemplatePackageVersion;
version: TemplatePackageVersion;
};
type Counts = {
@@ -111,8 +108,6 @@ export class AdminSampleContentService {
throw new BadRequestException("当前只支持 missing_only 导入策略");
const { brand, source } = await this.resolveSource(brandId, body.templatePackageVersionId);
if (!source.version)
throw new BadRequestException("品牌尚未安装可追溯的已发布模板包版本,不能导入示例内容");
const plan = await this.buildPlan(brandId, brand.config, source);
const priorImport = await this.prisma.templateBootstrapImport.findFirst({
where: {
@@ -263,50 +258,35 @@ export class AdminSampleContentService {
select: { templateKey: true, config: true },
});
if (!brand) throw new NotFoundException("品牌不存在");
const config = record(brand.config);
const visualTemplateKey =
typeof config.visualTemplateKey === "string"
? config.visualTemplateKey
: getDefaultVisualTemplateKey(brand.templateKey);
const installation = requestedVersionId
? null
: await this.prisma.brandTemplateInstallation.findFirst({
: await this.prisma.brandTemplateInstallation.findUnique({
where: { brandId },
orderBy: { updatedAt: "desc" },
select: { templatePackageVersionId: true },
});
const versionId = requestedVersionId ?? installation?.templatePackageVersionId ?? undefined;
let source: BootstrapSource;
if (versionId) {
const version = await this.prisma.templatePackageVersion.findUnique({
where: { id: versionId },
});
if (!version) throw new BadRequestException("指定的模板包版本不存在");
if (version.status !== "published")
throw new BadRequestException("只有已发布的模板包版本可用于 bootstrap");
const manifest = version.manifestSnapshot as unknown as TemplatePackageManifest;
const issues = validateTemplatePackageManifest(manifest);
if (issues.length)
throw new BadRequestException(`模板包 manifest 无效:${issues.join(";")}`);
if (
manifest.packageKey !== version.packageKey ||
manifest.version !== version.version ||
!/^[a-f0-9]{64}$/.test(version.contentChecksum) ||
(version.visualTemplateKey ?? version.packageKey) !==
(manifest.visualTemplateKey ?? version.packageKey) ||
!manifest.supportedIndustryTemplates.includes(brand.templateKey)
)
throw new BadRequestException("模板包版本与品牌行业或版本台账不匹配");
const sample = manifest.sampleContent?.[brand.templateKey];
if (!sample) throw new BadRequestException("该模板包没有当前行业的 bootstrap sampleContent");
source = { kind: "package_version", version, manifest, sample };
} else {
const manifest = getTemplatePackageManifestForVisualKey(visualTemplateKey);
const sample = getTemplateSampleContent(brand.templateKey, visualTemplateKey);
if (!manifest || !sample || requestedVersionId)
throw new BadRequestException("当前模板没有可用的 bootstrap sampleContent");
source = { kind: "registry", manifest, sample };
}
if (!versionId) throw new BadRequestException("品牌没有精确模板包安装版本,必须先完成模板安装");
const version = await this.prisma.templatePackageVersion.findUnique({
where: { id: versionId },
});
if (!version) throw new BadRequestException("指定的模板包版本不存在");
if (version.status !== "published")
throw new BadRequestException("只有已发布的模板包版本可用于 bootstrap");
const manifest = version.manifestSnapshot as unknown as TemplatePackageManifest;
const issues = validateTemplatePackageManifest(manifest);
if (issues.length) throw new BadRequestException(`模板包 manifest 无效:${issues.join(";")}`);
if (
manifest.packageKey !== version.packageKey ||
manifest.version !== version.version ||
manifest.packageKey !== manifest.visualTemplateKey ||
!/^[a-f0-9]{64}$/.test(version.contentChecksum) ||
!manifest.supportedIndustryTemplates.includes(brand.templateKey)
)
throw new BadRequestException("模板包版本与品牌行业或版本台账不匹配");
const sample = manifest.sampleContent?.[brand.templateKey];
if (!sample) throw new BadRequestException("该模板包没有当前行业的 bootstrap sampleContent");
const source: BootstrapSource = { kind: "package_version", version, manifest, sample };
const visualTemplateKey = manifest.visualTemplateKey;
const visual = getVisualTemplate(visualTemplateKey);
if (!visual || !isVisualTemplateCompatible(visual, brand.templateKey))
throw new BadRequestException("当前视觉模板与品牌行业不兼容");
@@ -417,8 +397,8 @@ export class AdminSampleContentService {
kind: source.kind,
packageKey: source.manifest.packageKey,
version: source.manifest.version,
contentChecksum: source.version?.contentChecksum ?? null,
...(source.version ? { templatePackageVersionId: source.version.id } : {}),
contentChecksum: source.version.contentChecksum,
templatePackageVersionId: source.version.id,
};
}
}