Skip to content

Commit 9f9459e

Browse files
committed
PM-5738 - return number of comments for workflow run
1 parent 2153813 commit 9f9459e

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

src/api/ai-workflow/ai-workflow.service.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,13 +766,44 @@ export class AiWorkflowService {
766766
},
767767
});
768768

769+
const runIds = runs.map((run) => run.id);
770+
const commentCounts =
771+
runIds.length > 0
772+
? await this.prisma.$queryRaw<
773+
Array<{ workflowRunId: string; count: bigint }>
774+
>(
775+
Prisma.sql`
776+
SELECT "aiWorkflowRunItem"."workflowRunId" AS "workflowRunId",
777+
COUNT(*) AS "count"
778+
FROM "aiWorkflowRunItemComment"
779+
INNER JOIN "aiWorkflowRunItem"
780+
ON "aiWorkflowRunItemComment"."workflowRunItemId" = "aiWorkflowRunItem"."id"
781+
WHERE "aiWorkflowRunItem"."workflowRunId" IN (${Prisma.join(runIds)})
782+
GROUP BY "aiWorkflowRunItem"."workflowRunId"
783+
`,
784+
)
785+
: [];
786+
787+
const commentCountMap = commentCounts.reduce(
788+
(acc, entry) => {
789+
acc[entry.workflowRunId] = Number(entry.count);
790+
return acc;
791+
},
792+
{} as Record<string, number>,
793+
);
794+
795+
const runsWithCommentCount = runs.map((run) => ({
796+
...run,
797+
commentsCount: commentCountMap[run.id] ?? 0,
798+
}));
799+
769800
if (filter.runId && !runs.length) {
770801
throw new NotFoundException(
771802
`AI Workflow run with id ${filter.runId} not found!`,
772803
);
773804
}
774805

775-
const submission = runs[0]?.submission;
806+
const submission = runsWithCommentCount[0]?.submission;
776807
if ((!submission || !submission.challengeId) && filter.submissionId) {
777808
this.logger.log(
778809
`No runs have been found for submission ${filter.submissionId}`,
@@ -830,7 +861,7 @@ export class AiWorkflowService {
830861
}
831862
}
832863

833-
return runs.map((r) => ({ ...r, submission: undefined }));
864+
return runsWithCommentCount.map((r) => ({ ...r, submission: undefined }));
834865
}
835866

836867
async updateWorkflowRun(

0 commit comments

Comments
 (0)