Skip to content

Commit f620832

Browse files
committed
Add new workflow runs endpoint
1 parent 36fe1d8 commit f620832

File tree

2 files changed

+86
-21
lines changed

2 files changed

+86
-21
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@ export class AiWorkflowController {
7070
return this.aiWorkflowService.getWorkflows({ name: name?.trim() });
7171
}
7272

73+
@Get('/runs')
74+
@Roles(
75+
UserRole.Admin,
76+
UserRole.Copilot,
77+
UserRole.ProjectManager,
78+
UserRole.Reviewer,
79+
UserRole.Submitter,
80+
UserRole.User,
81+
)
82+
@Scopes(Scope.ReadWorkflowRun)
83+
@ApiOperation({
84+
summary: 'Get all the AI workflow runs for a given submission ID',
85+
})
86+
@ApiQuery({
87+
name: 'submissionId',
88+
description: 'The ID of the submission to fetch AI workflow runs for',
89+
required: true,
90+
})
91+
@ApiResponse({
92+
status: 200,
93+
description: 'The AI workflow runs for the given submission ID.',
94+
})
95+
@ApiResponse({ status: 403, description: 'Forbidden.' })
96+
getSubmissionRuns(
97+
@Query('submissionId') submissionId: string,
98+
@User() user: JwtUser,
99+
) {
100+
return this.aiWorkflowService.getWorkflowRuns(user, {
101+
submissionId,
102+
});
103+
}
104+
73105
@Get(':id')
74106
@Roles(UserRole.Admin, UserRole.User, UserRole.Copilot, UserRole.Reviewer)
75107
@Scopes(Scope.ReadWorkflow)
@@ -130,7 +162,8 @@ export class AiWorkflowController {
130162
)
131163
@Scopes(Scope.ReadWorkflowRun)
132164
@ApiOperation({
133-
summary: 'Get all the AI workflow runs for a given submission ID',
165+
summary:
166+
'Get all the AI workflow runs for a given submission ID & workflow ID',
134167
})
135168
@ApiQuery({
136169
name: 'submissionId',
@@ -142,12 +175,13 @@ export class AiWorkflowController {
142175
description: 'The AI workflow runs for the given submission ID.',
143176
})
144177
@ApiResponse({ status: 403, description: 'Forbidden.' })
145-
getRuns(
178+
getWorkflowRuns(
146179
@Param('workflowId') workflowId: string,
147180
@Query('submissionId') submissionId: string,
148181
@User() user: JwtUser,
149182
) {
150-
return this.aiWorkflowService.getWorkflowRuns(workflowId, user, {
183+
return this.aiWorkflowService.getWorkflowRuns(user, {
184+
workflowId,
151185
submissionId,
152186
});
153187
}
@@ -180,13 +214,10 @@ export class AiWorkflowController {
180214
@Param('runId') runId: string,
181215
@User() user: JwtUser,
182216
) {
183-
const runs = await this.aiWorkflowService.getWorkflowRuns(
217+
const runs = await this.aiWorkflowService.getWorkflowRuns(user, {
184218
workflowId,
185-
user,
186-
{
187-
runId,
188-
},
189-
);
219+
runId,
220+
});
190221

191222
return runs[0];
192223
}
@@ -358,6 +389,7 @@ export class AiWorkflowController {
358389
@Roles(
359390
UserRole.Admin,
360391
UserRole.Copilot,
392+
UserRole.Reviewer,
361393
UserRole.ProjectManager,
362394
UserRole.User,
363395
)

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

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -510,21 +510,53 @@ export class AiWorkflowService {
510510
}
511511

512512
async getWorkflowRuns(
513-
workflowId: string,
514513
user: JwtUser,
515-
filter: { submissionId?: string; runId?: string },
514+
filter: { workflowId?: string; submissionId?: string; runId?: string },
516515
) {
517-
this.logger.log(
518-
`fetching workflow runs for workflowId ${workflowId} and ${JSON.stringify(filter)}`,
519-
);
516+
this.logger.log(`fetching workflow runs for ${JSON.stringify(filter)}`);
517+
518+
const { workflowId, submissionId } = filter;
520519

521520
// validate workflowId
522-
try {
523-
await this.getWorkflowById(workflowId);
524-
} catch (e) {
525-
if (e instanceof NotFoundException) {
526-
throw new BadRequestException(
527-
`Invalid workflow id provided! Workflow with id ${workflowId} does not exist!`,
521+
if (workflowId !== undefined) {
522+
console.log('here1');
523+
524+
try {
525+
await this.getWorkflowById(workflowId);
526+
} catch (e) {
527+
if (e instanceof NotFoundException) {
528+
throw new BadRequestException(
529+
`Invalid workflow id provided! Workflow with id ${workflowId} does not exist!`,
530+
);
531+
}
532+
}
533+
}
534+
535+
// validate submissionId
536+
if (workflowId === undefined && !submissionId?.trim()) {
537+
throw new BadRequestException('Submission id is required!');
538+
}
539+
540+
if (submissionId) {
541+
try {
542+
const submission = await this.prisma.submission.findUnique({
543+
where: { id: submissionId },
544+
});
545+
if (!submission) {
546+
throw new BadRequestException(
547+
`Invalid submission id provided! Submission with id ${submissionId} does not exist!`,
548+
);
549+
}
550+
} catch (e) {
551+
if (e instanceof BadRequestException) {
552+
throw e;
553+
}
554+
this.logger.error(
555+
`Failed to validate submission id ${submissionId}`,
556+
e,
557+
);
558+
throw new InternalServerErrorException(
559+
'Failed to validate submission id',
528560
);
529561
}
530562
}
@@ -533,7 +565,7 @@ export class AiWorkflowService {
533565
where: {
534566
workflowId,
535567
id: filter.runId,
536-
submissionId: filter.submissionId,
568+
submissionId,
537569
},
538570
include: {
539571
submission: true,
@@ -729,6 +761,7 @@ export class AiWorkflowService {
729761
const isM2mOrAdmin = user.isMachine || user.roles?.includes(UserRole.Admin);
730762
if (!isM2mOrAdmin) {
731763
const requiredRoles = [
764+
UserRole.Screener,
732765
UserRole.Reviewer,
733766
UserRole.ProjectManager,
734767
UserRole.Copilot,

0 commit comments

Comments
 (0)