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:
ZacharyZhang-NY committed 2026-04-16 22:29:36 -04:00
1 parent 555d8e59eb
commit 7f097e3319
5 files changed
+85 -24

No files matched your search

+27
View File
@@ -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;