Skip to content

Commit 14bc2cc

Browse files
committed
feat: api endpoint to create comment
1 parent c411951 commit 14bc2cc

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
UpdateAiWorkflowDto,
2525
CreateAiWorkflowRunItemsDto,
2626
UpdateAiWorkflowRunDto,
27+
CreateRunItemCommentDto,
2728
} from '../../dto/aiWorkflow.dto';
2829
import { Scopes } from 'src/shared/decorators/scopes.decorator';
2930
import { UserRole } from 'src/shared/enums/userRole.enum';
@@ -38,6 +39,43 @@ import { User } from 'src/shared/decorators/user.decorator';
3839
export class AiWorkflowController {
3940
constructor(private readonly aiWorkflowService: AiWorkflowService) {}
4041

42+
@Post('/:workflowId/runs/:runId/items/:itemId/comments')
43+
@Roles(
44+
UserRole.Submitter,
45+
UserRole.Copilot,
46+
UserRole.ProjectManager,
47+
UserRole.Admin,
48+
)
49+
@Scopes(Scope.CreateWorkflowRun)
50+
@ApiOperation({ summary: 'Create a comment for a specific run item' })
51+
@ApiResponse({
52+
status: 201,
53+
description: 'Comment created successfully.',
54+
})
55+
@ApiResponse({ status: 400, description: 'Bad Request.' })
56+
@ApiResponse({ status: 401, description: 'Unauthorized.' })
57+
@ApiResponse({ status: 403, description: 'Forbidden.' })
58+
@ApiResponse({
59+
status: 404,
60+
description: 'Workflow, Run, or Item not found.',
61+
})
62+
async createRunItemComment(
63+
@Param('workflowId') workflowId: string,
64+
@Param('runId') runId: string,
65+
@Param('itemId') itemId: string,
66+
@Body(new ValidationPipe({ whitelist: true, transform: true }))
67+
body: CreateRunItemCommentDto,
68+
@User() user: JwtUser,
69+
) {
70+
return this.aiWorkflowService.createRunItemComment(
71+
workflowId,
72+
runId,
73+
itemId,
74+
body,
75+
user,
76+
);
77+
}
78+
4179
@Post()
4280
@Roles(UserRole.Admin)
4381
@Scopes(Scope.CreateWorkflow)

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { PrismaService } from '../../shared/modules/global/prisma.service';
1010
import {
1111
CreateAiWorkflowDto,
1212
CreateAiWorkflowRunDto,
13+
CreateRunItemCommentDto,
1314
UpdateAiWorkflowDto,
1415
UpdateAiWorkflowRunDto,
1516
} from '../../dto/aiWorkflow.dto';
@@ -36,6 +37,51 @@ export class AiWorkflowService {
3637
this.logger = LoggerService.forRoot('AiWorkflowService');
3738
}
3839

40+
async createRunItemComment(
41+
workflowId: string,
42+
runId: string,
43+
itemId: string,
44+
body: CreateRunItemCommentDto,
45+
user: JwtUser,
46+
) {
47+
const workflow = await this.prisma.aiWorkflow.findUnique({
48+
where: { id: workflowId },
49+
});
50+
if (!workflow) {
51+
throw new NotFoundException(`Workflow with id ${workflowId} not found.`);
52+
}
53+
54+
const run = await this.prisma.aiWorkflowRun.findUnique({
55+
where: { id: runId },
56+
});
57+
if (!run || run.workflowId !== workflowId) {
58+
throw new NotFoundException(
59+
`Run with id ${runId} not found or does not belong to workflow ${workflowId}.`,
60+
);
61+
}
62+
63+
const item = await this.prisma.aiWorkflowRunItem.findUnique({
64+
where: { id: itemId },
65+
});
66+
if (!item || item.workflowRunId !== runId) {
67+
throw new NotFoundException(
68+
`Item with id ${itemId} not found or does not belong to run ${runId}.`,
69+
);
70+
}
71+
72+
const createdComment = await this.prisma.aiWorkflowRunItemComment.create({
73+
data: {
74+
workflowRunItemId: itemId,
75+
content: body.content,
76+
parentId: body.parentId ?? null,
77+
userId: user.userId!,
78+
createdAt: new Date(),
79+
},
80+
});
81+
82+
return createdComment;
83+
}
84+
3985
async scorecardExists(scorecardId: string): Promise<boolean> {
4086
const count = await this.prisma.scorecard.count({
4187
where: { id: scorecardId, status: ScorecardStatus.ACTIVE },

src/dto/aiWorkflow.dto.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,15 @@ export class CreateAiWorkflowRunItemsDto {
138138
@Type(() => CreateAiWorkflowRunItemDto)
139139
items: CreateAiWorkflowRunItemDto[];
140140
}
141+
142+
export class CreateRunItemCommentDto {
143+
@ApiProperty()
144+
@IsString()
145+
@IsNotEmpty()
146+
content: string;
147+
148+
@ApiProperty({ required: false })
149+
@IsString()
150+
@IsOptional()
151+
parentId?: string;
152+
}

0 commit comments

Comments
 (0)