refactor: 统一模板契约并修复预览版本
This commit is contained in:
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Post, Body, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { InstallTemplateDto } from "../../admin.dto.js";
|
||||
import { InstallTemplateDto, SelectTemplateVersionDto } from "../../admin.dto.js";
|
||||
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { requireAdminIdentity } from "./admin-access.identity.js";
|
||||
import { AdminWorkspaceService } from "./admin-workspace.service.js";
|
||||
@@ -52,6 +52,21 @@ class AdminWorkspacePreviewController {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@Post("template/select-version")
|
||||
async selectVersion(
|
||||
@Req() request: FastifyRequest,
|
||||
@Body() body: SelectTemplateVersionDto,
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
const requestId = request.headers["x-request-id"]?.toString() ?? request.id;
|
||||
return this.templateWorkspace.selectVersion(
|
||||
identity.brandId,
|
||||
body,
|
||||
identity.subject,
|
||||
requestId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export { AdminWorkspacePreviewController };
|
||||
@@ -1,10 +1,5 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { PrismaService } from "../../prisma.service.js";
|
||||
import {
|
||||
defaultBrandTemplateKey,
|
||||
getDefaultVisualTemplateKey,
|
||||
getTemplatePackageManifestForVisualKey,
|
||||
} from "@personal-brand/config";
|
||||
import { PublicWorkspaceService } from "../public/public-workspace.service.js";
|
||||
|
||||
@Injectable()
|
||||
@@ -15,11 +10,11 @@ export class AdminWorkspaceService {
|
||||
) {}
|
||||
|
||||
async get(brandId: string): Promise<unknown> {
|
||||
const [brand, installations, fields, offerings, forms] = await Promise.all([
|
||||
const [brand, installation, fields, offerings, forms] = await Promise.all([
|
||||
this.prisma.brandProfile.findUnique({ where: { id: brandId } }),
|
||||
this.prisma.brandTemplateInstallation.findMany({
|
||||
this.prisma.brandTemplateInstallation.findUnique({
|
||||
where: { brandId },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
include: { templatePackageVersion: true },
|
||||
}),
|
||||
this.prisma.brandFieldDefinition.findMany({
|
||||
where: { brandId },
|
||||
@@ -28,36 +23,50 @@ export class AdminWorkspaceService {
|
||||
this.prisma.offering.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }),
|
||||
this.prisma.inquiryForm.findMany({ where: { brandId }, orderBy: { updatedAt: "desc" } }),
|
||||
]);
|
||||
const config = this.jsonObject(brand?.config);
|
||||
const templateKey =
|
||||
typeof config.visualTemplateKey === "string"
|
||||
? config.visualTemplateKey
|
||||
: getDefaultVisualTemplateKey(brand?.templateKey ?? defaultBrandTemplateKey);
|
||||
const manifest = getTemplatePackageManifestForVisualKey(templateKey);
|
||||
if (!installation?.templatePackageVersion)
|
||||
throw new BadRequestException("品牌没有锁定精确视觉模板包版本");
|
||||
const manifest = installation.templatePackageVersion.manifestSnapshot as {
|
||||
visualTemplateKey: string;
|
||||
displayName: string;
|
||||
admin: unknown;
|
||||
navigation: Array<{ key: string; label: string; page: string }>;
|
||||
templateFields?: Array<Record<string, unknown> & { key: string; binding?: string }>;
|
||||
assetSlots: unknown[];
|
||||
trustContentTypes?: unknown[];
|
||||
};
|
||||
|
||||
return {
|
||||
brand,
|
||||
installations,
|
||||
installations: [
|
||||
{
|
||||
...installation,
|
||||
visualTemplateKey: manifest.visualTemplateKey,
|
||||
},
|
||||
],
|
||||
fields,
|
||||
offerings,
|
||||
forms,
|
||||
templateContent: manifest
|
||||
? {
|
||||
displayName: manifest.displayName,
|
||||
admin: manifest.admin,
|
||||
navigation: manifest.navigation.map(({ key, label, page }) => ({ key, label, page })),
|
||||
templateFields: (manifest.templateFields ?? []).map((field) => ({
|
||||
...field,
|
||||
binding: field.binding ?? `brand.config.templateData.${field.key}`,
|
||||
})),
|
||||
assetSlots: manifest.assetSlots,
|
||||
trustContentTypes: manifest.trustContentTypes ?? [],
|
||||
}
|
||||
: null,
|
||||
templateContent: {
|
||||
displayName: manifest.displayName,
|
||||
admin: manifest.admin,
|
||||
navigation: manifest.navigation.map(({ key, label, page }) => ({ key, label, page })),
|
||||
templateFields: (manifest.templateFields ?? []).map((field) => ({
|
||||
...field,
|
||||
binding: field.binding ?? `brand.config.templateData.${field.key}`,
|
||||
})),
|
||||
assetSlots: manifest.assetSlots,
|
||||
trustContentTypes: manifest.trustContentTypes ?? [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
getPreview(brandId: string): Promise<unknown> {
|
||||
async getPreview(brandId: string): Promise<unknown> {
|
||||
const installation = await this.prisma.brandTemplateInstallation.findUnique({
|
||||
where: { brandId },
|
||||
select: { templatePackageVersionId: true },
|
||||
});
|
||||
if (!installation?.templatePackageVersionId)
|
||||
throw new BadRequestException("品牌没有锁定精确模板包版本");
|
||||
return this.publicWorkspace.getForBrandId(brandId, true);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user