From 0335616d5750965920355d5047098955167c86f3 Mon Sep 17 00:00:00 2001 From: hpkoh <53825802+hpkoh@users.noreply.github.com> Date: Mon, 10 Oct 2022 03:46:51 +0800 Subject: [PATCH] [questions][feat] add answer comment crud (#332) Co-authored-by: Jeff Sieu --- apps/portal/src/server/router/index.ts | 2 + .../router/questions-answer-comment-router.ts | 229 ++++++++++++++++++ apps/portal/src/types/questions.d.ts | 6 + 3 files changed, 237 insertions(+) create mode 100644 apps/portal/src/server/router/questions-answer-comment-router.ts diff --git a/apps/portal/src/server/router/index.ts b/apps/portal/src/server/router/index.ts index 878ec632..aa8446ca 100644 --- a/apps/portal/src/server/router/index.ts +++ b/apps/portal/src/server/router/index.ts @@ -3,6 +3,7 @@ import superjson from 'superjson'; import { companiesRouter } from './companies-router'; import { createRouter } from './context'; import { protectedExampleRouter } from './protected-example-router'; +import { questionsAnswerCommentRouter } from './questions-answer-comment-router'; import { questionsAnswerRouter } from './questions-answer-router'; import { questionsQuestionCommentRouter } from './questions-question-comment-router'; import { questionsQuestionRouter } from './questions-question-router'; @@ -28,6 +29,7 @@ export const appRouter = createRouter() .merge('resumes.star.user.', resumesStarUserRouter) .merge('resumes.reviews.', resumeReviewsRouter) .merge('resumes.reviews.user.', resumesReviewsUserRouter) + .merge('questions.answers.comments.', questionsAnswerCommentRouter) .merge('questions.answers.', questionsAnswerRouter) .merge('questions.questions.comments.', questionsQuestionCommentRouter) .merge('questions.questions.', questionsQuestionRouter); diff --git a/apps/portal/src/server/router/questions-answer-comment-router.ts b/apps/portal/src/server/router/questions-answer-comment-router.ts new file mode 100644 index 00000000..b4578cc5 --- /dev/null +++ b/apps/portal/src/server/router/questions-answer-comment-router.ts @@ -0,0 +1,229 @@ +import { z } from 'zod'; +import { Vote } from '@prisma/client'; +import { TRPCError } from '@trpc/server'; + +import { createProtectedRouter } from './context'; + +import type { AnswerComment } from '~/types/questions'; + +export const questionsAnswerCommentRouter = createProtectedRouter() + .query('getAnswerComments', { + input: z.object({ + answerId: z.string(), + }), + async resolve({ ctx, input }) { + const questionAnswerCommentsData = await ctx.prisma.questionsAnswerComment.findMany({ + include: { + user: { + select: { + name: true, + }, + }, + votes: true, + }, + orderBy: { + createdAt: 'desc', + }, + where: { + ...input, + }, + }); + return questionAnswerCommentsData.map((data) => { + const votes:number = data.votes.reduce( + (previousValue:number, currentValue) => { + let result:number = previousValue; + + switch(currentValue.vote) { + case Vote.UPVOTE: + result += 1 + break; + case Vote.DOWNVOTE: + result -= 1 + break; + } + return result; + }, + 0 + ); + + let userName = ""; + + if (data.user) { + userName = data.user.name!; + } + + + const answerComment: AnswerComment = { + content: data.content, + id: data.id, + numVotes: votes, + updatedAt: data.updatedAt, + user: userName, + }; + return answerComment; + }); + } + }) + .mutation('create', { + input: z.object({ + answerId: z.string(), + content: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + return await ctx.prisma.questionsAnswerComment.create({ + data: { + ...input, + userId, + }, + }); + }, + }) + .mutation('update', { + input: z.object({ + content: z.string().optional(), + id: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + const answerCommentToUpdate = await ctx.prisma.questionsAnswerComment.findUnique({ + where: { + id: input.id, + }, + }); + + if (answerCommentToUpdate?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsAnswerComment.update({ + data: { + ...input, + }, + where: { + id: input.id, + }, + }); + }, + }) + .mutation('delete', { + input: z.object({ + id: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + const answerCommentToDelete = await ctx.prisma.questionsAnswerComment.findUnique({ + where: { + id: input.id, + }, + }); + + if (answerCommentToDelete?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsAnswerComment.delete({ + where: { + id: input.id, + }, + }); + }, + }) + .query('getVote', { + input: z.object({ + answerCommentId: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + const {answerCommentId} = input + + return await ctx.prisma.questionsAnswerCommentVote.findUnique({ + where: { + answerCommentId_userId : {answerCommentId,userId }, + }, + }); + }, + }) + .mutation('createVote', { + input: z.object({ + answerCommentId: z.string(), + vote: z.nativeEnum(Vote), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + return await ctx.prisma.questionsAnswerCommentVote.create({ + data: { + ...input, + userId, + }, + }); + }, + }) + .mutation('updateVote', { + input: z.object({ + id: z.string(), + vote: z.nativeEnum(Vote), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + const {id, vote} = input + + const voteToUpdate = await ctx.prisma.questionsAnswerCommentVote.findUnique({ + where: { + id: input.id, + }, + }); + + if (voteToUpdate?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsAnswerCommentVote.update({ + data: { + vote, + }, + where: { + id, + }, + }); + }, + }) + .mutation('deleteVote', { + input: z.object({ + id: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + const voteToDelete = await ctx.prisma.questionsAnswerCommentVote.findUnique({ + where: { + id: input.id, + },}); + + if (voteToDelete?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsAnswerCommentVote.delete({ + where: { + id: input.id, + }, + }); + }, + }); \ No newline at end of file diff --git a/apps/portal/src/types/questions.d.ts b/apps/portal/src/types/questions.d.ts index 36aef367..0d60ca76 100644 --- a/apps/portal/src/types/questions.d.ts +++ b/apps/portal/src/types/questions.d.ts @@ -12,6 +12,12 @@ export type Question = { user: string; }; +export type AnswerComment = { + content: string; + id: string; + numVotes: number; +}; + export type Answer = { content: string; createdAt: Date;