refactor(api-server): move agents-standup SQL into db domain methods

Replace the six inline queryAll calls in routes-agents-standup.ts with
typed domain methods: db.agents gains listDirectReportsForStandup and
listReportStatusesForStandup (LEFT JOIN aggregation), db.tasks gains
listAssigneeTasksForStandup and listTasksForAssignees (safe on empty
assignee list), db.approvals gains listAgentPendingApprovals and
listAgentResolvedApprovals. Shared listRecentTaskComments is reused.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-16 22:39:43 -04:00
1 parent 4342cf3e2f
commit 9874d825b6
5 files changed
+146 -94

No files matched your search

+46
View File
@@ -139,6 +139,52 @@ export function buildAgentHierarchyMethods(
);
},
/**
* Returns the slim direct-reports projection used by
* GET /api/agents/me/standup — only (id, name, role, status).
*/
listDirectReportsForStandup(
companyId: string,
managerId: string,
): Array<{ id: string; name: string; role: string; status: string }> {
return readAll(
"select id, name, role, status from agents where company_id = ? and reports_to = ? order by created_at asc",
companyId,
managerId,
);
},
/**
* Returns per-report task-status counts for the standup aggregation.
* Joins agents with their tasks and aggregates statuses (in_progress,
* in_review, blocked, done).
*/
listReportStatusesForStandup(companyId: string, managerId: string): Array<{
id: string;
name: string;
role: string;
status: string;
tasksInProgress: number;
tasksInReview: number;
tasksBlocked: number;
tasksDone: number;
}> {
return readAll(
`select agents.id as id, agents.name as name, agents.role as role, agents.status as status,
coalesce(sum(case when tasks.status = 'in_progress' then 1 else 0 end), 0) as tasksInProgress,
coalesce(sum(case when tasks.status = 'in_review' then 1 else 0 end), 0) as tasksInReview,
coalesce(sum(case when tasks.status = 'blocked' then 1 else 0 end), 0) as tasksBlocked,
coalesce(sum(case when tasks.status = 'done' then 1 else 0 end), 0) as tasksDone
from agents
left join tasks on tasks.assignee_agent_id = agents.id and tasks.company_id = agents.company_id
where agents.company_id = ? and agents.reports_to = ?
group by agents.id, agents.name, agents.role, agents.status
order by agents.created_at asc`,
companyId,
managerId,
);
},
/**
* Returns the duplicate-detection projection used by
* POST /api/companies/:id/agents to reject hires with a name that
+44
View File
@@ -130,6 +130,50 @@ export function buildApprovalMethods(
getDb().prepare("delete from approvals where id = ? and company_id = ?").run(id, companyId);
},
/**
* Returns the requesting agent's pending approvals for standup
* aggregation (id, type, state, payloadSummary, decisionNote).
*/
listAgentPendingApprovals(
companyId: string,
agentId: string,
): Array<{
id: string;
type: string;
state: string;
payloadSummary: string | null;
decisionNote: string | null;
}> {
return readAll(
"select id, type, state, payload_summary as payloadSummary, decision_note as decisionNote from approvals where company_id = ? and requested_by_agent_id = ? and state = 'pending' order by created_at desc",
companyId,
agentId,
);
},
/**
* Returns up to `limit` of the requesting agent's resolved approvals
* for standup aggregation (state != 'pending').
*/
listAgentResolvedApprovals(
companyId: string,
agentId: string,
limit = 5,
): Array<{
id: string;
type: string;
state: string;
payloadSummary: string | null;
decisionNote: string | null;
}> {
return readAll(
"select id, type, state, payload_summary as payloadSummary, decision_note as decisionNote from approvals where company_id = ? and requested_by_agent_id = ? and state != 'pending' order by created_at desc limit ?",
companyId,
agentId,
limit,
);
},
/**
* Returns the slim approval projection used by
* GET /api/agents/me/standup. Ordered by created_at desc.
+40
View File
@@ -190,6 +190,46 @@ export function buildTaskCrudMethods(
);
},
/**
* Returns the slim task projection (id, title, status, priority) for a
* single assignee, ordered by created_at desc. Used by the alternate
* me/standup builder in routes-agents-standup.ts.
*/
listAssigneeTasksForStandup(
companyId: string,
agentId: string,
): Array<{ id: string; title: string; status: string; priority: string }> {
return readAll(
"select id, title, status, priority from tasks where company_id = ? and assignee_agent_id = ? order by created_at desc",
companyId,
agentId,
);
},
/**
* Returns the slim task projection for a set of assignees. Safe on
* an empty assignee list (returns []). Used by the alternate
* me/standup builder to fetch reports' tasks in one query.
*/
listTasksForAssignees(
companyId: string,
assigneeIds: string[],
): Array<{
id: string;
title: string;
status: string;
priority: string;
assigneeAgentId: string;
}> {
if (assigneeIds.length === 0) return [];
const placeholders = assigneeIds.map(() => "?").join(",");
return readAll(
`select id, title, status, priority, assignee_agent_id as assigneeAgentId from tasks where company_id = ? and assignee_agent_id in (${placeholders}) order by created_at desc`,
companyId,
...assigneeIds,
);
},
/**
* Returns the slim task projection used by the me/standup aggregation.
* Ordered by created_at desc.