diff --git a/src/Type_Definitions/Comment_Comment.js b/src/Type_Definitions/Comment_Comment.js
index dc744e4..c16f8ba 100644
--- a/src/Type_Definitions/Comment_Comment.js
+++ b/src/Type_Definitions/Comment_Comment.js
@@ -46,6 +46,7 @@ const commentDefs = gql`
input VoteCommentInput {
commentId: ID!
action: voteCommentAction!
+ userId: ID!
}
input CreateCommentInput {
@@ -77,7 +78,7 @@ const commentDefs = gql`
votes: Int!
children: [Comment]
- userVotePost: [String]!
+ userVoteComment: [String]!
}
`;
diff --git a/src/prisma/ERD.svg b/src/prisma/ERD.svg
index 008d66f..e09bb50 100644
--- a/src/prisma/ERD.svg
+++ b/src/prisma/ERD.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/src/prisma/schema.prisma b/src/prisma/schema.prisma
index 53e2a5b..242b707 100644
--- a/src/prisma/schema.prisma
+++ b/src/prisma/schema.prisma
@@ -229,6 +229,7 @@ model Comment {
children Comment[] @relation(name: "Comment_Children")
votes Int @default(0)
+ userVoteComment String[]
}
model Contest {
diff --git a/src/resolvers/Mutation/comment.js b/src/resolvers/Mutation/comment.js
index ba0b7cc..9a0815f 100644
--- a/src/resolvers/Mutation/comment.js
+++ b/src/resolvers/Mutation/comment.js
@@ -87,12 +87,12 @@ const commentMutation = {
/**
*
* @param {*} parent
- * @param {{data: {commentId: string, action: UPVOTE | DOWNVOTE}}} args
+ * @param {{data: {commentId: string, userId: string, action: UPVOTE | DOWNVOTE}}} args
* @param {*} info
* @returns
*/
voteComment: async (parent, args, info) => {
- const { action, commentId } = args.data;
+ const { action, commentId, userId } = args.data;
const existedComment = await prisma.comment.findUnique({
where: {
@@ -104,6 +104,10 @@ const commentMutation = {
throw Error('Comment Not Exsited');
}
+ if (existedComment.userVoteComment.length > 0 && existedComment.userVoteComment.includes(userId)) {
+ throw Error('User has voted this comment already');
+ }
+
switch (action) {
case VOTE_COMMENT_ACTION.DOWNVOTE:
return await prisma.comment.update({
@@ -112,6 +116,9 @@ const commentMutation = {
},
data: {
votes: existedComment.votes - 1,
+ userVoteComment: {
+ push: userId
+ }
},
});
@@ -122,6 +129,9 @@ const commentMutation = {
},
data: {
votes: existedComment.votes + 1,
+ userVoteComment: {
+ push: userId
+ }
},
});
default: