feat: 输出统一运行时工作空间契约

This commit is contained in:
que01 committed 2026-09-22 17:23:52 +08:00
1 parent 40b08b5e1d
commit f9ca5b1fea
4 files changed
+138 -8

No files matched your search

+131 -6
View File
@@ -42,6 +42,12 @@ import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js";
import { getBrandTemplate, resolveCapabilities } from "@personal-brand/config";
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
import { TemplateWorkspaceService } from "./template-workspace.service.js";
import type {
RuntimeInquiryField,
RuntimeOffering,
RuntimeWorkspace,
RuntimeModule,
} from "@personal-brand/types";
@Controller("health")
export class HealthController {
@@ -132,15 +138,93 @@ export class PublicWorkspaceController {
]);
const templateKey = installation?.templateKey ?? brand?.templateKey ?? "personal";
const template = getBrandTemplate(templateKey);
const runtimeModules = modules.map((module) => ({
const runtimeModules: RuntimeModule[] = modules.map((module) => ({
key: module.type,
type: module.type,
title: module.title,
schemaVersion: "1.0",
sortOrder: module.sortOrder,
config: module.config,
type: this.moduleType(module.type),
version: "1.0.0",
propsSchema: `module.${module.type}.v1`,
dataSource: module.type,
config: this.jsonObject(module.config),
source:
module.source === "custom" ? "custom" : module.source === "template" ? "template" : "core",
}));
const runtimeOfferings: RuntimeOffering[] = offerings.map((item) => {
const config = this.jsonObject(item.config);
const sections = Array.isArray(config.sections) ? config.sections : [];
return {
id: item.id,
name: item.name,
summary: item.summary ?? item.description ?? "",
enabled: item.isEnabled,
schemaKey: typeof config.schemaKey === "string" ? config.schemaKey : "offering.default",
schemaVersion: typeof config.schemaVersion === "string" ? config.schemaVersion : "1.0.0",
sections: sections
.filter((section): section is Record<string, unknown> =>
Boolean(section && typeof section === "object"),
)
.map((section) => ({
key: typeof section.key === "string" ? section.key : "section",
type: this.sectionType(section.type),
config: this.jsonObject(section.config),
})),
};
});
const inquiryFields: RuntimeInquiryField[] = this.jsonArray(
inquiryForm?.fieldsSnapshot,
).flatMap((field) => {
if (!field || typeof field !== "object") return [];
const value = field as Record<string, unknown>;
if (typeof value.key !== "string" || typeof value.label !== "string") return [];
const type = [
"text",
"tel",
"number",
"select",
"radio",
"checkbox",
"textarea",
"date",
].includes(String(value.type))
? (value.type as RuntimeInquiryField["type"])
: "text";
return [{ key: value.key, label: value.label, type, required: value.required === true }];
});
const runtimeWorkspace: RuntimeWorkspace = {
schemaVersion: "1.0.0",
brand: (brand ?? {}) as Record<string, unknown>,
navigation: {
tabs: [
{ key: "home", label: "首页", page: "pages/index/index" },
{ key: "offerings", label: "案例与体系", page: "pages/services/index" },
{ key: "profile", label: "主理人与我", page: "pages/bookings/index" },
],
},
modules: runtimeModules,
offerings: runtimeOfferings,
inquiryForms: inquiryForm
? [
{
key: inquiryForm.id,
version: String(inquiryForm.version),
purpose: this.formPurpose(inquiryForm.purpose),
target: inquiryForm.targetType === "offering" ? "offering" : "brand",
submissionAction: this.submissionAction(inquiryForm.submissionAction),
fields: inquiryFields,
},
]
: [],
capabilities: (template ? resolveCapabilities(template.capabilities) : []).map(
(capability) => ({
key: capability.key,
version: capability.version,
config: {},
status: "enabled" as const,
}),
),
theme: { colors: { background: "#F7F5F1", foreground: "#252525", accent: "#8A6846" } },
};
return {
...runtimeWorkspace,
contractVersion: "1.0",
brand,
template: template
@@ -159,6 +243,47 @@ export class PublicWorkspaceController {
inquiryForm,
};
}
private jsonObject(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
private jsonArray(value: unknown): Prisma.JsonValue[] {
return Array.isArray(value) ? value : [];
}
private moduleType(value: string): RuntimeModule["type"] {
const known: RuntimeModule["type"][] = [
"brand-hero",
"founder-card",
"trust-proof",
"offering-list",
"offering-detail",
"gallery",
"timeline",
"faq",
"contact",
"inquiry",
"records",
"custom",
];
return (
known.includes(value as RuntimeModule["type"]) ? value : "custom"
) as RuntimeModule["type"];
}
private sectionType(value: unknown): RuntimeOffering["sections"][number]["type"] {
const known = ["text", "image-list", "timeline", "faq", "specification"] as const;
return known.includes(value as (typeof known)[number])
? (value as (typeof known)[number])
: "text";
}
private formPurpose(value: string): RuntimeWorkspace["inquiryForms"][number]["purpose"] {
return value === "booking" || value === "quote-request" ? value : "inquiry";
}
private submissionAction(
value: string,
): RuntimeWorkspace["inquiryForms"][number]["submissionAction"] {
return value === "create-booking" || value === "create-quote" ? value : "create-inquiry";
}
}
@Controller("public/offerings")