feat: 增加模板升级预览与显式升级
This commit is contained in:
1 parent
388032000a
commit
44b6f6db64
5 files changed
+114
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE "BrandTemplateInstallation" ADD COLUMN "previousSnapshots" JSONB NOT NULL DEFAULT '[]';
|
||||||
@@ -91,6 +91,7 @@ model BrandTemplateInstallation {
|
|||||||
templateKey String
|
templateKey String
|
||||||
templateVersion String @default("1.0.0")
|
templateVersion String @default("1.0.0")
|
||||||
configSnapshot Json
|
configSnapshot Json
|
||||||
|
previousSnapshots Json @default("[]")
|
||||||
installedAt DateTime @default(now())
|
installedAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
@@unique([brandId, templateKey])
|
@@unique([brandId, templateKey])
|
||||||
|
|||||||
@@ -396,6 +396,99 @@ export class AdminWorkspaceController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get("template/preview")
|
||||||
|
async previewTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
|
this.requireAdmin(request);
|
||||||
|
const installation = await this.prisma.brandTemplateInstallation.findFirst({
|
||||||
|
where: { brandId: this.brandId() },
|
||||||
|
});
|
||||||
|
if (!installation) throw new BadRequestException("尚未安装行业模板");
|
||||||
|
const template = getBrandTemplate(installation.templateKey);
|
||||||
|
if (!template) throw new BadRequestException("已安装模板不存在");
|
||||||
|
const [modules, fields] = await Promise.all([
|
||||||
|
this.prisma.brandModule.findMany({ where: { brandId: this.brandId() } }),
|
||||||
|
this.prisma.brandFieldDefinition.findMany({ where: { brandId: this.brandId() } }),
|
||||||
|
]);
|
||||||
|
const missingModules = template.modules.filter(
|
||||||
|
(item) =>
|
||||||
|
!modules.some((current) => current.type === item.type && current.source === "template"),
|
||||||
|
);
|
||||||
|
const missingFields = template.fields.filter(
|
||||||
|
(item) => !fields.some((current) => current.scope === item.scope && current.key === item.key),
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
currentVersion: installation.templateVersion,
|
||||||
|
latestVersion: template.version,
|
||||||
|
canUpgrade: installation.templateVersion !== template.version,
|
||||||
|
missingModules,
|
||||||
|
missingFields,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("template/upgrade")
|
||||||
|
async upgradeTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
|
this.requireAdmin(request);
|
||||||
|
const brandId = this.brandId();
|
||||||
|
const installation = await this.prisma.brandTemplateInstallation.findFirst({
|
||||||
|
where: { brandId },
|
||||||
|
});
|
||||||
|
if (!installation) throw new BadRequestException("尚未安装行业模板");
|
||||||
|
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 missingModules = template.modules.filter(
|
||||||
|
(item) =>
|
||||||
|
!modules.some((current) => current.type === item.type && current.source === "template"),
|
||||||
|
);
|
||||||
|
const missingFields = template.fields.filter(
|
||||||
|
(item) =>
|
||||||
|
!fields.some((current) => current.scope === item.scope && current.key === item.key),
|
||||||
|
);
|
||||||
|
await tx.brandModule.createMany({
|
||||||
|
data: missingModules.map(({ type, title }, index) => ({
|
||||||
|
brandId,
|
||||||
|
type,
|
||||||
|
title,
|
||||||
|
sortOrder: modules.length + index,
|
||||||
|
source: "template",
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
await tx.brandFieldDefinition.createMany({
|
||||||
|
data: missingFields.map(({ scope, key, label, type, required }, index) => ({
|
||||||
|
brandId,
|
||||||
|
scope,
|
||||||
|
key,
|
||||||
|
label,
|
||||||
|
type,
|
||||||
|
required,
|
||||||
|
sortOrder: fields.length + index,
|
||||||
|
source: "template",
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
const previousSnapshots = 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: [
|
||||||
|
...previousSnapshots,
|
||||||
|
installation.configSnapshot,
|
||||||
|
] as unknown as Prisma.InputJsonValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { installation: updated, addedModules: missingModules, addedFields: missingFields };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Patch("modules/:id")
|
@Patch("modules/:id")
|
||||||
async updateModule(
|
async updateModule(
|
||||||
@Req() request: FastifyRequest,
|
@Req() request: FastifyRequest,
|
||||||
|
|||||||
@@ -67,6 +67,13 @@ test("admin can install a template and public workspace exposes enabled configur
|
|||||||
assert.equal(installed.statusCode, 201);
|
assert.equal(installed.statusCode, 201);
|
||||||
assert.equal(JSON.parse(installed.body).template.key, "personal");
|
assert.equal(JSON.parse(installed.body).template.key, "personal");
|
||||||
try {
|
try {
|
||||||
|
const previewUnauthorized = await request("/admin/workspace/template/preview");
|
||||||
|
assert.equal(previewUnauthorized.statusCode, 401);
|
||||||
|
const preview = await request("/admin/workspace/template/preview", {
|
||||||
|
headers: { authorization: `Bearer ${token}` },
|
||||||
|
});
|
||||||
|
assert.equal(preview.statusCode, 200);
|
||||||
|
assert.equal(JSON.parse(preview.body).canUpgrade, false);
|
||||||
const workspace = await request("/admin/workspace", {
|
const workspace = await request("/admin/workspace", {
|
||||||
headers: { authorization: `Bearer ${token}` },
|
headers: { authorization: `Bearer ${token}` },
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -345,6 +345,18 @@ export class ApiClient {
|
|||||||
installAdminTemplate(templateKey: "personal" | "food"): Promise<unknown> {
|
installAdminTemplate(templateKey: "personal" | "food"): Promise<unknown> {
|
||||||
return this.request("/admin/workspace/install", { method: "POST", body: { templateKey } });
|
return this.request("/admin/workspace/install", { method: "POST", body: { templateKey } });
|
||||||
}
|
}
|
||||||
|
getAdminTemplateUpgradePreview(): Promise<{
|
||||||
|
currentVersion: string;
|
||||||
|
latestVersion: string;
|
||||||
|
canUpgrade: boolean;
|
||||||
|
missingModules: unknown[];
|
||||||
|
missingFields: unknown[];
|
||||||
|
}> {
|
||||||
|
return this.request("/admin/workspace/template/preview");
|
||||||
|
}
|
||||||
|
upgradeAdminTemplate(): Promise<unknown> {
|
||||||
|
return this.request("/admin/workspace/template/upgrade", { method: "POST" });
|
||||||
|
}
|
||||||
updateAdminModule(id: string, body: Partial<BrandModule>): Promise<BrandModule> {
|
updateAdminModule(id: string, body: Partial<BrandModule>): Promise<BrandModule> {
|
||||||
return this.request(`/admin/workspace/modules/${encodeURIComponent(id)}`, {
|
return this.request(`/admin/workspace/modules/${encodeURIComponent(id)}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
|
|||||||
Reference in new issue
Block a user