-
Notifications
You must be signed in to change notification settings - Fork 9
feat(PM-2177): modified schema to support vote tracking #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| Warnings: | ||
| - You are about to drop the column `downVotes` on the `aiWorkflowRunItem` table. All the data in the column will be lost. | ||
| - You are about to drop the column `upVotes` on the `aiWorkflowRunItem` table. All the data in the column will be lost. | ||
| - You are about to drop the column `downVotes` on the `aiWorkflowRunItemComment` table. All the data in the column will be lost. | ||
| - You are about to drop the column `upVotes` on the `aiWorkflowRunItemComment` table. All the data in the column will be lost. | ||
| */ | ||
| -- CreateEnum | ||
| CREATE TYPE "VoteType" AS ENUM ('UPVOTE', 'DOWNVOTE'); | ||
|
|
||
| -- AlterTable | ||
| ALTER TABLE "aiWorkflowRunItem" DROP COLUMN "downVotes", | ||
| DROP COLUMN "upVotes"; | ||
|
|
||
| -- AlterTable | ||
| ALTER TABLE "aiWorkflowRunItemComment" DROP COLUMN "downVotes", | ||
| DROP COLUMN "upVotes"; | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "aiWorkflowRunItemVote" ( | ||
| "id" VARCHAR(14) NOT NULL DEFAULT nanoid(), | ||
| "workflowRunItemId" VARCHAR(14) NOT NULL, | ||
| "voteType" "VoteType" NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "createdBy" TEXT, | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| CONSTRAINT "aiWorkflowRunItemVote_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "aiWorkflowRunItemCommentVote" ( | ||
| "id" VARCHAR(14) NOT NULL DEFAULT nanoid(), | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "workflowRunItemCommentId" VARCHAR(14) NOT NULL, | ||
| "voteType" "VoteType" NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "createdBy" TEXT, | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| CONSTRAINT "aiWorkflowRunItemCommentVote_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "aiWorkflowRunItemVote_workflowRunItemId_idx" ON "aiWorkflowRunItemVote"("workflowRunItemId"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "aiWorkflowRunItemCommentVote_workflowRunItemCommentId_idx" ON "aiWorkflowRunItemCommentVote"("workflowRunItemCommentId"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "aiWorkflowRunItemVote" ADD CONSTRAINT "aiWorkflowRunItemVote_workflowRunItemId_fkey" FOREIGN KEY ("workflowRunItemId") REFERENCES "aiWorkflowRunItem"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "aiWorkflowRunItemCommentVote" ADD CONSTRAINT "aiWorkflowRunItemCommentVote_workflowRunItemCommentId_fkey" FOREIGN KEY ("workflowRunItemCommentId") REFERENCES "aiWorkflowRunItemComment"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ import { UserRole } from 'src/shared/enums/userRole.enum'; | |
| import { ChallengeStatus } from 'src/shared/enums/challengeStatus.enum'; | ||
| import { LoggerService } from 'src/shared/modules/global/logger.service'; | ||
| import { GiteaService } from 'src/shared/modules/global/gitea.service'; | ||
| import { VoteType } from '@prisma/client'; | ||
|
|
||
| @Injectable() | ||
| export class AiWorkflowService { | ||
|
|
@@ -96,7 +97,49 @@ export class AiWorkflowService { | |
| ); | ||
| } | ||
|
|
||
| const allowedFields = ['content', 'upVotes', 'downVotes']; | ||
| // Handle vote updates | ||
| if (patchData.upVote !== undefined || patchData.downVote !== undefined) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| if (!user.userId) { | ||
| throw new BadRequestException('User id is not available'); | ||
| } | ||
|
|
||
| // Remove existing votes by this user for this comment | ||
| await this.prisma.aiWorkflowRunItemCommentVote.deleteMany({ | ||
| where: { | ||
| workflowRunItemCommentId: commentId, | ||
| createdBy: user.userId.toString(), | ||
| }, | ||
| }); | ||
|
|
||
| // Add new vote if upVote or downVote is true | ||
| if (patchData.upVote) { | ||
| await this.prisma.aiWorkflowRunItemCommentVote.create({ | ||
| data: { | ||
| workflowRunItemCommentId: commentId, | ||
| voteType: VoteType.UPVOTE, | ||
| createdBy: user.userId.toString(), | ||
| }, | ||
| }); | ||
| } else if (patchData.downVote) { | ||
| await this.prisma.aiWorkflowRunItemCommentVote.create({ | ||
| data: { | ||
| workflowRunItemCommentId: commentId, | ||
| voteType: VoteType.DOWNVOTE, | ||
| createdBy: user.userId.toString(), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| delete patchData.downVote; | ||
| delete patchData.upVote; | ||
| } | ||
|
|
||
| // No other fields to update apart from likes | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (Object.keys(patchData).length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const allowedFields = ['content']; | ||
| const updateData: any = {}; | ||
| for (const key of allowedFields) { | ||
| if (key in patchData) { | ||
|
|
@@ -388,8 +431,6 @@ export class AiWorkflowService { | |
| workflowRunId: runId, | ||
| scorecardQuestionId: item.scorecardQuestionId, | ||
| content: item.content, | ||
| upVotes: item.upVotes ?? 0, | ||
| downVotes: item.downVotes ?? 0, | ||
| questionScore: item.questionScore ?? null, | ||
| createdAt: new Date(), | ||
| // TODO: Remove this once prisma middleware implementation is done | ||
|
|
@@ -819,15 +860,6 @@ export class AiWorkflowService { | |
| ); | ||
| } | ||
|
|
||
| const updateData: any = {}; | ||
|
|
||
| if (patchData.upVotes !== undefined) { | ||
| updateData.upVotes = patchData.upVotes; | ||
| } | ||
| if (patchData.downVotes !== undefined) { | ||
| updateData.downVotes = patchData.downVotes; | ||
| } | ||
|
|
||
| if (!user.isMachine) { | ||
| const keys = Object.keys(patchData); | ||
| const prohibitedKeys = ['content', 'questionScore']; | ||
|
|
@@ -838,21 +870,70 @@ export class AiWorkflowService { | |
| } | ||
| } | ||
|
|
||
| // Update properties which can be updated only via m2m | ||
| if (patchData.upVote !== undefined || patchData.downVote !== undefined) { | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Remove existing votes by this user for this item | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!user.userId) { | ||
| throw new BadRequestException('User id is not available'); | ||
| } | ||
|
|
||
| await this.prisma.aiWorkflowRunItemVote.deleteMany({ | ||
| where: { | ||
| workflowRunItemId: itemId, | ||
| createdBy: user.userId.toString(), | ||
| }, | ||
| }); | ||
|
|
||
| // Add new vote if upVote or downVote is true | ||
| if (patchData.upVote) { | ||
| await this.prisma.aiWorkflowRunItemVote.create({ | ||
| data: { | ||
| workflowRunItemId: itemId, | ||
| voteType: VoteType.UPVOTE, | ||
| createdBy: user.userId.toString(), | ||
| }, | ||
| }); | ||
| } else if (patchData.downVote) { | ||
| await this.prisma.aiWorkflowRunItemVote.create({ | ||
| data: { | ||
| workflowRunItemId: itemId, | ||
| voteType: VoteType.DOWNVOTE, | ||
| createdBy: user.userId.toString(), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| delete patchData.downVote; | ||
| delete patchData.upVote; | ||
| } | ||
|
|
||
| // Update other properties only allowed for machine users | ||
| const updateData: any = {}; | ||
| if (user.isMachine) { | ||
| if (patchData.content) { | ||
| updateData.content = patchData.content; | ||
| } | ||
|
|
||
| if (patchData.questionScore) { | ||
| updateData.questionScore = patchData.questionScore; | ||
| } | ||
| } | ||
|
|
||
| // If there are no other fields to update | ||
| // just return the run item | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (Object.keys(updateData).length === 0) { | ||
| return this.prisma.aiWorkflowRunItem.findUnique({ | ||
| where: { id: itemId }, | ||
| include: { | ||
| comments: true, | ||
| votes: true, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| return this.prisma.aiWorkflowRunItem.update({ | ||
| where: { id: itemId }, | ||
| include: { | ||
| comments: true, | ||
| votes: true, | ||
| }, | ||
| data: updateData, | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import { | |
| IsUUID, | ||
| Max, | ||
| IsEmpty, | ||
| IsBoolean, | ||
| } from 'class-validator'; | ||
| import { Type, Transform } from 'class-transformer'; | ||
|
|
||
|
|
@@ -134,15 +135,13 @@ export class CreateAiWorkflowRunItemDto { | |
|
|
||
| @ApiProperty({ required: false }) | ||
| @IsOptional() | ||
| @IsInt() | ||
| @Min(0) | ||
| upVotes?: number; | ||
| @IsBoolean() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| upVote?: boolean; | ||
|
|
||
| @ApiProperty({ required: false }) | ||
| @IsOptional() | ||
| @IsInt() | ||
| @Min(0) | ||
| downVotes?: number; | ||
| @IsBoolean() | ||
| downVote?: boolean; | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @ApiProperty({ required: false }) | ||
| @IsOptional() | ||
|
|
@@ -202,14 +201,14 @@ export class UpdateRunItemCommentDto { | |
| content?: string; | ||
|
|
||
| @ApiProperty({ required: false }) | ||
| @IsInt() | ||
| @IsBoolean() | ||
| @IsOptional() | ||
| upVotes?: number; | ||
| upVote?: boolean; | ||
|
|
||
| @ApiProperty({ required: false }) | ||
| @IsInt() | ||
| @IsBoolean() | ||
| @IsOptional() | ||
| downVotes?: number; | ||
| downVote?: boolean; | ||
|
|
||
| @ApiHideProperty() | ||
| @IsEmpty({ message: 'parentId cannot be updated' }) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.