From 925ba937b4fa03cf5dd12bddb6b4dedec61cfdc4 Mon Sep 17 00:00:00 2001 From: hpkoh <53825802+hpkoh@users.noreply.github.com> Date: Wed, 19 Oct 2022 15:12:56 +0800 Subject: [PATCH] [questions][refactor]: refactor queries (#383) --- .../router/questions-answer-comment-router.ts | 15 +++-- .../server/router/questions-answer-router.ts | 14 ++++- .../questions-question-comment-router.ts | 16 ++++-- .../router/questions-question-router.ts | 55 +++++++++++-------- 4 files changed, 67 insertions(+), 33 deletions(-) diff --git a/apps/portal/src/server/router/questions-answer-comment-router.ts b/apps/portal/src/server/router/questions-answer-comment-router.ts index f75195c7..7ed3e6e2 100644 --- a/apps/portal/src/server/router/questions-answer-comment-router.ts +++ b/apps/portal/src/server/router/questions-answer-comment-router.ts @@ -27,7 +27,7 @@ export const questionsAnswerCommentRouter = createProtectedRouter() createdAt: 'desc', }, where: { - ...input, + answerId : input.answerId, }, }); return questionAnswerCommentsData.map((data) => { @@ -69,9 +69,12 @@ export const questionsAnswerCommentRouter = createProtectedRouter() async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { answerId, content } = input; + return await ctx.prisma.questionsAnswerComment.create({ data: { - ...input, + answerId, + content, userId, }, }); @@ -99,9 +102,10 @@ export const questionsAnswerCommentRouter = createProtectedRouter() }); } + const { content } = input; return await ctx.prisma.questionsAnswerComment.update({ data: { - ...input, + content, }, where: { id: input.id, @@ -160,10 +164,13 @@ export const questionsAnswerCommentRouter = createProtectedRouter() async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { answerCommentId, vote } = input; + return await ctx.prisma.questionsAnswerCommentVote.create({ data: { - ...input, + answerCommentId, userId, + vote, }, }); }, diff --git a/apps/portal/src/server/router/questions-answer-router.ts b/apps/portal/src/server/router/questions-answer-router.ts index 7dadbeb4..5d386854 100644 --- a/apps/portal/src/server/router/questions-answer-router.ts +++ b/apps/portal/src/server/router/questions-answer-router.ts @@ -12,6 +12,8 @@ export const questionsAnswerRouter = createProtectedRouter() questionId: z.string(), }), async resolve({ ctx, input }) { + const { questionId } = input; + const answersData = await ctx.prisma.questionsAnswer.findMany({ include: { _count: { @@ -31,7 +33,7 @@ export const questionsAnswerRouter = createProtectedRouter() createdAt: 'desc', }, where: { - ...input, + questionId, }, }); return answersData.map((data) => { @@ -132,9 +134,12 @@ export const questionsAnswerRouter = createProtectedRouter() async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { content, questionId } = input; + return await ctx.prisma.questionsAnswer.create({ data: { - ...input, + content, + questionId, userId, }, }); @@ -222,10 +227,13 @@ export const questionsAnswerRouter = createProtectedRouter() async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { answerId, vote } = input; + return await ctx.prisma.questionsAnswerVote.create({ data: { - ...input, + answerId, userId, + vote, }, }); }, diff --git a/apps/portal/src/server/router/questions-question-comment-router.ts b/apps/portal/src/server/router/questions-question-comment-router.ts index f3985f31..e2f786f9 100644 --- a/apps/portal/src/server/router/questions-question-comment-router.ts +++ b/apps/portal/src/server/router/questions-question-comment-router.ts @@ -12,6 +12,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter() questionId: z.string(), }), async resolve({ ctx, input }) { + const { questionId } = input; const questionCommentsData = await ctx.prisma.questionsQuestionComment.findMany({ include: { @@ -27,7 +28,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter() createdAt: 'desc', }, where: { - ...input, + questionId, }, }); return questionCommentsData.map((data) => { @@ -68,9 +69,12 @@ export const questionsQuestionCommentRouter = createProtectedRouter() async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { content, questionId } = input; + return await ctx.prisma.questionsQuestionComment.create({ data: { - ...input, + content, + questionId, userId, }, }); @@ -84,6 +88,8 @@ export const questionsQuestionCommentRouter = createProtectedRouter() async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { content } = input; + const questionCommentToUpdate = await ctx.prisma.questionsQuestionComment.findUnique({ where: { @@ -100,7 +106,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter() return await ctx.prisma.questionsQuestionComment.update({ data: { - ...input, + content, }, where: { id: input.id, @@ -158,11 +164,13 @@ export const questionsQuestionCommentRouter = createProtectedRouter() }), async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { questionCommentId, vote } = input; return await ctx.prisma.questionsQuestionCommentVote.create({ data: { - ...input, + questionCommentId, userId, + vote, }, }); }, diff --git a/apps/portal/src/server/router/questions-question-router.ts b/apps/portal/src/server/router/questions-question-router.ts index 2cb564d3..7fbf59ed 100644 --- a/apps/portal/src/server/router/questions-question-router.ts +++ b/apps/portal/src/server/router/questions-question-router.ts @@ -51,29 +51,34 @@ export const questionsQuestionRouter = createProtectedRouter() }, } : {}), + encounters : { + some: { + ...(input.companies.length > 0 + ? { + company : { + in : input.companies + } + } + : {}), + ...(input.locations.length > 0 + ? { + location: { + in: input.locations + }, + } + : {}), + ...(input.roles.length > 0 + ? { + role : { + in: input.roles + } + } + : {}), + } + } }, }); return questionsData - .filter((data) => { - for (let i = 0; i < data.encounters.length; i++) { - const encounter = data.encounters[i]; - const matchCompany = - input.companies.length === 0 || - input.companies.includes(encounter.company); - const matchLocation = - input.locations.length === 0 || - input.locations.includes(encounter.location); - const matchRole = - input.roles.length === 0 || input.roles.includes(encounter.role); - const matchDate = - (!input.startDate || encounter.seenAt >= input.startDate) && - encounter.seenAt <= input.endDate; - if (matchCompany && matchLocation && matchRole && matchDate) { - return true; - } - } - return false; - }) .map((data) => { const votes: number = data.votes.reduce( (previousValue: number, currentValue) => { @@ -251,9 +256,13 @@ export const questionsQuestionRouter = createProtectedRouter() }); } + const { content, questionType } = input; + return await ctx.prisma.questionsQuestion.update({ + data: { - ...input, + content, + questionType, }, where: { id: input.id, @@ -311,11 +320,13 @@ export const questionsQuestionRouter = createProtectedRouter() }), async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; + const { questionId, vote } = input; return await ctx.prisma.questionsQuestionVote.create({ data: { - ...input, + questionId, userId, + vote, }, }); },