From 193858c7272090a0b08dddd607ae1ec0f0b3306a Mon Sep 17 00:00:00 2001 From: que01 Date: Tue, 20 Oct 2026 22:14:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E5=A5=91=E7=BA=A6=E5=8F=91=E5=B8=83=E5=BF=AB=E7=85=A7=E9=87=8D?= =?UTF-8?q?=E5=8F=91=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/republish-stale-template-contracts.mjs | 114 ++++++++++++++++++ ...epublish-stale-template-contracts.test.mjs | 51 ++++++++ 2 files changed, 165 insertions(+) create mode 100644 deploy/republish-stale-template-contracts.mjs create mode 100644 deploy/republish-stale-template-contracts.test.mjs diff --git a/deploy/republish-stale-template-contracts.mjs b/deploy/republish-stale-template-contracts.mjs new file mode 100644 index 0000000..f91fd9a --- /dev/null +++ b/deploy/republish-stale-template-contracts.mjs @@ -0,0 +1,114 @@ +#!/usr/bin/env node + +import { isDeepStrictEqual } from "node:util"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { createRequire } from "node:module"; + +const CONTRACT_KEYS = new Set([ + "templateDefinition", + "visualTemplateDefinition", + "visualTemplateManifest", + "templatePackageManifest", + "installation", + "templateKey", +]); +const VOLATILE_KEYS = new Set(["generatedAt"]); + +/** + * 只允许自动重发“模板契约字段变化、品牌内容未变”的发布快照。 + * 一旦草稿内容与已发布快照不同,必须由管理员显式审查后发布,避免误发草稿。 + */ +export function classifyContractRefresh(published, current) { + if (!published || !current) throw new Error("发布快照不能为空"); + const keys = new Set([...Object.keys(published), ...Object.keys(current)]); + const changedKeys = [...keys] + .filter((key) => !VOLATILE_KEYS.has(key)) + .filter((key) => !isDeepStrictEqual(published[key], current[key])); + if (changedKeys.length === 0) return { status: "current", changedKeys, blockingKeys: [] }; + const blockingKeys = changedKeys.filter((key) => !CONTRACT_KEYS.has(key)); + return { + status: blockingKeys.length ? "content-diverged" : "contract-stale", + changedKeys, + blockingKeys, + }; +} + +async function loadServices() { + const apiRequire = createRequire(new URL("../apps/api/package.json", import.meta.url)); + const { PrismaService } = await import("../apps/api/dist/prisma.service.js"); + const { BrandReleaseService } = await import("../apps/api/dist/brand-release.service.js"); + const { BrandReleaseApiService } = await import("../apps/api/dist/brand-release-api.service.js"); + // 触发运行时依赖解析,避免发布脚本在错误的工作目录下才失败。 + apiRequire("@prisma/client"); + const prisma = new PrismaService(); + const snapshots = new BrandReleaseService(prisma); + return { + prisma, + snapshots, + releases: new BrandReleaseApiService(prisma, snapshots), + }; +} + +async function main() { + const apply = process.argv.includes("--apply"); + const actorUsername = process.env.TEMPLATE_CATALOG_ACTOR ?? "admin"; + const note = + process.env.TEMPLATE_CONTRACT_REPUBLISH_NOTE ?? "模板契约迁移:刷新已发布页面方案兼容性"; + const { prisma, snapshots, releases } = await loadServices(); + try { + const actor = await prisma.adminUser.findFirst({ + where: { username: actorUsername, isEnabled: true }, + select: { id: true }, + }); + if (!actor) throw new Error(`找不到启用的模板迁移操作人:${actorUsername}`); + const brands = await prisma.brandProfile.findMany({ + select: { id: true, slug: true }, + orderBy: { slug: "asc" }, + }); + const results = []; + for (const brand of brands) { + const release = await prisma.brandRelease.findFirst({ + where: { brandId: brand.id, status: "published" }, + orderBy: { version: "desc" }, + select: { version: true, snapshot: true }, + }); + if (!release) { + results.push({ slug: brand.slug, status: "unpublished" }); + continue; + } + const current = await snapshots.buildSnapshot(brand.id); + const classification = classifyContractRefresh(release.snapshot, current); + const result = { + slug: brand.slug, + releaseVersion: release.version, + ...classification, + }; + results.push(result); + if (!apply || classification.status !== "contract-stale") continue; + const readiness = await releases.readiness(brand.id); + if (!readiness.ready) { + result.status = "not-ready"; + result.blockingKeys = readiness.checks + .filter((check) => !check.passed) + .map((check) => check.key); + continue; + } + const published = await releases.publish(brand.id, actor.id, note); + result.status = "republished"; + result.newReleaseVersion = published.version; + } + console.log(JSON.stringify({ apply, actor: actorUsername, results }, null, 2)); + if (apply && results.some((item) => ["content-diverged", "not-ready"].includes(item.status))) + process.exitCode = 1; + } finally { + await prisma.$disconnect(); + } +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch((error) => { + console.error(error instanceof Error ? error.message : String(error)); + process.exitCode = 1; + }); +} diff --git a/deploy/republish-stale-template-contracts.test.mjs b/deploy/republish-stale-template-contracts.test.mjs new file mode 100644 index 0000000..f330300 --- /dev/null +++ b/deploy/republish-stale-template-contracts.test.mjs @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { classifyContractRefresh } from "./republish-stale-template-contracts.mjs"; + +function snapshot(overrides = {}) { + return { + generatedAt: "2026-10-20T00:00:00.000Z", + templateKey: "doors", + templateDefinition: { key: "doors" }, + visualTemplateDefinition: { supportedIndustryTemplates: ["personal", "doors"] }, + visualTemplateManifest: { pages: [] }, + templatePackageManifest: { supportedIndustryTemplates: ["personal", "doors"] }, + installation: { templateVersion: "1.0.6" }, + brand: { name: "林境门作" }, + offerings: [{ name: "入户门" }], + ...overrides, + }; +} + +test("detects a contract-only stale publication", () => { + const current = snapshot({ + generatedAt: "2026-10-20T01:00:00.000Z", + visualTemplateDefinition: { supportedIndustryTemplates: ["doors"] }, + templatePackageManifest: { supportedIndustryTemplates: ["doors"] }, + installation: { templateVersion: "1.0.7" }, + }); + assert.deepEqual(classifyContractRefresh(snapshot(), current), { + status: "contract-stale", + changedKeys: ["visualTemplateDefinition", "templatePackageManifest", "installation"], + blockingKeys: [], + }); +}); + +test("ignores generated timestamps when the publication is current", () => { + assert.deepEqual( + classifyContractRefresh(snapshot(), snapshot({ generatedAt: "2026-10-21T00:00:00.000Z" })), + { status: "current", changedKeys: [], blockingKeys: [] }, + ); +}); + +test("blocks automatic publication when draft content has diverged", () => { + const current = snapshot({ + installation: { templateVersion: "1.0.7" }, + brand: { name: "尚未发布的品牌名" }, + }); + assert.deepEqual(classifyContractRefresh(snapshot(), current), { + status: "content-diverged", + changedKeys: ["installation", "brand"], + blockingKeys: ["brand"], + }); +});