refactor: 抽离模板工作空间服务
This commit is contained in:
1 parent
9e4f2620c6
commit
b2788966bf
3 files changed
+178
-153
No files matched your search
@@ -0,0 +1,161 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { getBrandTemplate } from "@personal-brand/config";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
|
||||
@Injectable()
|
||||
export class TemplateWorkspaceService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
async install(brandId: string, templateKey: "personal" | "food") {
|
||||
const template = getBrandTemplate(templateKey);
|
||||
if (!template) throw new BadRequestException("行业模板不存在");
|
||||
const existing = await this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } });
|
||||
if (existing)
|
||||
throw new BadRequestException(
|
||||
existing.templateKey === templateKey
|
||||
? "该模板已经安装"
|
||||
: "品牌已经安装主模板,请先完成模板切换流程",
|
||||
);
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const installation = await tx.brandTemplateInstallation.create({
|
||||
data: {
|
||||
brandId,
|
||||
templateKey,
|
||||
templateVersion: template.version,
|
||||
configSnapshot: template as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
await tx.brandModule.createMany({
|
||||
data: template.modules.map(({ type, title }, sortOrder) => ({
|
||||
brandId,
|
||||
type,
|
||||
title,
|
||||
sortOrder,
|
||||
source: "template",
|
||||
})),
|
||||
});
|
||||
await tx.brandFieldDefinition.createMany({
|
||||
data: template.fields.map(({ scope, key, label, type, required }, sortOrder) => ({
|
||||
brandId,
|
||||
scope,
|
||||
key,
|
||||
label,
|
||||
type,
|
||||
sortOrder,
|
||||
source: "core",
|
||||
required,
|
||||
})),
|
||||
});
|
||||
return { installation, template };
|
||||
});
|
||||
}
|
||||
private async installation(brandId: string) {
|
||||
const item = await this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } });
|
||||
if (!item) throw new BadRequestException("尚未安装行业模板");
|
||||
return item;
|
||||
}
|
||||
async preview(brandId: string) {
|
||||
const installation = await this.installation(brandId);
|
||||
const template = getBrandTemplate(installation.templateKey);
|
||||
if (!template) throw new BadRequestException("已安装模板不存在");
|
||||
const [modules, fields] = await Promise.all([
|
||||
this.prisma.brandModule.findMany({ where: { brandId } }),
|
||||
this.prisma.brandFieldDefinition.findMany({ where: { brandId } }),
|
||||
]);
|
||||
return {
|
||||
currentVersion: installation.templateVersion,
|
||||
latestVersion: template.version,
|
||||
canUpgrade: installation.templateVersion !== template.version,
|
||||
canRollback:
|
||||
Array.isArray(installation.previousSnapshots) && installation.previousSnapshots.length > 0,
|
||||
missingModules: template.modules.filter(
|
||||
(item) =>
|
||||
!modules.some((current) => current.type === item.type && current.source === "template"),
|
||||
),
|
||||
missingFields: template.fields.filter(
|
||||
(item) =>
|
||||
!fields.some((current) => current.scope === item.scope && current.key === item.key),
|
||||
),
|
||||
};
|
||||
}
|
||||
async upgrade(brandId: string) {
|
||||
const installation = await this.installation(brandId);
|
||||
const template = getBrandTemplate(installation.templateKey);
|
||||
if (!template) throw new BadRequestException("已安装模板不存在");
|
||||
if (installation.templateVersion === template.version)
|
||||
throw new BadRequestException("当前模板已经是最新版本");
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const [modules, fields] = await Promise.all([
|
||||
tx.brandModule.findMany({ where: { brandId } }),
|
||||
tx.brandFieldDefinition.findMany({ where: { brandId } }),
|
||||
]);
|
||||
const addedModules = template.modules.filter(
|
||||
(item) =>
|
||||
!modules.some((current) => current.type === item.type && current.source === "template"),
|
||||
);
|
||||
const addedFields = template.fields.filter(
|
||||
(item) =>
|
||||
!fields.some((current) => current.scope === item.scope && current.key === item.key),
|
||||
);
|
||||
await tx.brandModule.createMany({
|
||||
data: addedModules.map(({ type, title }, index) => ({
|
||||
brandId,
|
||||
type,
|
||||
title,
|
||||
sortOrder: modules.length + index,
|
||||
source: "template",
|
||||
})),
|
||||
});
|
||||
await tx.brandFieldDefinition.createMany({
|
||||
data: addedFields.map(({ scope, key, label, type, required }, index) => ({
|
||||
brandId,
|
||||
scope,
|
||||
key,
|
||||
label,
|
||||
type,
|
||||
required,
|
||||
sortOrder: fields.length + index,
|
||||
source: "template",
|
||||
})),
|
||||
});
|
||||
const history = Array.isArray(installation.previousSnapshots)
|
||||
? installation.previousSnapshots
|
||||
: [];
|
||||
const updated = await tx.brandTemplateInstallation.update({
|
||||
where: { id: installation.id },
|
||||
data: {
|
||||
templateVersion: template.version,
|
||||
configSnapshot: template as unknown as Prisma.InputJsonValue,
|
||||
previousSnapshots: [
|
||||
...history,
|
||||
installation.configSnapshot,
|
||||
] as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
return { installation: updated, addedModules, addedFields };
|
||||
});
|
||||
}
|
||||
async rollback(brandId: string) {
|
||||
const installation = await this.installation(brandId);
|
||||
const history = Array.isArray(installation.previousSnapshots)
|
||||
? installation.previousSnapshots
|
||||
: [];
|
||||
const previous = history.at(-1);
|
||||
if (!previous || typeof previous !== "object" || Array.isArray(previous))
|
||||
throw new BadRequestException("没有可回滚的模板版本");
|
||||
const version = (previous as { version?: unknown }).version;
|
||||
if (typeof version !== "string") throw new BadRequestException("模板快照无效");
|
||||
const updated = await this.prisma.brandTemplateInstallation.update({
|
||||
where: { id: installation.id },
|
||||
data: {
|
||||
templateVersion: version,
|
||||
configSnapshot: previous as Prisma.InputJsonValue,
|
||||
previousSnapshots: history.slice(0, -1) as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
return {
|
||||
installation: updated,
|
||||
note: "已恢复上一版模板配置基线,现有模块、字段和业务内容未删除。",
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user