161 lines
5.3 KiB
TypeScript
161 lines
5.3 KiB
TypeScript
import type { CapabilityKey } from "./template-keys.js";
|
|
|
|
export interface WorkflowDefinition {
|
|
key: string;
|
|
version: string;
|
|
initialState: string;
|
|
transitions: Record<string, readonly string[]>;
|
|
}
|
|
|
|
export interface CapabilityDefinition {
|
|
key: CapabilityKey;
|
|
version: string;
|
|
workflowKey?: string;
|
|
}
|
|
|
|
export type CapabilityDisablePolicy = "allowed" | "disabled";
|
|
|
|
export interface CapabilityMetadata {
|
|
displayName: string;
|
|
description: string;
|
|
impact: string;
|
|
optional: boolean;
|
|
disablePolicy: CapabilityDisablePolicy;
|
|
allowedPages: readonly string[];
|
|
requiredModules: readonly string[];
|
|
purposeBindings: readonly string[];
|
|
entryNavigationKey?: string;
|
|
}
|
|
|
|
export type CapabilityConfigFieldKey = "entryLabel" | "submitHint" | "disabledHint";
|
|
|
|
export interface CapabilityConfigField {
|
|
key: CapabilityConfigFieldKey;
|
|
label: string;
|
|
input: "text";
|
|
required: boolean;
|
|
}
|
|
|
|
export interface CapabilityMetadataDefinition extends CapabilityMetadata {
|
|
configFields: readonly CapabilityConfigField[];
|
|
}
|
|
|
|
export const workflowRegistry = {
|
|
"booking.basic": {
|
|
key: "booking.basic",
|
|
version: "1.0.0",
|
|
initialState: "received",
|
|
transitions: {
|
|
received: ["contacting", "cancelled"],
|
|
contacting: ["confirmed", "cancelled"],
|
|
confirmed: ["completed", "cancelled"],
|
|
completed: [],
|
|
cancelled: [],
|
|
},
|
|
},
|
|
} as const satisfies Record<string, WorkflowDefinition>;
|
|
|
|
export const capabilityRegistry = {
|
|
offerings: {
|
|
key: "offerings",
|
|
version: "1.0.0",
|
|
displayName: "业务提供项",
|
|
description: "展示品牌的服务与产品。",
|
|
impact: "停用会移除业务提供项展示入口。",
|
|
optional: false,
|
|
disablePolicy: "disabled",
|
|
allowedPages: ["home", "offerings", "detail"],
|
|
requiredModules: ["offering-list", "offering-detail"],
|
|
purposeBindings: [],
|
|
},
|
|
inquiry: {
|
|
key: "inquiry",
|
|
version: "1.0.0",
|
|
displayName: "通用咨询",
|
|
description: "接收客户的一对一合作咨询。",
|
|
impact: "停用后所有通用咨询表单都无法提交。",
|
|
optional: false,
|
|
disablePolicy: "allowed",
|
|
allowedPages: ["inquiry", "success"],
|
|
requiredModules: ["inquiry", "records", "success-summary"],
|
|
purposeBindings: ["inquiry"],
|
|
entryNavigationKey: "inquiry",
|
|
},
|
|
booking: { key: "booking", version: "1.0.0", workflowKey: "booking.basic" },
|
|
"quote-request": {
|
|
key: "quote-request",
|
|
version: "1.0.0",
|
|
displayName: "报价咨询",
|
|
description: "可选安装的报价请求链路。",
|
|
impact: "安装后可提交报价咨询;移除后报价表单不再接收提交。",
|
|
optional: true,
|
|
disablePolicy: "allowed",
|
|
allowedPages: ["inquiry", "success"],
|
|
requiredModules: ["inquiry", "records", "success-summary"],
|
|
purposeBindings: ["quote-request"],
|
|
},
|
|
} as const satisfies Record<CapabilityKey, CapabilityDefinition & Partial<CapabilityMetadata>>;
|
|
|
|
export const capabilityConfigFields = {
|
|
entryLabel: { key: "entryLabel", label: "入口文案", input: "text", required: false },
|
|
submitHint: { key: "submitHint", label: "提交提示", input: "text", required: false },
|
|
disabledHint: { key: "disabledHint", label: "停用提示", input: "text", required: false },
|
|
} as const satisfies Record<CapabilityConfigFieldKey, CapabilityConfigField>;
|
|
|
|
export function capabilityMetadata(key: CapabilityKey): CapabilityMetadataDefinition | null {
|
|
const definition = capabilityRegistry[key];
|
|
if (!("disablePolicy" in definition)) return null;
|
|
return { ...definition, configFields: Object.values(capabilityConfigFields) };
|
|
}
|
|
|
|
export function validateCapabilityConfig(
|
|
capabilityKey: CapabilityKey,
|
|
config: unknown,
|
|
): Record<string, string> | null {
|
|
if (config === undefined || config === null) return {};
|
|
if (typeof config !== "object" || Array.isArray(config)) return null;
|
|
const metadata = capabilityMetadata(capabilityKey);
|
|
if (!metadata) return {};
|
|
const source = config as Record<string, unknown>;
|
|
if (Object.keys(source).some((key) => !metadata.configFields.some((field) => field.key === key)))
|
|
return null;
|
|
const errors: Record<string, string> = {};
|
|
for (const [key, value] of Object.entries(source)) {
|
|
const field = capabilityConfigFields[key as CapabilityConfigFieldKey];
|
|
const message =
|
|
value === undefined || value === null || (typeof value === "string" && !value.trim())
|
|
? "不能为空"
|
|
: typeof value !== "string"
|
|
? "必须是字符串"
|
|
: value.length > 80
|
|
? "不能超过 80 个字符"
|
|
: "";
|
|
if (message) errors[key] = `${field?.label ?? key}${message}`;
|
|
}
|
|
return errors;
|
|
}
|
|
|
|
export const capabilityPlugins = {
|
|
inquiry: {
|
|
key: "inquiry",
|
|
version: "1.0.0",
|
|
displayName: "通用咨询",
|
|
purpose: "inquiry",
|
|
requiredModules: ["inquiry", "records", "success-summary"],
|
|
apiContract: "/customer/inquiry-submissions",
|
|
configSchema: "capability.inquiry.v1",
|
|
purposeBindings: ["inquiry"],
|
|
entryNavigationKey: "inquiry",
|
|
},
|
|
"quote-request": {
|
|
key: "quote-request",
|
|
version: "1.0.0",
|
|
displayName: "报价咨询",
|
|
purpose: "quote-request",
|
|
requiredModules: ["inquiry", "records", "success-summary"],
|
|
apiContract: "/customer/inquiry-submissions",
|
|
configSchema: "capability.quote-request.v1",
|
|
purposeBindings: ["quote-request"],
|
|
},
|
|
} as const;
|