86 lines
4.0 KiB
TypeScript
86 lines
4.0 KiB
TypeScript
/** 仅恢复开发餐饮品牌的原型视觉槽位,不覆盖已有结构化编辑和发布历史。 */
|
|
import "reflect-metadata";
|
|
import fs from "node:fs";
|
|
import { PrismaClient, type Prisma } from "@prisma/client";
|
|
import { getTemplatePackageManifestForVisualKey } from "@personal-brand/config";
|
|
import { BrandReleaseService } from "../src/brand-release.service.js";
|
|
import { BrandReleaseApiService } from "../src/brand-release-api.service.js";
|
|
import type { PrismaService } from "../src/prisma.service.js";
|
|
const prisma = new PrismaClient();
|
|
const note = "餐饮主理人原型视觉槽位迁移 v1";
|
|
try {
|
|
const brand = await prisma.brandProfile.findUniqueOrThrow({ where: { slug: "default-brand" } });
|
|
const config = (brand.config ?? {}) as Record<string, unknown>;
|
|
if (brand.templateKey !== "food" || config.visualTemplateKey !== "acheng-campus-food")
|
|
throw new Error("Migration is restricted to the development food brand");
|
|
const previous = await prisma.brandRelease.findFirst({
|
|
where: { brandId: brand.id },
|
|
orderBy: { version: "desc" },
|
|
});
|
|
if (await prisma.brandRelease.findFirst({ where: { brandId: brand.id, note } })) {
|
|
console.log("Already migrated; no changes");
|
|
} else {
|
|
const trust = await prisma.trustContent.findMany({
|
|
where: { brandId: brand.id },
|
|
orderBy: [{ sortOrder: "asc" }, { createdAt: "asc" }],
|
|
});
|
|
const backup = `/tmp/food-owner-visual-backup-${Date.now()}.json`;
|
|
fs.writeFileSync(backup, JSON.stringify({ brand, trust, previous }, null, 2), { mode: 0o600 });
|
|
console.log(`Backup: ${backup}`);
|
|
if (!process.argv.includes("--apply"))
|
|
console.log("Dry run; pass --apply to migrate and publish");
|
|
else {
|
|
const sample =
|
|
getTemplatePackageManifestForVisualKey("acheng-campus-food")!.sampleContent!.food!;
|
|
const service = new BrandReleaseService(prisma as PrismaService);
|
|
const publisher = new BrandReleaseApiService(prisma as PrismaService, service);
|
|
await prisma.$transaction(async (tx) => {
|
|
await tx.brandProfile.update({
|
|
where: { id: brand.id },
|
|
data: { avatarUrl: brand.avatarUrl || "/assets/owner/portrait.jpg" },
|
|
});
|
|
for (const type of ["credential", "case", "promise"]) {
|
|
const item = trust.find((item) => item.type === type && item.isPublished);
|
|
if (!item) continue;
|
|
const defaults = sample.trustContent.find((item) => item.type === type)!;
|
|
const existing = (item.config ?? {}) as Record<string, unknown>;
|
|
// 只补迁移前已确认的开发样例内容;用户改写的正文不能被模板样例遮挡。
|
|
const known =
|
|
type === "credential"
|
|
? item.content === "当天采购,现做现出餐;食具每日清洁消毒,按约定标注和分装。"
|
|
: item.content === defaults.content;
|
|
if (!known) continue;
|
|
await tx.trustContent.update({
|
|
where: { id: item.id },
|
|
data: {
|
|
config: { ...defaults.config, ...existing } as Prisma.InputJsonValue,
|
|
},
|
|
});
|
|
}
|
|
const readiness = await publisher.readiness(brand.id, tx);
|
|
if (!readiness.ready)
|
|
throw new Error(JSON.stringify(readiness.checks.filter((check) => !check.passed)));
|
|
const snapshot = await service.buildSnapshot(brand.id, tx);
|
|
await tx.brandRelease.updateMany({
|
|
where: { brandId: brand.id, status: "published" },
|
|
data: { status: "superseded" },
|
|
});
|
|
await tx.brandRelease.create({
|
|
data: {
|
|
brandId: brand.id,
|
|
version: (previous?.version ?? 0) + 1,
|
|
actorId: previous?.actorId ?? "content-migration",
|
|
note,
|
|
snapshot: snapshot as unknown as Prisma.InputJsonValue,
|
|
},
|
|
});
|
|
});
|
|
console.log(
|
|
"Owner visual slots migrated and published; existing edits and historical releases retained",
|
|
);
|
|
}
|
|
}
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
}
|