From d9a19093fd7919e3cffc3609efdac943179f864d Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Fri, 8 May 2026 16:15:42 -0400 Subject: [PATCH] fix(server): wire org-health agent count to member_profiles instead of hardcoded 0 Resolves the "Register an agent" CTA appearing on the org profile page even when agents are already registered. The agentCount in assembleOrgHealth was stubbed as Promise.resolve(0) with a TODO comment; this replaces it with a real query against member_profiles.agents (JSONB array, added in migration 014_agent_configs). The double COALESCE handles nullable agents columns on older rows and the case where no profile row exists yet. --- .changeset/fix-org-health-agent-count-stub.md | 4 ++++ server/src/services/org-health.ts | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 .changeset/fix-org-health-agent-count-stub.md diff --git a/.changeset/fix-org-health-agent-count-stub.md b/.changeset/fix-org-health-agent-count-stub.md new file mode 100644 index 0000000000..ec043e31ed --- /dev/null +++ b/.changeset/fix-org-health-agent-count-stub.md @@ -0,0 +1,4 @@ +--- +--- + +Fix org profile page always showing "Register an agent to start integrating" CTA by replacing the hardcoded `agentCount = 0` stub in `assembleOrgHealth` with a real query against `member_profiles.agents`. Orgs with registered agents will now correctly see their tech-integration health score reflect actual registrations and will no longer see the spurious onboarding CTA. diff --git a/server/src/services/org-health.ts b/server/src/services/org-health.ts index ceade224ea..489bfae060 100644 --- a/server/src/services/org-health.ts +++ b/server/src/services/org-health.ts @@ -309,8 +309,17 @@ export async function assembleOrgHealth(orgId: string): Promise { return []; }), - // Agent count — no org-scoped agents table yet; always 0 until one exists - Promise.resolve(0), + // Count all registered agents across org member profiles, regardless of visibility. + // "Any agent registered" is the threshold — visibility tier is not the gate here. + query<{ total: string }>( + `SELECT COALESCE(SUM(COALESCE(jsonb_array_length(agents), 0)), 0) AS total + FROM member_profiles + WHERE workos_organization_id = $1`, + [orgId] + ).then(r => parseInt(r.rows[0]?.total || '0', 10)).catch(err => { + logger.error({ err, orgId }, 'Failed to fetch agent count'); + return 0; + }), // Leadership roles across all org members query<{ count: string }>(