import type { Actor, DelegatedTask, DelegatedTaskStatus } from "@rakazo/contracts"; import type { PrismaClient } from "@rakazo/db"; const TASK_LIST_LIMIT = 100; /** * The caller's delegated tasks that touch the current space: reported in one of its * conversations or worked on by one of its specialists. Newest first. */ export async function listDelegatedTasks( prisma: PrismaClient, actor: Actor, input: { threadId?: string; status?: DelegatedTaskStatus }, ): Promise { const rows = await prisma.delegatedTask.findMany({ where: { userId: actor.userId, OR: [{ spaceId: actor.spaceId }, { assigneeSpaceId: actor.spaceId }], ...(input.threadId ? { threadId: input.threadId } : {}), ...(input.status ? { status: input.status } : {}), }, orderBy: [{ createdAt: "desc" }, { id: "desc" }], take: TASK_LIST_LIMIT, }); return rows.map((row) => ({ id: row.id, threadId: row.threadId, requesterBotId: row.requesterBotId, assigneeBotId: row.assigneeBotId, assigneeSpaceId: row.assigneeSpaceId, title: row.title, brief: row.brief, status: row.status as DelegatedTaskStatus, deadlineAt: row.deadlineAt?.toISOString() ?? null, result: row.result, runId: row.runId, createdAt: row.createdAt.toISOString(), updatedAt: row.updatedAt.toISOString(), completedAt: row.completedAt?.toISOString() ?? null, })); }