fix: 收敛模板编辑发布契约并还原餐饮页面视觉
This commit is contained in:
1 parent
20bcb43534
commit
f4061a24bd
102 files changed
+4510
-870
No files matched your search
@@ -0,0 +1,85 @@
|
||||
/** 仅恢复开发餐饮品牌的原型视觉槽位,不覆盖已有结构化编辑和发布历史。 */
|
||||
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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": { "rootDir": "..", "noEmit": true },
|
||||
"include": ["*.ts", "../src/**/*.ts", "../prisma/seed.ts"]
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import fs from "node:fs";
|
||||
import vm from "node:vm";
|
||||
import assert from "node:assert/strict";
|
||||
process.env.NODE_ENV = "test";
|
||||
const { createApp } = await import("../dist/main.js");
|
||||
const app = await createApp();
|
||||
try {
|
||||
await app.init();
|
||||
const response = await app
|
||||
.getHttpAdapter()
|
||||
.getInstance()
|
||||
.inject({ method: "GET", url: "/public/workspace?brandSlug=default-brand" });
|
||||
assert.equal(response.statusCode, 200);
|
||||
const workspace = JSON.parse(response.body) as Record<string, unknown>;
|
||||
const trustItems = workspace.trustItems as { id?: string }[];
|
||||
assert.ok(trustItems.every((item) => typeof item.id === "string"));
|
||||
const live = await fetch(
|
||||
`http://127.0.0.1:${process.env.API_PORT ?? 3001}/public/workspace?brandSlug=default-brand`,
|
||||
);
|
||||
assert.equal(live.status, 200);
|
||||
const liveWorkspace = (await live.json()) as Record<string, unknown>;
|
||||
assert.deepEqual(liveWorkspace.trustItems, workspace.trustItems);
|
||||
assert.deepEqual(liveWorkspace.inquiryForms, workspace.inquiryForms);
|
||||
fs.writeFileSync("/tmp/food-template-public-workspace.json", JSON.stringify(workspace, null, 2));
|
||||
let page:
|
||||
| {
|
||||
data: Record<string, unknown>;
|
||||
load: () => Promise<void>;
|
||||
setData: (value: Record<string, unknown>) => void;
|
||||
}
|
||||
| undefined;
|
||||
vm.runInNewContext(
|
||||
fs.readFileSync("../../dist/miniapp/default-brand/pages/stall-service/index.js", "utf8"),
|
||||
{
|
||||
Page(value: typeof page) {
|
||||
page = value;
|
||||
},
|
||||
require(name: string) {
|
||||
return name.includes("runtime-workspace")
|
||||
? { loadWorkspaceWithFallback: () => Promise.resolve(workspace) }
|
||||
: { openPage() {} };
|
||||
},
|
||||
},
|
||||
);
|
||||
assert.ok(page);
|
||||
page.setData = (data) => Object.assign(page!.data, data);
|
||||
await page.load();
|
||||
const data = page.data as {
|
||||
schedule: { rows: unknown[] };
|
||||
coverage: { config: { pickupDetail: string; deliveryDetail: string } };
|
||||
process: { steps: unknown[] };
|
||||
};
|
||||
assert.equal(data.schedule.rows.length, 2);
|
||||
assert.ok(data.coverage.config.pickupDetail && data.coverage.config.deliveryDetail);
|
||||
assert.equal(data.process.steps.length, 4);
|
||||
const forms = workspace.inquiryForms as { fields: { key: string }[] }[];
|
||||
for (const key of ["type", "people", "date", "budget", "delivery", "remark", "phone"])
|
||||
assert.ok(forms.some((form) => form.fields.some((field) => field.key === key)));
|
||||
fs.writeFileSync("/tmp/food-template-service-page-data.json", JSON.stringify(page.data, null, 2));
|
||||
let inquiry:
|
||||
| {
|
||||
data: Record<string, unknown>;
|
||||
onLoad: (options: Record<string, unknown>) => Promise<void>;
|
||||
setData: (value: Record<string, unknown>) => void;
|
||||
}
|
||||
| undefined;
|
||||
vm.runInNewContext(
|
||||
fs.readFileSync("../../dist/miniapp/default-brand/pages/inquiry/index.js", "utf8"),
|
||||
{
|
||||
Page(value: typeof inquiry) {
|
||||
inquiry = value;
|
||||
},
|
||||
require(name: string) {
|
||||
return name.includes("runtime-workspace")
|
||||
? { loadWorkspaceWithFallback: () => Promise.resolve(workspace) }
|
||||
: {};
|
||||
},
|
||||
},
|
||||
);
|
||||
assert.ok(inquiry);
|
||||
inquiry.setData = (value) => Object.assign(inquiry!.data, value);
|
||||
await inquiry.onLoad({});
|
||||
const inquiryFields = inquiry.data.fields as {
|
||||
key: string;
|
||||
useChips: boolean;
|
||||
halfWidth: boolean;
|
||||
}[];
|
||||
assert.equal(inquiryFields.find((field) => field.key === "type")?.useChips, true);
|
||||
assert.equal(inquiryFields.filter((field) => field.halfWidth).length, 4);
|
||||
fs.writeFileSync(
|
||||
"/tmp/food-template-inquiry-page-data.json",
|
||||
JSON.stringify(inquiry.data, null, 2),
|
||||
);
|
||||
console.log(
|
||||
"Actual database -> published API -> generated page: 2 schedule cards, 2 delivery columns, 4 steps, food inquiry fields verified",
|
||||
);
|
||||
const doorsResponse = await app
|
||||
.getHttpAdapter()
|
||||
.getInstance()
|
||||
.inject({ method: "GET", url: "/public/workspace?brandSlug=linjing-doors" });
|
||||
assert.equal(doorsResponse.statusCode, 200);
|
||||
const doors = JSON.parse(doorsResponse.body) as {
|
||||
trustItems: { type: string; content: string; config: Record<string, unknown> }[];
|
||||
trustPresentation: Record<string, unknown>;
|
||||
};
|
||||
assert.equal(
|
||||
doors.trustItems.some((item: { type: string }) => item.type === "profile"),
|
||||
false,
|
||||
);
|
||||
const credentials = doors.trustItems.filter(
|
||||
(item: { type: string }) => item.type === "credential",
|
||||
);
|
||||
const primary = credentials.find(
|
||||
(item: { config: object }) => Object.keys(item.config).length > 0,
|
||||
);
|
||||
assert.ok(primary);
|
||||
assert.deepEqual(doors.trustPresentation.stats, primary.config.stats);
|
||||
const liveDoors = await fetch(
|
||||
`http://127.0.0.1:${process.env.API_PORT ?? 3001}/public/workspace?brandSlug=linjing-doors`,
|
||||
);
|
||||
assert.equal(liveDoors.status, 200);
|
||||
const liveDoorsWorkspace = (await liveDoors.json()) as Record<string, unknown>;
|
||||
assert.deepEqual(liveDoorsWorkspace.trustPresentation, doors.trustPresentation);
|
||||
type DoorsPage = {
|
||||
data: { trustPresentation: { bio: string } };
|
||||
setData: (value: object) => void;
|
||||
onLoad: () => void;
|
||||
};
|
||||
let doorsPage: DoorsPage | undefined;
|
||||
vm.runInNewContext(
|
||||
fs.readFileSync("../../dist/miniapp/linjing-doors/pages/index/index.js", "utf8"),
|
||||
{
|
||||
Page(value: DoorsPage) {
|
||||
doorsPage = value;
|
||||
},
|
||||
require() {
|
||||
return { loadWorkspaceWithFallback: () => Promise.resolve(doors) };
|
||||
},
|
||||
wx: { setNavigationBarTitle() {} },
|
||||
},
|
||||
);
|
||||
assert.ok(doorsPage);
|
||||
doorsPage.setData = (value: object) => Object.assign(doorsPage!.data, value);
|
||||
doorsPage.onLoad();
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal(doorsPage.data.trustPresentation.bio, primary.content);
|
||||
fs.writeFileSync("/tmp/linjing-template-page-data.json", JSON.stringify(doorsPage.data, null, 2));
|
||||
console.log(
|
||||
"Linjing published API -> generated page: declared credential stats and biography verified; obsolete profile excluded",
|
||||
);
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
Reference in new issue
Block a user