diff --git a/apps/portal/src/server/router/index.ts b/apps/portal/src/server/router/index.ts index e8e6713e..94eecbe8 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 { questionsQuestionCommentRouter } from './questions-question-comment-router'; import { questionsQuestionRouter } from './questions-question-router'; import { resumesRouter } from './resumes/resumes-resume-router'; import { resumesResumeUserRouter } from './resumes/resumes-resume-user-router'; @@ -26,6 +27,7 @@ export const appRouter = createRouter() .merge('resumes.star.user.', resumesStarUserRouter) .merge('resumes.reviews.', resumeReviewsRouter) .merge('resumes.reviews.user.', resumesReviewsUserRouter) + .merge('questions.questions.comments.', questionsQuestionCommentRouter) .merge('questions.questions.', questionsQuestionRouter); // Export type definition of API diff --git a/apps/portal/src/server/router/questions-question-comment-router.ts b/apps/portal/src/server/router/questions-question-comment-router.ts new file mode 100644 index 00000000..12f2bc21 --- /dev/null +++ b/apps/portal/src/server/router/questions-question-comment-router.ts @@ -0,0 +1,228 @@ +import { z } from 'zod'; +import { Vote } from '@prisma/client'; +import { TRPCError } from '@trpc/server'; + +import { createProtectedRouter } from './context'; + +import type { QuestionComment } from '~/types/questions'; + +export const questionsQuestionCommentRouter = createProtectedRouter() + .query('getQuestionComments', { + input: z.object({ + questionId: z.string(), + }), + async resolve({ ctx, input }) { + const questionCommentsData = await ctx.prisma.questionsQuestionComment.findMany({ + include: { + user: { + select: { + name: true, + }, + }, + votes: true, + }, + orderBy: { + createdAt: 'desc', + }, + where: { + ...input, + }, + }); + return questionCommentsData.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 questionComment: QuestionComment = { + content: data.content, + createdAt: data.createdAt, + id: data.id, + numVotes: votes, + user: userName, + }; + return questionComment; + }); + } + }) + .mutation('create', { + input: z.object({ + content: z.string(), + questionId: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + return await ctx.prisma.questionsQuestionComment.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 questionCommentToUpdate = await ctx.prisma.questionsQuestionComment.findUnique({ + where: { + id: input.id, + }, + }); + + if (questionCommentToUpdate?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsQuestionComment.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 questionCommentToDelete = await ctx.prisma.questionsQuestionComment.findUnique({ + where: { + id: input.id, + }, + }); + + if (questionCommentToDelete?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsQuestionComment.delete({ + where: { + id: input.id, + }, + }); + }, + }) + .query('getVote', { + input: z.object({ + questionCommentId: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + const {questionCommentId} = input + + return await ctx.prisma.questionsQuestionCommentVote.findUnique({ + where: { + questionCommentId_userId : {questionCommentId,userId }, + }, + }); + }, + }) + .mutation('createVote', { + input: z.object({ + questionCommentId: z.string(), + vote: z.nativeEnum(Vote), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + return await ctx.prisma.questionsQuestionCommentVote.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.questionsQuestionCommentVote.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.questionsQuestionCommentVote.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.questionsQuestionCommentVote.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.questionsQuestionCommentVote.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 fa72f638..633504ef 100644 --- a/apps/portal/src/types/questions.d.ts +++ b/apps/portal/src/types/questions.d.ts @@ -10,4 +10,11 @@ export type Question = { role: string; updatedAt: Date; user: string; +}; + +export type QuestionComment = { + content: string; + createdAt: Date; + id: string; + numVotes: number; }; \ No newline at end of file