refactor: 拆分业务、信任内容与公共工作台模块
This commit is contained in:
1 parent
357f271639
commit
466dc66907
33 files changed
+4950
-4500
No files matched your search
@@ -0,0 +1,213 @@
|
||||
import type { BrandReleaseSnapshot } from "../../brand-release.service.js";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import {
|
||||
capabilityPlugins,
|
||||
type BrandTemplateKey,
|
||||
getBrandTemplate,
|
||||
getDefaultVisualTemplateKey,
|
||||
getTemplatePackageManifestForVisualKey,
|
||||
getVisualTemplate,
|
||||
getVisualTemplateManifest,
|
||||
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) ||
|
||||
asString(installationData.templateKey) ||
|
||||
asString(brandData.templateKey) ||
|
||||
"personal";
|
||||
const template = releaseSnapshot.templateDefinition ?? getBrandTemplate(templateKey);
|
||||
const capabilityData = asArray(releaseSnapshot.capabilities) as Array<Record<string, unknown>>;
|
||||
const capabilities = capabilityData.length
|
||||
? capabilityData.map((item) => ({
|
||||
key: asString(item.capabilityKey),
|
||||
version: asString(item.capabilityVersion, "1.0.0"),
|
||||
status: capabilityStatus(item.status),
|
||||
config: asRecord(item.config),
|
||||
}))
|
||||
: template
|
||||
? resolveCapabilities(template.capabilities).map((item) => ({
|
||||
key: item.key,
|
||||
version: item.version,
|
||||
status: "enabled" as const,
|
||||
config: {},
|
||||
}))
|
||||
: [];
|
||||
const brandConfig = asRecord(brandData.config);
|
||||
const visualTemplateKey =
|
||||
asString(brandConfig.visualTemplateKey) || getDefaultVisualTemplateKey(templateKey);
|
||||
const visualTemplate =
|
||||
releaseSnapshot.visualTemplateDefinition ?? getVisualTemplate(visualTemplateKey);
|
||||
const visualTemplateManifest =
|
||||
releaseSnapshot.visualTemplateManifest ?? getVisualTemplateManifest(visualTemplateKey);
|
||||
const templatePackageManifest =
|
||||
releaseSnapshot.templatePackageManifest ??
|
||||
getTemplatePackageManifestForVisualKey(visualTemplateKey);
|
||||
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: {} })),
|
||||
theme: visualTemplate?.theme ?? {
|
||||
colors: { background: "#F7F5F1", foreground: "#252525", accent: "#8A6846" },
|
||||
},
|
||||
...(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,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user