import logging from yuxi.channel.protocols import ChannelAccountSnapshot logger = logging.getLogger(__name__) class JDStatus: def __init__(self, gateway=None): self._gateway = gateway async def probe(self, account: dict | None = None) -> bool: gateway = self._gateway if gateway is None: return False token = gateway.get_token("default") if not token: return False return True def build_summary(self, snapshot: object) -> dict: if not isinstance(snapshot, ChannelAccountSnapshot): return {} return { "account_id": snapshot.account_id, "running": snapshot.running, "connected": snapshot.connected, "health_state": snapshot.health_state, } def build_account_snapshot( self, account: dict, config: dict, runtime: ChannelAccountSnapshot | None = None, probe_result: object | None = None, audit: object | None = None, ) -> ChannelAccountSnapshot: gateway = self._gateway token = gateway.get_token("default") if gateway else None shop_id = account.get("shop_id", "") display = f"jd-{shop_id}" if shop_id else "jd-default" return ChannelAccountSnapshot( account_id=account.get("account_id", "default"), name=account.get("name", display), enabled=account.get("enabled", True), configured=bool(account.get("app_key") and account.get("app_secret") and account.get("shop_id")), status_state="linked" if token else "not-linked", running=getattr(runtime, "running", False) if runtime else False, connected=bool(token), last_message_at=getattr(runtime, "last_message_at", None) if runtime else None, health_state="ok" if probe_result else "unknown", dm_policy=account.get("dm_policy", "pairing"), ) def collect_status_issues(self, accounts: list[ChannelAccountSnapshot]) -> list: issues = [] for acc in accounts: if not acc.configured: issues.append( { "channel": "jd", "account_id": acc.account_id, "kind": "not-configured", "message": "JD credentials not configured", "fix": "Set JD_APP_KEY, JD_APP_SECRET, JD_SHOP_ID environment variables", } ) elif not acc.connected: issues.append( { "channel": "jd", "account_id": acc.account_id, "kind": "not-connected", "message": "JD gateway token not available", "fix": "Check JD OAuth token configuration and gateway status", } ) return issues