feat: 收敛行业模板身份为品牌单一真相
- BrandProfile.templateKey 为唯一真相,安装记录降级为同模板的版本台账 - 安装只允许补建当前模板台账,跨行业模板请求返回 400,杜绝身份漂移 - preview 不再强制要求台账存在,台账缺失时以模板目录版本为准 - seed 按品牌身份补齐模板台账,消除「身份有、台账无」 - 记录 ADR 0026
This commit is contained in:
1 parent
896f3240f9
commit
6073e72940
4 files changed
+298
-40
No files matched your search
@@ -62,34 +62,47 @@ export class TemplateWorkspaceService {
|
||||
});
|
||||
}
|
||||
async getEffectiveTemplateKey(brandId: string): Promise<BrandTemplateKey> {
|
||||
const [installation, brand] = await Promise.all([
|
||||
const [brand, installation] = await Promise.all([
|
||||
this.prisma.brandProfile.findUnique({
|
||||
where: { id: brandId },
|
||||
select: { templateKey: true },
|
||||
}),
|
||||
this.prisma.brandTemplateInstallation.findFirst({
|
||||
where: { brandId },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
select: { templateKey: true },
|
||||
}),
|
||||
this.prisma.brandProfile.findUnique({
|
||||
where: { id: brandId },
|
||||
select: { templateKey: true },
|
||||
}),
|
||||
]);
|
||||
const key =
|
||||
getBrandTemplate(installation?.templateKey ?? brand?.templateKey ?? defaultBrandTemplateKey)
|
||||
?.key ?? defaultBrandTemplateKey;
|
||||
return key;
|
||||
// BrandProfile.templateKey 是「品牌使用哪个行业模板」的唯一真相;
|
||||
// 安装记录只是同一模板的版本台账,缺失时不能反过来改写品牌身份。
|
||||
for (const candidate of [brand?.templateKey, installation?.templateKey]) {
|
||||
const template = candidate ? getBrandTemplate(candidate) : undefined;
|
||||
if (template) return template.key;
|
||||
}
|
||||
return defaultBrandTemplateKey;
|
||||
}
|
||||
async install(brandId: string, templateKey: BrandTemplateKey) {
|
||||
const template = getBrandTemplate(templateKey);
|
||||
if (!template) throw new BadRequestException("行业模板不存在");
|
||||
const [existing, brand] = await Promise.all([
|
||||
this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } }),
|
||||
this.prisma.brandProfile.findUnique({ where: { id: brandId }, select: { config: true } }),
|
||||
this.prisma.brandProfile.findUnique({
|
||||
where: { id: brandId },
|
||||
select: { config: true, templateKey: true },
|
||||
}),
|
||||
]);
|
||||
if (existing)
|
||||
throw new BadRequestException(
|
||||
existing.templateKey === templateKey
|
||||
? "该模板已经安装"
|
||||
: "品牌已经安装主模板,请先完成模板切换流程",
|
||||
: "品牌已经安装主模板,更换行业模板不在本版本范围内",
|
||||
);
|
||||
// 一个品牌只使用一个行业模板(BrandProfile.templateKey 即身份)。安装只允许补建当前模板的版本台账,
|
||||
// 不得换成别的行业模板,否则会出现「身份是 A、台账是 B」的漂移,并把发布快照接到不适配的视觉模板上。
|
||||
const currentTemplate = brand?.templateKey ? getBrandTemplate(brand.templateKey) : undefined;
|
||||
if (currentTemplate && currentTemplate.key !== templateKey)
|
||||
throw new BadRequestException(
|
||||
`当前品牌已在使用「${currentTemplate.name}」行业模板,更换行业模板不在本版本范围内`,
|
||||
);
|
||||
const brandConfig =
|
||||
brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config)
|
||||
@@ -119,14 +132,17 @@ export class TemplateWorkspaceService {
|
||||
capabilities: template.capabilities,
|
||||
};
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
if (brandConfig.visualTemplateKey === undefined) {
|
||||
await tx.brandProfile.update({
|
||||
where: { id: brandId },
|
||||
data: {
|
||||
config: { ...brandConfig, visualTemplateKey },
|
||||
},
|
||||
});
|
||||
}
|
||||
// 安装记录与品牌身份必须同时落库,否则会出现「品牌在跑 A 模板、台账记的是 B」的漂移。
|
||||
// 已经自定义过视觉模板的品牌,补建台账时保留原选择,只把行业模板身份写回。
|
||||
const nextConfig = (
|
||||
brandConfig.visualTemplateKey === undefined
|
||||
? { ...brandConfig, visualTemplateKey }
|
||||
: brandConfig
|
||||
) as Prisma.InputJsonValue;
|
||||
await tx.brandProfile.update({
|
||||
where: { id: brandId },
|
||||
data: { templateKey, config: nextConfig },
|
||||
});
|
||||
const installation = await tx.brandTemplateInstallation.create({
|
||||
data: {
|
||||
brandId,
|
||||
@@ -156,16 +172,22 @@ export class TemplateWorkspaceService {
|
||||
return item;
|
||||
}
|
||||
async preview(brandId: string) {
|
||||
const installation = await this.installation(brandId);
|
||||
const template = getBrandTemplate(installation.templateKey);
|
||||
if (!template) throw new BadRequestException("已安装模板不存在");
|
||||
const fields = await this.prisma.brandFieldDefinition.findMany({ where: { brandId } });
|
||||
const templateKey = await this.getEffectiveTemplateKey(brandId);
|
||||
const template = getBrandTemplate(templateKey);
|
||||
if (!template) throw new BadRequestException("当前行业模板不存在");
|
||||
const [installation, fields] = await Promise.all([
|
||||
this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } }),
|
||||
this.prisma.brandFieldDefinition.findMany({ where: { brandId } }),
|
||||
]);
|
||||
// 品牌一直有行业模板,但版本台账可能还没建立;此时以模板目录的版本为准,不报错。
|
||||
const currentVersion = installation?.templateVersion ?? template.version;
|
||||
return {
|
||||
currentVersion: installation.templateVersion,
|
||||
templateKey,
|
||||
currentVersion,
|
||||
latestVersion: template.version,
|
||||
canUpgrade: installation.templateVersion !== template.version,
|
||||
canUpgrade: currentVersion !== template.version,
|
||||
canRollback:
|
||||
Array.isArray(installation.previousSnapshots) && installation.previousSnapshots.length > 0,
|
||||
Array.isArray(installation?.previousSnapshots) && installation.previousSnapshots.length > 0,
|
||||
missingFields: template.fields.filter(
|
||||
(item) =>
|
||||
!fields.some((current) => current.scope === item.scope && current.key === item.key),
|
||||
|
||||
Reference in new issue
Block a user