diff --git a/apps/portal/src/server/router/questions/questions-answer-comment-user-router.ts b/apps/portal/src/server/router/questions/questions-answer-comment-user-router.ts index c2bb0089..e9d77a73 100644 --- a/apps/portal/src/server/router/questions/questions-answer-comment-user-router.ts +++ b/apps/portal/src/server/router/questions/questions-answer-comment-user-router.ts @@ -109,6 +109,20 @@ export const questionsAnswerCommentUserRouter = createProtectedRouter() const { answerCommentId } = input; return await ctx.prisma.$transaction(async (tx) => { + const answerCommentToUpdate = + await tx.questionsAnswerComment.findUnique({ + where: { + id: answerCommentId, + }, + }); + + if (answerCommentToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Answer Comment do not exist.', + }); + } + const vote = await tx.questionsAnswerCommentVote.findUnique({ where: { answerCommentId_userId: { answerCommentId, userId }, @@ -189,6 +203,20 @@ export const questionsAnswerCommentUserRouter = createProtectedRouter() const { answerCommentId } = input; return await ctx.prisma.$transaction(async (tx) => { + const answerCommentToUpdate = + await tx.questionsAnswerComment.findUnique({ + where: { + id: answerCommentId, + }, + }); + + if (answerCommentToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Answer Comment do not exist.', + }); + } + const vote = await tx.questionsAnswerCommentVote.findUnique({ where: { answerCommentId_userId: { answerCommentId, userId }, @@ -269,6 +297,20 @@ export const questionsAnswerCommentUserRouter = createProtectedRouter() const { answerCommentId } = input; return await ctx.prisma.$transaction(async (tx) => { + const answerCommentToUpdate = + await tx.questionsAnswerComment.findUnique({ + where: { + id: answerCommentId, + }, + }); + + if (answerCommentToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Answer Comment do not exist.', + }); + } + const voteToDelete = await tx.questionsAnswerCommentVote.findUnique({ where: { answerCommentId_userId: { answerCommentId, userId }, diff --git a/apps/portal/src/server/router/questions/questions-answer-user-router.ts b/apps/portal/src/server/router/questions/questions-answer-user-router.ts index 24597eaa..6418d500 100644 --- a/apps/portal/src/server/router/questions/questions-answer-user-router.ts +++ b/apps/portal/src/server/router/questions/questions-answer-user-router.ts @@ -39,7 +39,7 @@ export const questionsAnswerUserRouter = createProtectedRouter() }, }); - if (answerToUpdate?.id !== userId) { + if (answerToUpdate?.userId !== userId) { throw new TRPCError({ code: 'UNAUTHORIZED', message: 'User have no authorization to record.', @@ -69,7 +69,7 @@ export const questionsAnswerUserRouter = createProtectedRouter() }, }); - if (answerToDelete?.id !== userId) { + if (answerToDelete?.userId !== userId) { throw new TRPCError({ code: 'UNAUTHORIZED', message: 'User have no authorization to record.', @@ -107,6 +107,20 @@ export const questionsAnswerUserRouter = createProtectedRouter() const { answerId } = input; return await ctx.prisma.$transaction(async (tx) => { + const answerToUpdate = + await tx.questionsAnswer.findUnique({ + where: { + id: answerId, + }, + }); + + if (answerToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Answer do not exist.', + }); + } + const vote = await tx.questionsAnswerVote.findUnique({ where: { answerId_userId: { answerId, userId }, @@ -187,6 +201,20 @@ export const questionsAnswerUserRouter = createProtectedRouter() const { answerId } = input; return await ctx.prisma.$transaction(async (tx) => { + const answerToUpdate = + await tx.questionsAnswer.findUnique({ + where: { + id: answerId, + }, + }); + + if (answerToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Answer do not exist.', + }); + } + const vote = await tx.questionsAnswerVote.findUnique({ where: { answerId_userId: { answerId, userId }, @@ -267,6 +295,20 @@ export const questionsAnswerUserRouter = createProtectedRouter() const { answerId } = input; return await ctx.prisma.$transaction(async (tx) => { + const answerToUpdate = + await tx.questionsAnswer.findUnique({ + where: { + id: answerId, + }, + }); + + if (answerToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Answer do not exist.', + }); + } + const voteToDelete = await tx.questionsAnswerVote.findUnique({ where: { answerId_userId: { answerId, userId }, diff --git a/apps/portal/src/server/router/questions/questions-question-comment-user-router.ts b/apps/portal/src/server/router/questions/questions-question-comment-user-router.ts index e2cc34c6..5066197e 100644 --- a/apps/portal/src/server/router/questions/questions-question-comment-user-router.ts +++ b/apps/portal/src/server/router/questions/questions-question-comment-user-router.ts @@ -110,6 +110,20 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter() const { questionCommentId } = input; return await ctx.prisma.$transaction(async (tx) => { + const questionCommentToUpdate = + await tx.questionsQuestionComment.findUnique({ + where: { + id: questionCommentId, + }, + }); + + if (questionCommentToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Question Comment do not exist.', + }); + } + const vote = await tx.questionsQuestionCommentVote.findUnique({ where: { questionCommentId_userId: { questionCommentId, userId }, @@ -190,6 +204,20 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter() const { questionCommentId } = input; return await ctx.prisma.$transaction(async (tx) => { + const questionCommentToUpdate = + await tx.questionsQuestionComment.findUnique({ + where: { + id: questionCommentId, + }, + }); + + if (questionCommentToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Question Comment do not exist.', + }); + } + const vote = await tx.questionsQuestionCommentVote.findUnique({ where: { questionCommentId_userId: { questionCommentId, userId }, @@ -270,6 +298,20 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter() const { questionCommentId } = input; return await ctx.prisma.$transaction(async (tx) => { + const questionCommentToUpdate = + await tx.questionsQuestionComment.findUnique({ + where: { + id: questionCommentId, + }, + }); + + if (questionCommentToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Question Comment do not exist.', + }); + } + const voteToDelete = await tx.questionsQuestionCommentVote.findUnique({ where: { questionCommentId_userId: { questionCommentId, userId }, diff --git a/apps/portal/src/server/router/questions/questions-question-user-router.ts b/apps/portal/src/server/router/questions/questions-question-user-router.ts index 7f8050c7..25a53d0b 100644 --- a/apps/portal/src/server/router/questions/questions-question-user-router.ts +++ b/apps/portal/src/server/router/questions/questions-question-user-router.ts @@ -132,6 +132,20 @@ export const questionsQuestionUserRouter = createProtectedRouter() const { questionId } = input; return await ctx.prisma.$transaction(async (tx) => { + const questionToUpdate = + await tx.questionsQuestion.findUnique({ + where: { + id: questionId, + }, + }); + + if (questionToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Question do not exist.', + }); + } + const vote = await tx.questionsQuestionVote.findUnique({ where: { questionId_userId: { questionId, userId }, @@ -212,6 +226,20 @@ export const questionsQuestionUserRouter = createProtectedRouter() const { questionId } = input; return await ctx.prisma.$transaction(async (tx) => { + const questionToUpdate = + await tx.questionsQuestion.findUnique({ + where: { + id: questionId, + }, + }); + + if (questionToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Question do not exist.', + }); + } + const vote = await tx.questionsQuestionVote.findUnique({ where: { questionId_userId: { questionId, userId }, @@ -292,6 +320,20 @@ export const questionsQuestionUserRouter = createProtectedRouter() const { questionId } = input; return await ctx.prisma.$transaction(async (tx) => { + const questionToUpdate = + await tx.questionsQuestion.findUnique({ + where: { + id: questionId, + }, + }); + + if (questionToUpdate === null) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Question do not exist.', + }); + } + const voteToDelete = await tx.questionsQuestionVote.findUnique({ where: { questionId_userId: { questionId, userId },