215 lines
7.6 KiB
TypeScript
215 lines
7.6 KiB
TypeScript
import type { BrandReleaseSnapshot } from "../../brand-release.service.js";
|
|
import type { Prisma } from "@prisma/client";
|
|
import {
|
|
capabilityMetadata,
|
|
capabilityPlugins,
|
|
type BrandTemplateKey,
|
|
resolveCapabilities,
|
|
} from "@personal-brand/config";
|
|
import type { RuntimeTrustItem, RuntimeWorkspace } from "@personal-brand/types";
|
|
import {
|
|
projectBrandAliases,
|
|
projectTemplateData,
|
|
projectTrustItem,
|
|
projectTrustPresentation,
|
|
} from "@personal-brand/types";
|
|
import {
|
|
asArray,
|
|
asRecord,
|
|
asString,
|
|
capabilityStatus,
|
|
formPurpose,
|
|
submissionAction,
|
|
toRuntimeInquiryFields,
|
|
toRuntimeOffering,
|
|
} from "./public-workspace.runtime-mapper.js";
|
|
|
|
type ReleaseMetadata = { version: number; createdAt: Date };
|
|
|
|
type PublicWorkspaceResponse = Omit<RuntimeWorkspace, "brand"> & {
|
|
contractVersion: string;
|
|
brand: Record<string, unknown> | null;
|
|
template: {
|
|
key: string;
|
|
version: string;
|
|
name: string;
|
|
} | null;
|
|
visualTemplate: {
|
|
key: string;
|
|
version: string;
|
|
name: string;
|
|
supportedIndustryTemplates: BrandTemplateKey[];
|
|
variants: Record<string, string>;
|
|
} | null;
|
|
visualTemplateManifest: BrandReleaseSnapshot["visualTemplateManifest"];
|
|
};
|
|
|
|
export function createUnpublishedWorkspace(): PublicWorkspaceResponse {
|
|
return {
|
|
contractVersion: "1.0",
|
|
schemaVersion: "1.0.0",
|
|
publication: { status: "unpublished", version: null },
|
|
brand: null,
|
|
template: null,
|
|
visualTemplate: null,
|
|
visualTemplateManifest: null,
|
|
offerings: [],
|
|
inquiryForms: [],
|
|
trustItems: [],
|
|
capabilities: [],
|
|
trustPresentation: {},
|
|
navigation: { tabs: [] },
|
|
templateData: {},
|
|
plugins: [],
|
|
theme: { colors: {} },
|
|
};
|
|
}
|
|
|
|
export function assemblePublicWorkspace(input: {
|
|
release: ReleaseMetadata | null;
|
|
snapshot: BrandReleaseSnapshot;
|
|
draftSnapshot: BrandReleaseSnapshot | null;
|
|
}): PublicWorkspaceResponse {
|
|
const { release, snapshot, draftSnapshot } = input;
|
|
const releaseSnapshot = snapshot;
|
|
const brandData = asRecord(releaseSnapshot.brand);
|
|
const offeringsData = asArray(releaseSnapshot.offerings).filter(
|
|
(item) => asRecord(item).isEnabled === true,
|
|
) as Array<Record<string, unknown>>;
|
|
const enabledOfferingIds = new Set(offeringsData.map((item) => item.id));
|
|
const formsData = asArray(releaseSnapshot.forms).filter((item) => {
|
|
const form = asRecord(item);
|
|
return (
|
|
form.isEnabled === true &&
|
|
(form.offeringId === null || enabledOfferingIds.has(form.offeringId))
|
|
);
|
|
}) as Array<Record<string, unknown>>;
|
|
const trustData = asArray(releaseSnapshot.trustContents).filter(
|
|
(item) => asRecord(item).isPublished === true,
|
|
) as Array<Record<string, unknown>>;
|
|
const installationData = asRecord(releaseSnapshot.installation);
|
|
const templateKey = asString(releaseSnapshot.templateKey);
|
|
const template = releaseSnapshot.templateDefinition;
|
|
const capabilityData = asArray(releaseSnapshot.capabilities) as Array<Record<string, unknown>>;
|
|
const capabilityRecord = (item: Record<string, unknown>, fallbackVersion = "1.0.0") => {
|
|
const key = asString(item.capabilityKey) || asString(item.key);
|
|
const metadata = capabilityMetadata(key as never);
|
|
return {
|
|
key,
|
|
version: asString(item.capabilityVersion, fallbackVersion),
|
|
status: capabilityStatus(item.status),
|
|
config: asRecord(item.config),
|
|
...(metadata ? { metadata: { ...metadata } } : {}),
|
|
};
|
|
};
|
|
const capabilities = capabilityData.length
|
|
? capabilityData.map((item) => capabilityRecord(item))
|
|
: template
|
|
? resolveCapabilities(template.capabilities).map((item) =>
|
|
capabilityRecord({ ...item, config: {} }),
|
|
)
|
|
: [];
|
|
const brandConfig = asRecord(brandData.config);
|
|
const visualTemplate = releaseSnapshot.visualTemplateDefinition;
|
|
const visualTemplateManifest = releaseSnapshot.visualTemplateManifest;
|
|
const templatePackageManifest = releaseSnapshot.templatePackageManifest;
|
|
const visualTemplateKey = asString(templatePackageManifest?.visualTemplateKey);
|
|
if (
|
|
!templateKey ||
|
|
!visualTemplateKey ||
|
|
!templatePackageManifest ||
|
|
!template ||
|
|
!visualTemplate ||
|
|
!visualTemplateManifest
|
|
)
|
|
throw new Error("发布快照缺少统一模板契约字段");
|
|
const categoryOptions = new Map(
|
|
(templatePackageManifest?.admin.sections ?? [])
|
|
.filter((section) => section.target === "offering")
|
|
.flatMap((section) => section.fields)
|
|
.filter((field) => field.binding === "offering.config.categoryKey")
|
|
.flatMap((field) => field.options ?? [])
|
|
.map((option) => [option.value, option.label]),
|
|
);
|
|
const runtimeBrand = projectBrandAliases(brandData, brandConfig);
|
|
const templateData = projectTemplateData(
|
|
brandConfig,
|
|
(templatePackageManifest?.templateFields ?? []).map((field) => field.key),
|
|
);
|
|
const trustDeclarations = templatePackageManifest?.trustContentTypes ?? [];
|
|
const trustItems: RuntimeTrustItem[] = trustData.flatMap((raw) => {
|
|
const item = projectTrustItem(asRecord(raw));
|
|
return item && trustDeclarations.some((declaration) => declaration.type === item.type)
|
|
? [item]
|
|
: [];
|
|
});
|
|
const runtimeWorkspace: RuntimeWorkspace = {
|
|
schemaVersion: "1.0.0",
|
|
publication: {
|
|
status: draftSnapshot ? "draft" : "published",
|
|
version: draftSnapshot ? null : (release?.version ?? null),
|
|
...(!draftSnapshot && release ? { publishedAt: release.createdAt.toISOString() } : {}),
|
|
},
|
|
brand: runtimeBrand,
|
|
templateData,
|
|
navigation: {
|
|
tabs:
|
|
templatePackageManifest?.navigation.map((tab) => ({
|
|
key: tab.key,
|
|
label: tab.label,
|
|
page: templatePackageManifest.entrypoints[tab.page] ?? tab.page,
|
|
})) ?? [],
|
|
},
|
|
offerings: offeringsData.map((item) => toRuntimeOffering(item, categoryOptions)),
|
|
inquiryForms: formsData.map((raw) => {
|
|
const form = asRecord(raw);
|
|
return {
|
|
key: asString(form.id),
|
|
version:
|
|
typeof form.version === "number" ? String(form.version) : asString(form.version, "1"),
|
|
purpose: formPurpose(asString(form.purpose, "inquiry")),
|
|
target: form.targetType === "offering" ? "offering" : "brand",
|
|
...(typeof form.offeringId === "string" ? { targetId: form.offeringId } : {}),
|
|
submissionAction: submissionAction(asString(form.submissionAction, "create-inquiry")),
|
|
fields: toRuntimeInquiryFields(form.fieldsSnapshot as Prisma.JsonValue),
|
|
};
|
|
}),
|
|
trustItems,
|
|
trustPresentation: projectTrustPresentation(trustItems, trustDeclarations),
|
|
capabilities,
|
|
plugins: capabilities
|
|
.filter((capability) => capability.status === "enabled")
|
|
.map((capability) => capabilityPlugins[capability.key as keyof typeof capabilityPlugins])
|
|
.filter(Boolean)
|
|
.map((plugin) => ({
|
|
...plugin,
|
|
status: "enabled" as const,
|
|
config: capabilities.find((capability) => capability.key === plugin.key)?.config ?? {},
|
|
})),
|
|
theme: visualTemplate.theme,
|
|
...(visualTemplate.layout ? { layout: visualTemplate.layout } : {}),
|
|
};
|
|
return {
|
|
...runtimeWorkspace,
|
|
contractVersion: "1.0",
|
|
brand: runtimeBrand,
|
|
template: template
|
|
? {
|
|
key: template.key,
|
|
version: asString(installationData.templateVersion, template.version),
|
|
name: template.name,
|
|
}
|
|
: null,
|
|
visualTemplate: visualTemplate
|
|
? {
|
|
key: visualTemplate.key,
|
|
version: visualTemplate.version,
|
|
name: visualTemplate.name,
|
|
supportedIndustryTemplates: [...visualTemplate.supportedIndustryTemplates],
|
|
variants: visualTemplate.variants,
|
|
}
|
|
: null,
|
|
visualTemplateManifest,
|
|
};
|
|
}
|