@@ -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