refactor(api-server): move tasks SQL into db.tasks/db.documents domain methods
Replace inline queryAll calls in routes-tasks.ts with two new domain methods: tasks.listTasksByStatuses (used by GET /api/companies/:id/issues) and documents.listImportantDocsForReview (used by the deliverable-review gate). Routes now hold no SQL or column-mapping logic; the TaskListByStatusesRow shape preserves the existing HTTP response fields including requiresUserReview as a boolean.
This commit is contained in:
1 parent
555d8e59eb
commit
7f097e3319
5 files changed
+85
-24
No files matched your search
@@ -68,6 +68,33 @@ export function buildDocumentMethods(
|
||||
return row?.latest_revision_id ?? null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns up to `limit` documents of the given types for a company that
|
||||
* are either authored by `authorAgentId` or were created at/after the
|
||||
* task's createdAt. Used by the deliverable-review gate when a task is
|
||||
* advanced to "done".
|
||||
*/
|
||||
listImportantDocsForReview(input: {
|
||||
companyId: string;
|
||||
types: readonly string[];
|
||||
authorAgentId: string | null;
|
||||
taskId: string;
|
||||
limit?: number;
|
||||
}): Array<{ id: string; type: string; title: string }> {
|
||||
const limit = input.limit ?? 10;
|
||||
const placeholders = input.types.map(() => "?").join(", ");
|
||||
return readAll<{ id: string; type: string; title: string }>(
|
||||
`select id, type, title from documents where company_id = ? and type in (${placeholders})
|
||||
and (author_agent_id = ? or created_at >= (select created_at from tasks where id = ?))
|
||||
order by created_at desc limit ?`,
|
||||
input.companyId,
|
||||
...input.types,
|
||||
input.authorAgentId ?? "",
|
||||
input.taskId,
|
||||
limit,
|
||||
);
|
||||
},
|
||||
|
||||
listDocumentRevisions(documentId: string, companyId: string) {
|
||||
return readAll<{
|
||||
id: string; company_id: string; document_id: string; revision_number: number;
|
||||
|
||||
@@ -10,6 +10,36 @@ import {
|
||||
taskPriorityOrderSql,
|
||||
} from "./helpers";
|
||||
|
||||
/**
|
||||
* Row shape returned by listTasksByStatuses — used by the
|
||||
* GET /api/companies/:companyId/issues route. Mirrors the historical
|
||||
* inline-SQL projection (no taskType column) so the HTTP response shape
|
||||
* stays stable for agent CLI clients.
|
||||
*/
|
||||
export type TaskListByStatusesRow = {
|
||||
id: string;
|
||||
companyId: string;
|
||||
projectId: string | null;
|
||||
goalId: string | null;
|
||||
parentId: string | null;
|
||||
title: string;
|
||||
description: string;
|
||||
assigneeAgentId: string | null;
|
||||
workspaceId: string | null;
|
||||
status: string;
|
||||
priority: string;
|
||||
activeRunId: string | null;
|
||||
sessionJson: string | null;
|
||||
sessionDisplayId: string | null;
|
||||
costUsd: number | null;
|
||||
requiresUserReview: boolean;
|
||||
startedAt: string | null;
|
||||
completedAt: string | null;
|
||||
cancelledAt: string | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export function buildTaskCrudMethods(
|
||||
getDb: () => DatabaseSync,
|
||||
readAll: ReadAll,
|
||||
@@ -160,6 +190,24 @@ export function buildTaskCrudMethods(
|
||||
);
|
||||
},
|
||||
|
||||
listTasksByStatuses(
|
||||
companyId: string,
|
||||
statuses: string[],
|
||||
): TaskListByStatusesRow[] {
|
||||
const placeholders = statuses.map(() => "?").join(", ");
|
||||
return readAll<TaskListByStatusesRow & { requiresUserReview: number | boolean }>(
|
||||
`select id, company_id as companyId, project_id as projectId, goal_id as goalId, parent_id as parentId, title, description, assignee_agent_id as assigneeAgentId, workspace_id as workspaceId, status, priority, active_run_id as activeRunId, session_json as sessionJson, session_display_id as sessionDisplayId, cost_usd as costUsd, requires_user_review as requiresUserReview, started_at as startedAt, completed_at as completedAt, cancelled_at as cancelledAt, created_at as createdAt, updated_at as updatedAt from tasks where company_id = ? and status in (${placeholders}) order by created_at desc`,
|
||||
companyId,
|
||||
...statuses,
|
||||
).map((t) => ({
|
||||
...t,
|
||||
requiresUserReview: Boolean(t.requiresUserReview),
|
||||
startedAt: t.startedAt ?? null,
|
||||
completedAt: t.completedAt ?? null,
|
||||
cancelledAt: t.cancelledAt ?? null,
|
||||
})) as TaskListByStatusesRow[];
|
||||
},
|
||||
|
||||
countActiveTasksForCompany(companyId: string): number {
|
||||
const row = getDb()
|
||||
.prepare(
|
||||
|
||||
Reference in new issue
Block a user