Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ workflows:
only:
- develop
- feat/ai-workflows
- pm-1782

- 'build-prod':
context: org-global
Expand Down
27 changes: 27 additions & 0 deletions src/api/ai-workflow/ai-workflow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ import { User } from 'src/shared/decorators/user.decorator';
export class AiWorkflowController {
constructor(private readonly aiWorkflowService: AiWorkflowService) {}

@Get('/:workflowId/runs/:runId/items')
@Roles(
UserRole.Admin,
UserRole.Copilot,
UserRole.ProjectManager,
UserRole.User,
)
@Scopes(Scope.ReadWorkflowRun)
@ApiOperation({
summary: 'Get AIWorkflowRunItems for a given workflow run ID',
})
@ApiResponse({
status: 200,
description: 'The AIWorkflowRunItems for the given run ID.',
})
@ApiResponse({ status: 400, description: 'Bad Request.' })
@ApiResponse({ status: 401, description: 'Unauthorized.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@ApiResponse({ status: 404, description: 'Run not found.' })
async getRunItems(
@Param('workflowId') workflowId: string,
@Param('runId') runId: string,
@User() user: JwtUser,
) {
return this.aiWorkflowService.getRunItems(workflowId, runId, user);
}

@Post()
@Roles(UserRole.Admin)
@Scopes(Scope.CreateWorkflow)
Expand Down
99 changes: 99 additions & 0 deletions src/api/ai-workflow/ai-workflow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,103 @@ export class AiWorkflowService {
throw error;
}
}

async getRunItems(workflowId: string, runId: string, user: JwtUser) {
const workflow = await this.prisma.aiWorkflow.findUnique({
where: { id: workflowId },
});
if (!workflow) {
this.logger.error(`Workflow with id ${workflowId} not found.`);
throw new NotFoundException(`Workflow with id ${workflowId} not found.`);
}

const run = await this.prisma.aiWorkflowRun.findUnique({
where: { id: runId },
include: { workflow: true },
});
if (!run || run.workflowId !== workflowId) {
this.logger.error(
`Run with id ${runId} not found or does not belong to workflow ${workflowId}.`,
);
throw new NotFoundException(
`Run with id ${runId} not found or does not belong to workflow ${workflowId}.`,
);
}

const submission = run.submissionId
? await this.prisma.submission.findUnique({
where: { id: run.submissionId },
})
: null;
const challengeId = submission?.challengeId;

if (!challengeId) {
this.logger.error(
`Challenge ID not found for submission ${run.submissionId}`,
);
throw new InternalServerErrorException(
`Challenge ID not found for submission ${run.submissionId}`,
);
}

const challenge: ChallengeData =
await this.challengeApiService.getChallengeDetail(challengeId);

if (!challenge) {
throw new InternalServerErrorException(
`Challenge with id ${challengeId} was not found!`,
);
}

const isM2mOrAdmin = user.isMachine || user.roles?.includes(UserRole.Admin);
if (!isM2mOrAdmin) {
const requiredRoles = [
UserRole.Reviewer,
UserRole.ProjectManager,
UserRole.Copilot,
UserRole.Submitter,
].map((r) => r.toLowerCase());

const memberRoles = (
await this.resourceApiService.getMemberResourcesRoles(
challengeId,
user.userId,
)
).filter((resource) =>
requiredRoles.some(
(role) =>
resource.roleName!.toLowerCase().indexOf(role.toLowerCase()) >= 0,
),
);

if (!memberRoles.length) {
throw new ForbiddenException('Insufficient permissions');
}

if (
challenge.status !== ChallengeStatus.COMPLETED &&
memberRoles.some(
(r) => r.roleName?.toLowerCase() === UserRole.Submitter.toLowerCase(),
) &&
user.userId !== submission?.memberId
) {
this.logger.log(
`Submitter ${user.userId} trying to access AI workflow run for other submitters.`,
);
throw new ForbiddenException('Insufficient permissions');
}
}

const items = await this.prisma.aiWorkflowRunItem.findMany({
where: { workflowRunId: runId },
include: {
comments: true,
},
orderBy: {
createdAt: 'asc',
},
});

return items;
}
}