fix: 收敛模板编辑发布契约并还原餐饮页面视觉
This commit is contained in:
1 parent
20bcb43534
commit
f4061a24bd
102 files changed
+4510
-870
No files matched your search
@@ -0,0 +1,157 @@
|
||||
/** 开发餐饮品牌的幂等内容迁移。保留旧记录为草稿,历史快照和提交不变。 */
|
||||
import "reflect-metadata";
|
||||
import fs from "node:fs";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import { PrismaClient } 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 = "餐饮模板结构化内容迁移 v3";
|
||||
try {
|
||||
const brand = await prisma.brandProfile.findUniqueOrThrow({ where: { slug: "default-brand" } });
|
||||
if (
|
||||
brand.templateKey !== "food" ||
|
||||
(brand.config as Record<string, unknown>)?.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 (previous?.note === note) {
|
||||
console.log("Already migrated; no changes");
|
||||
} else {
|
||||
const trust = await prisma.trustContent.findMany({
|
||||
where: { brandId: brand.id },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
const forms = await prisma.inquiryForm.findMany({ where: { brandId: brand.id } });
|
||||
const backup = `/tmp/food-template-content-backup-${Date.now()}.json`;
|
||||
fs.writeFileSync(backup, JSON.stringify({ brand, trust, forms, 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 releaseService = new BrandReleaseService(prisma as PrismaService);
|
||||
const publisher = new BrandReleaseApiService(prisma as PrismaService, releaseService);
|
||||
await prisma.$transaction(async (tx) => {
|
||||
for (const type of ["schedule", "coverage", "process"]) {
|
||||
const candidates = trust.filter((item) => item.type === type);
|
||||
const structured = candidates.find(
|
||||
(item) => item.config && Object.keys(item.config as object).length,
|
||||
);
|
||||
const current = structured ?? candidates[0];
|
||||
const defaults = sample.trustContent.find((item) => item.type === type)!;
|
||||
const config = {
|
||||
...defaults.config,
|
||||
...((structured?.config as Record<string, unknown>) ?? {}),
|
||||
};
|
||||
// 已有的结构化配置保留。旧配送记录中的用户地点保留在自取说明中。
|
||||
if (type === "coverage") {
|
||||
const custom = candidates.find(
|
||||
(item) =>
|
||||
item.title !== defaults.title && item.content !== "示例内容,待品牌主理人完善。",
|
||||
);
|
||||
if (custom && !structured)
|
||||
config.pickupDetail = `${custom.content}。${String(config.pickupDetail)}`;
|
||||
}
|
||||
if (type === "schedule") {
|
||||
if (config.weekdayDetail === "二号校门外小吃商业街 #07 号铺位 · 17:00 - 22:30")
|
||||
config.weekdayDetail = defaults.config!.weekdayDetail;
|
||||
if (config.weekendDetail === "白天承接团体单;晚间 18:00 - 23:00 体育馆南侧流动出摊")
|
||||
config.weekendDetail = defaults.config!.weekendDetail;
|
||||
const custom = candidates.find((item) => !item.config && item.summary && item.content);
|
||||
if (custom) {
|
||||
config.weekdayHours = custom.summary!.replace(/^周一(?:到|至)周五\s*/, "");
|
||||
config.weekendHours = custom.content.replace(/^周六(?:到|至)周(?:天|日)\s*/, "");
|
||||
}
|
||||
}
|
||||
if (current)
|
||||
await tx.trustContent.update({
|
||||
where: { id: current.id },
|
||||
data: {
|
||||
title: defaults.title,
|
||||
content: "",
|
||||
summary: null,
|
||||
config: config as Prisma.InputJsonValue,
|
||||
isPublished: true,
|
||||
},
|
||||
});
|
||||
else
|
||||
await tx.trustContent.create({
|
||||
data: {
|
||||
brandId: brand.id,
|
||||
type,
|
||||
title: defaults.title,
|
||||
content: "",
|
||||
config: config as Prisma.InputJsonValue,
|
||||
isPublished: true,
|
||||
},
|
||||
});
|
||||
await tx.trustContent.updateMany({
|
||||
where: {
|
||||
brandId: brand.id,
|
||||
type,
|
||||
id: {
|
||||
in: candidates.filter((item) => item.id !== current?.id).map((item) => item.id),
|
||||
},
|
||||
},
|
||||
data: { isPublished: false },
|
||||
});
|
||||
}
|
||||
const supportedTypes = getTemplatePackageManifestForVisualKey(
|
||||
"acheng-campus-food",
|
||||
)!.trustContentTypes!.map((item) => item.type);
|
||||
await tx.trustContent.updateMany({
|
||||
where: { brandId: brand.id, type: { notIn: supportedTypes } },
|
||||
data: { isPublished: false },
|
||||
});
|
||||
const form = forms.find((item) => item.targetType === "brand" && item.isEnabled);
|
||||
if (!form) throw new Error("Expected existing brand inquiry form");
|
||||
const oldFields = form.fieldsSnapshot as Array<Record<string, unknown>>;
|
||||
const keys = new Set(sample.fields.map((field) => field.key));
|
||||
const fields = [
|
||||
...sample.fields.map((field) => ({
|
||||
...field,
|
||||
...oldFields.find((old) => old.key === field.key),
|
||||
})),
|
||||
...oldFields.filter((field) => !keys.has(String(field.key))),
|
||||
];
|
||||
await tx.inquiryForm.update({
|
||||
where: { id: form.id },
|
||||
data: {
|
||||
name: sample.inquiryFormName,
|
||||
fieldsSnapshot: fields as Prisma.InputJsonValue,
|
||||
version: { increment: 1 },
|
||||
},
|
||||
});
|
||||
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 releaseService.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("Migrated and published development food brand; old content retained as drafts");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
Reference in new issue
Block a user