fix(admin): generically resolve clinical module categories from metadata and kind
This commit is contained in:
parent
b241287579
commit
3bc3d15c7c
|
|
@ -1,4 +1,5 @@
|
|||
from __future__ import annotations
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
|
@ -6,14 +7,53 @@ from app.db.enums import ModuleStatus
|
|||
from app.db.models import Module, AccessPlanModule
|
||||
from ..schema import ModuleStatusUpdateRequest, ModuleUpdateRequest
|
||||
|
||||
UI_MAPPINGS: dict[str, dict[str, str]] = {
|
||||
"acupoints": {"category": "point", "icon": "water-outline", "color": "#2563EB"},
|
||||
"ear-acupuncture": {"category": "point", "icon": "ear-outline", "color": "#F59E0B"},
|
||||
"scalp-acupuncture": {"category": "point", "icon": "accessibility-outline", "color": "#EF4444"},
|
||||
"points_tung": {"category": "point", "icon": "analytics-outline", "color": "#10B981"},
|
||||
"auriculotherapy": {"category": "point", "icon": "body-outline", "color": "#8B5CF6"},
|
||||
CATEGORY_UI_DEFAULTS: dict[str, dict[str, str]] = {
|
||||
"Points": {"icon": "water-outline", "color": "#2563EB"},
|
||||
"Patterns": {"icon": "analytics-outline", "color": "#8B5CF6"},
|
||||
"Nutrition": {"icon": "leaf-outline", "color": "#10B981"},
|
||||
"Therapeutics": {"icon": "medkit-outline", "color": "#EC4899"},
|
||||
"General": {"icon": "book-outline", "color": "#6366F1"},
|
||||
}
|
||||
|
||||
# Backward-compatibility alias
|
||||
UI_MAPPINGS: dict[str, dict[str, str]] = {}
|
||||
|
||||
|
||||
def resolve_module_category(meta: dict[str, Any] | None) -> str:
|
||||
"""
|
||||
Generically resolve module clinical category from metadata / kind.
|
||||
No hardcoded module codes or slugs.
|
||||
"""
|
||||
if not meta:
|
||||
return "General"
|
||||
|
||||
# 1. Explicitly configured category in metadata
|
||||
cat = meta.get("category")
|
||||
if cat and str(cat).strip().lower() != "general":
|
||||
c = str(cat).strip().lower()
|
||||
if c in ("point", "points"):
|
||||
return "Points"
|
||||
if c in ("pattern", "patterns", "five-elements"):
|
||||
return "Patterns"
|
||||
if c == "nutrition":
|
||||
return "Nutrition"
|
||||
if c in ("therapeutic", "therapeutics"):
|
||||
return "Therapeutics"
|
||||
return str(cat).strip()
|
||||
|
||||
# 2. Derive from content kind
|
||||
kind = str(meta.get("kind") or "").strip().lower()
|
||||
if kind in ("point", "points"):
|
||||
return "Points"
|
||||
if kind in ("pattern", "patterns", "five-elements"):
|
||||
return "Patterns"
|
||||
if kind == "nutrition":
|
||||
return "Nutrition"
|
||||
if kind in ("therapeutic", "therapeutics"):
|
||||
return "Therapeutics"
|
||||
|
||||
return "General"
|
||||
|
||||
|
||||
class AdminModuleService:
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
|
|
@ -30,10 +70,10 @@ class AdminModuleService:
|
|||
result: list[dict] = []
|
||||
for m in modules:
|
||||
meta = m.metadata_json or {}
|
||||
ui = UI_MAPPINGS.get(m.slug, {})
|
||||
category = meta.get("category") or ui.get("category", "General")
|
||||
icon = meta.get("icon") or ui.get("icon", "book-outline")
|
||||
color = meta.get("color") or ui.get("color", "#6366F1")
|
||||
category = resolve_module_category(meta)
|
||||
ui_defaults = CATEGORY_UI_DEFAULTS.get(category, CATEGORY_UI_DEFAULTS["General"])
|
||||
icon = meta.get("icon") or ui_defaults["icon"]
|
||||
color = meta.get("color") or ui_defaults["color"]
|
||||
|
||||
result.append({
|
||||
"id": str(m.id),
|
||||
|
|
@ -109,6 +149,8 @@ class AdminModuleService:
|
|||
|
||||
await self.session.commit()
|
||||
await self.session.refresh(module)
|
||||
resolved_category = resolve_module_category(meta)
|
||||
ui_defaults = CATEGORY_UI_DEFAULTS.get(resolved_category, CATEGORY_UI_DEFAULTS["General"])
|
||||
return {
|
||||
"status": "success",
|
||||
"message": f"Module '{module.name}' updated successfully.",
|
||||
|
|
@ -117,8 +159,8 @@ class AdminModuleService:
|
|||
"slug": module.slug,
|
||||
"name": module.name,
|
||||
"description": module.description,
|
||||
"category": meta.get("category"),
|
||||
"color": meta.get("color"),
|
||||
"icon": meta.get("icon"),
|
||||
"category": resolved_category,
|
||||
"color": meta.get("color") or ui_defaults["color"],
|
||||
"icon": meta.get("icon") or ui_defaults["icon"],
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||
from app.db.enums import BillingPeriod, PlanType, SubscriptionStatus
|
||||
from app.db.models import AccessPlan, AccessPlanModule, AppUser, Module, UserSubscription
|
||||
from ..schema import CreatePlanRequest, PlanPricingUpdateRequest, PlanStatusUpdateRequest
|
||||
from .module_service import UI_MAPPINGS
|
||||
from .module_service import resolve_module_category, CATEGORY_UI_DEFAULTS, UI_MAPPINGS
|
||||
|
||||
|
||||
class AdminPlanService:
|
||||
|
|
@ -28,14 +28,15 @@ class AdminPlanService:
|
|||
if p_str not in plan_mods_map:
|
||||
plan_mods_map[p_str] = []
|
||||
meta = m.metadata_json or {}
|
||||
ui = UI_MAPPINGS.get(m.slug, {})
|
||||
category = resolve_module_category(meta)
|
||||
ui_defaults = CATEGORY_UI_DEFAULTS.get(category, CATEGORY_UI_DEFAULTS["General"])
|
||||
plan_mods_map[p_str].append({
|
||||
"id": str(m.id),
|
||||
"slug": m.slug,
|
||||
"name": m.name,
|
||||
"category": meta.get("category") or ui.get("category", "General"),
|
||||
"icon": meta.get("icon") or ui.get("icon", "book-outline"),
|
||||
"color": meta.get("color") or ui.get("color", "#6366F1"),
|
||||
"category": category,
|
||||
"icon": meta.get("icon") or ui_defaults["icon"],
|
||||
"color": meta.get("color") or ui_defaults["color"],
|
||||
"is_active": m.is_active,
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
from app.features.admin.services.module_service import (
|
||||
resolve_module_category,
|
||||
CATEGORY_UI_DEFAULTS,
|
||||
)
|
||||
|
||||
|
||||
def test_resolve_module_category_from_explicit_metadata():
|
||||
assert resolve_module_category({"category": "Points"}) == "Points"
|
||||
assert resolve_module_category({"category": "point"}) == "Points"
|
||||
assert resolve_module_category({"category": "Patterns"}) == "Patterns"
|
||||
assert resolve_module_category({"category": "Nutrition"}) == "Nutrition"
|
||||
assert resolve_module_category({"category": "Therapeutics"}) == "Therapeutics"
|
||||
|
||||
|
||||
def test_resolve_module_category_from_kind():
|
||||
assert resolve_module_category({"kind": "point"}) == "Points"
|
||||
assert resolve_module_category({"kind": "POINTS"}) == "Points"
|
||||
assert resolve_module_category({"kind": "pattern"}) == "Patterns"
|
||||
assert resolve_module_category({"kind": "five-elements"}) == "Patterns"
|
||||
assert resolve_module_category({"kind": "nutrition"}) == "Nutrition"
|
||||
assert resolve_module_category({"kind": "therapeutic"}) == "Therapeutics"
|
||||
assert resolve_module_category({"kind": "therapeutics"}) == "Therapeutics"
|
||||
|
||||
|
||||
def test_resolve_module_category_fallback():
|
||||
assert resolve_module_category(None) == "General"
|
||||
assert resolve_module_category({}) == "General"
|
||||
assert resolve_module_category({"category": "General"}) == "General"
|
||||
assert resolve_module_category({"category": "General", "kind": "point"}) == "Points"
|
||||
|
||||
|
||||
def test_category_ui_defaults():
|
||||
assert "Points" in CATEGORY_UI_DEFAULTS
|
||||
assert "Patterns" in CATEGORY_UI_DEFAULTS
|
||||
assert "Nutrition" in CATEGORY_UI_DEFAULTS
|
||||
assert "Therapeutics" in CATEGORY_UI_DEFAULTS
|
||||
assert "General" in CATEGORY_UI_DEFAULTS
|
||||
Loading…
Reference in New Issue