feat: 完成模板管理阶段4闭环
This commit is contained in:
1 parent
b1e8012ded
commit
d1958a7d7f
26 files changed
+1179
-310
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import type { TemplatePackageVersion } from "@prisma/client";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import {
|
||||
type BrandTemplateKey,
|
||||
@@ -95,7 +96,7 @@ export class TemplateWorkspaceService {
|
||||
}
|
||||
return defaultBrandTemplateKey;
|
||||
}
|
||||
async install(brandId: string, templateKey: BrandTemplateKey) {
|
||||
async install(brandId: string, templateKey: BrandTemplateKey, templatePackageVersionId?: string) {
|
||||
const template = getBrandTemplate(templateKey);
|
||||
if (!template) throw new BadRequestException("行业模板不存在");
|
||||
const [existing, brand] = await Promise.all([
|
||||
@@ -141,6 +142,11 @@ export class TemplateWorkspaceService {
|
||||
visualTemplate: visualManifest,
|
||||
capabilities: template.capabilities,
|
||||
};
|
||||
const packageVersion = await this.resolveInstallablePackageVersion(
|
||||
templateKey,
|
||||
visualTemplateKey,
|
||||
templatePackageVersionId,
|
||||
);
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
// 安装记录与品牌身份必须同时落库,否则会出现「品牌在跑 A 模板、台账记的是 B」的漂移。
|
||||
// 已经自定义过视觉模板的品牌,补建台账时保留原选择,只把行业模板身份写回。
|
||||
@@ -158,6 +164,9 @@ export class TemplateWorkspaceService {
|
||||
brandId,
|
||||
templateKey,
|
||||
templateVersion: template.version,
|
||||
visualTemplateKey,
|
||||
templatePackageVersionId: packageVersion?.id ?? null,
|
||||
contentChecksum: packageVersion?.contentChecksum ?? null,
|
||||
configSnapshot: templatePackageSnapshot as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
@@ -176,6 +185,42 @@ export class TemplateWorkspaceService {
|
||||
return { installation, template, visualTemplate: visualManifest };
|
||||
});
|
||||
}
|
||||
private async resolveInstallablePackageVersion(
|
||||
templateKey: BrandTemplateKey,
|
||||
visualTemplateKey: string,
|
||||
templatePackageVersionId?: string,
|
||||
): Promise<TemplatePackageVersion | null> {
|
||||
const version = templatePackageVersionId
|
||||
? await this.prisma.templatePackageVersion.findUnique({
|
||||
where: { id: templatePackageVersionId },
|
||||
})
|
||||
: await this.prisma.templatePackageVersion.findFirst({
|
||||
where: { packageKey: visualTemplateKey, status: "published" },
|
||||
orderBy: { importedAt: "desc" },
|
||||
});
|
||||
if (!version) {
|
||||
if (templatePackageVersionId) throw new BadRequestException("指定的模板包版本不存在");
|
||||
return null;
|
||||
}
|
||||
if (version.status !== "published")
|
||||
throw new BadRequestException("只有已发布的模板包版本可以安装到品牌");
|
||||
const manifest = record(version.manifestSnapshot);
|
||||
const manifestVisualTemplateKey =
|
||||
typeof manifest.visualTemplateKey === "string"
|
||||
? manifest.visualTemplateKey
|
||||
: version.packageKey;
|
||||
if (
|
||||
manifest.packageKey !== version.packageKey ||
|
||||
manifest.version !== version.version ||
|
||||
manifestVisualTemplateKey !== visualTemplateKey ||
|
||||
!Array.isArray(manifest.supportedIndustryTemplates) ||
|
||||
!manifest.supportedIndustryTemplates.includes(templateKey)
|
||||
) {
|
||||
throw new BadRequestException("模板包版本与行业或视觉模板不匹配");
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
private async installation(brandId: string) {
|
||||
const item = await this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } });
|
||||
if (!item) throw new BadRequestException("尚未安装行业模板");
|
||||
@@ -266,3 +311,9 @@ export class TemplateWorkspaceService {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function record(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
Reference in new issue
Block a user