From 44b6f6db64ed6253a548520fab6ee4ef93f31436 Mon Sep 17 00:00:00 2001 From: que01 Date: Sun, 20 Sep 2026 17:55:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E9=A2=84=E8=A7=88=E4=B8=8E=E6=98=BE=E5=BC=8F?= =?UTF-8?q?=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migration.sql | 1 + apps/api/prisma/schema.prisma | 1 + apps/api/src/main.ts | 93 +++++++++++++++++++ apps/api/test/http.integration.test.js | 7 ++ packages/api-client/src/index.ts | 12 +++ 5 files changed, 114 insertions(+) create mode 100644 apps/api/prisma/migrations/20260920175000_add_template_upgrade_history/migration.sql diff --git a/apps/api/prisma/migrations/20260920175000_add_template_upgrade_history/migration.sql b/apps/api/prisma/migrations/20260920175000_add_template_upgrade_history/migration.sql new file mode 100644 index 0000000..9ebd9c7 --- /dev/null +++ b/apps/api/prisma/migrations/20260920175000_add_template_upgrade_history/migration.sql @@ -0,0 +1 @@ +ALTER TABLE "BrandTemplateInstallation" ADD COLUMN "previousSnapshots" JSONB NOT NULL DEFAULT '[]'; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 766216b..2c3f05c 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -91,6 +91,7 @@ model BrandTemplateInstallation { templateKey String templateVersion String @default("1.0.0") configSnapshot Json + previousSnapshots Json @default("[]") installedAt DateTime @default(now()) updatedAt DateTime @updatedAt @@unique([brandId, templateKey]) diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 8373e95..610948a 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -396,6 +396,99 @@ export class AdminWorkspaceController { }); } + @Get("template/preview") + async previewTemplate(@Req() request: FastifyRequest): Promise { + 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 { + 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") async updateModule( @Req() request: FastifyRequest, diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index 247f333..dd28b84 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -67,6 +67,13 @@ test("admin can install a template and public workspace exposes enabled configur assert.equal(installed.statusCode, 201); assert.equal(JSON.parse(installed.body).template.key, "personal"); 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", { headers: { authorization: `Bearer ${token}` }, }); diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index cc747d1..15cd381 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -345,6 +345,18 @@ export class ApiClient { installAdminTemplate(templateKey: "personal" | "food"): Promise { 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 { + return this.request("/admin/workspace/template/upgrade", { method: "POST" }); + } updateAdminModule(id: string, body: Partial): Promise { return this.request(`/admin/workspace/modules/${encodeURIComponent(id)}`, { method: "PATCH",