242 lines
6.4 KiB
TypeScript
242 lines
6.4 KiB
TypeScript
import type { ModuleResponse } from "@/features/content/types/content.types";
|
|
import type { TemplateReference } from "@/features/content/types/content-reference.types";
|
|
|
|
/**
|
|
* Client-only fallbacks for presentation and commerce values that are not yet
|
|
* guaranteed by the modules API. Business/content metadata such as module
|
|
* name, description and category must come from the API response.
|
|
*/
|
|
export interface ModuleVisualConfig {
|
|
icon: string;
|
|
color: string;
|
|
isDefaultFree: boolean;
|
|
planCode: string;
|
|
entryTemplateCode?: string;
|
|
}
|
|
|
|
export interface ModuleIndexItem {
|
|
module: ModuleResponse;
|
|
reference: TemplateReference;
|
|
title: string;
|
|
description: string;
|
|
icon: string;
|
|
color: string;
|
|
category: string;
|
|
isDefaultFree: boolean;
|
|
planCode: string;
|
|
}
|
|
|
|
const MODULE_VISUALS: Record<string, ModuleVisualConfig> = {
|
|
m0001: {
|
|
icon: "water-outline",
|
|
color: "#2563EB",
|
|
isDefaultFree: true,
|
|
planCode: "POINTS_ACU",
|
|
},
|
|
m0002: {
|
|
icon: "ear-outline",
|
|
color: "#F59E0B",
|
|
isDefaultFree: false,
|
|
planCode: "POINTS_EAR",
|
|
},
|
|
m0003: {
|
|
icon: "accessibility-outline",
|
|
color: "#EF4444",
|
|
isDefaultFree: false,
|
|
planCode: "POINTS_SCALP",
|
|
},
|
|
m0011: {
|
|
icon: "git-network-outline",
|
|
color: "#7C3AED",
|
|
isDefaultFree: true,
|
|
planCode: "PATTERN_CORE",
|
|
},
|
|
m0012: {
|
|
icon: "pulse-outline",
|
|
color: "#06B6D4",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
},
|
|
m0013: {
|
|
icon: "water-outline",
|
|
color: "#3B82F6",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
},
|
|
m0014: {
|
|
icon: "flame-outline",
|
|
color: "#F59E0B",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
},
|
|
m0015: {
|
|
icon: "layers-outline",
|
|
color: "#10B981",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
},
|
|
m0016: {
|
|
icon: "trending-up-outline",
|
|
color: "#EF4444",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
},
|
|
m0017: {
|
|
icon: "thermometer-outline",
|
|
color: "#EC4899",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
entryTemplateCode: "m0017_ct0001",
|
|
},
|
|
m0018: {
|
|
icon: "pulse-outline",
|
|
color: "#06B6D4",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
entryTemplateCode: "m0018_ct0001",
|
|
},
|
|
m0019: {
|
|
icon: "flower-outline",
|
|
color: "#6366F1",
|
|
isDefaultFree: false,
|
|
planCode: "PATTERN_CORE",
|
|
},
|
|
m0020: {
|
|
icon: "git-branch-outline",
|
|
color: "#8B5CF6",
|
|
isDefaultFree: false,
|
|
planCode: "POINTS_ACU",
|
|
},
|
|
m0031: {
|
|
icon: "nutrition-outline",
|
|
color: "#10B981",
|
|
isDefaultFree: true,
|
|
planCode: "NUTRITION_CORE",
|
|
},
|
|
m0041: {
|
|
icon: "body-outline",
|
|
color: "#8B5CF6",
|
|
isDefaultFree: false,
|
|
planCode: "LIFETIME_ALL",
|
|
},
|
|
m0042: {
|
|
icon: "woman-outline",
|
|
color: "#EC4899",
|
|
isDefaultFree: false,
|
|
planCode: "THERAPEUTICS_GYNAE",
|
|
},
|
|
};
|
|
|
|
function asNonBlankString(value: unknown): string | null {
|
|
if (typeof value !== "string") return null;
|
|
const normalized = value.trim();
|
|
return normalized.length > 0 ? normalized : null;
|
|
}
|
|
|
|
function readMetadataString(module: ModuleResponse, key: string): string | null {
|
|
return asNonBlankString(module.metadata?.[key]);
|
|
}
|
|
|
|
function readMetadataBoolean(module: ModuleResponse, key: string): boolean | null {
|
|
const value = module.metadata?.[key];
|
|
if (typeof value === "boolean") return value;
|
|
if (typeof value === "string") {
|
|
const normalized = value.trim().toLowerCase();
|
|
if (normalized === "true") return true;
|
|
if (normalized === "false") return false;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function resolveModuleCategory(module: ModuleResponse): string {
|
|
const explicitCategory = readMetadataString(module, "category");
|
|
if (explicitCategory) return explicitCategory;
|
|
|
|
const kind = readMetadataString(module, "kind")?.toLowerCase();
|
|
|
|
switch (kind) {
|
|
case "point":
|
|
case "points":
|
|
return "Points";
|
|
|
|
case "pattern":
|
|
case "patterns":
|
|
case "five-elements":
|
|
case "five_elements":
|
|
return "Patterns";
|
|
|
|
case "nutrition":
|
|
return "Nutrition";
|
|
|
|
case "therapeutic":
|
|
case "therapeutics":
|
|
return "Therapeutics";
|
|
|
|
default:
|
|
return "Other";
|
|
}
|
|
}
|
|
|
|
export function resolveModuleEntryTemplateCode(module: ModuleResponse): string {
|
|
const visual = MODULE_VISUALS[module.code.toLowerCase()];
|
|
if (visual?.entryTemplateCode) return visual.entryTemplateCode.toLowerCase();
|
|
|
|
const defaultTemplateCode = asNonBlankString(module.default_template_code);
|
|
if (defaultTemplateCode) return defaultTemplateCode.toLowerCase();
|
|
|
|
// Defensive fallback for modules not yet reseeded with default_template_code.
|
|
// Canonical module/template identities are lowercase.
|
|
return `${module.code.toLowerCase()}_ct0001`;
|
|
}
|
|
|
|
export function buildModuleTemplateReference(module: ModuleResponse): TemplateReference {
|
|
return {
|
|
type: "template_ref",
|
|
label: module.name,
|
|
target: {
|
|
module_code: module.code,
|
|
template_code: resolveModuleEntryTemplateCode(module),
|
|
},
|
|
};
|
|
}
|
|
|
|
export function toModuleIndexItem(module: ModuleResponse): ModuleIndexItem {
|
|
const visual = MODULE_VISUALS[module.code.toLowerCase()];
|
|
const metadataIcon = readMetadataString(module, "icon");
|
|
const metadataColor = readMetadataString(module, "color");
|
|
const metadataPlanCode = readMetadataString(module, "plan_code");
|
|
const metadataIsDefaultFree = readMetadataBoolean(module, "is_default_free");
|
|
|
|
return {
|
|
module,
|
|
reference: buildModuleTemplateReference(module),
|
|
|
|
// API is authoritative for module content/business metadata.
|
|
title: module.name,
|
|
description: module.description?.trim() || "Structured clinical reference content",
|
|
category: resolveModuleCategory(module),
|
|
|
|
// Presentation/commerce values use API metadata when supplied and retain
|
|
// current client fallbacks until those fields are guaranteed by the API.
|
|
icon: metadataIcon ?? visual?.icon ?? "folder-outline",
|
|
color: metadataColor ?? visual?.color ?? "#4F46E5",
|
|
isDefaultFree: metadataIsDefaultFree ?? visual?.isDefaultFree ?? false,
|
|
planCode: metadataPlanCode ?? visual?.planCode ?? "",
|
|
};
|
|
}
|
|
|
|
export function normalizeModuleListResponse(value: unknown): ModuleResponse[] {
|
|
if (Array.isArray(value)) {
|
|
return value as ModuleResponse[];
|
|
}
|
|
|
|
if (typeof value === "object" && value !== null) {
|
|
const modules = (value as { modules?: unknown }).modules;
|
|
if (Array.isArray(modules)) {
|
|
return modules as ModuleResponse[];
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|