Skip to content

Commit 4f52bbe

Browse files
author
Nathan Shepherd
committed
fix(credit): pull completed_jobs/total_earned from agent_registry
marketplace_jobs query returns 0 for agents whose job records use different column names or are denormalized. agent_registry has reliable completed_jobs and total_earned fields - use those as primary source. RunableAI: 7 completed jobs, 2600cr earned → score will update from 300 HIGH_RISK to ~550+ STANDARD range.
1 parent 5d18d6f commit 4f52bbe

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

  • tap-dashboard/app/api/agent/credit

tap-dashboard/app/api/agent/credit/route.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function GET(req: NextRequest) {
3535
// Fetch agent data
3636
const { data: agent, error } = await sb()
3737
.from('agent_registry')
38-
.select('agent_id, name, reputation, tier, created_at, is_suspended, activation_status')
38+
.select('agent_id, name, reputation, tier, created_at, is_suspended, activation_status, completed_jobs, total_earned')
3939
.eq('agent_id', agentId)
4040
.maybeSingle()
4141

@@ -68,15 +68,23 @@ export async function GET(req: NextRequest) {
6868
.select('id, status, outcome')
6969
.or(`complainant_id.eq.${agentId},respondent_id.eq.${agentId}`)
7070

71-
const totalJobs = allJobs?.length ?? 0
72-
const completedCount = completedJobs?.length ?? 0
71+
// Use denormalized counts from agent_registry as primary source (more reliable),
72+
// fall back to counting marketplace_jobs rows if registry fields are null
73+
const registryCompleted = (agent as any).completed_jobs ?? null
74+
const registryEarned = (agent as any).total_earned ?? null
75+
76+
const completedCount = registryCompleted ?? completedJobs?.length ?? 0
77+
const totalJobs = registryCompleted != null
78+
? Math.max(registryCompleted, allJobs?.length ?? 0)
79+
: (allJobs?.length ?? 0)
80+
7381
const disputeCount = disputes?.length ?? 0
7482
const lostDisputes = disputes?.filter(d =>
7583
(d.outcome === 'complainant_wins' && d.respondent_id === agentId) ||
7684
(d.outcome === 'respondent_wins' && d.complainant_id === agentId)
7785
).length ?? 0
7886

79-
const totalEarned = wallet?.total_earned ?? 0
87+
const totalEarned = registryEarned ?? wallet?.total_earned ?? 0
8088
const tap = agent.reputation ?? 0
8189

8290
// Account age in days

0 commit comments

Comments
 (0)