From b3d110f90f9df83b2e7c0a55849a2748fbc1f96e Mon Sep 17 00:00:00 2001 From: Terence <45381509+Vielheim@users.noreply.github.com> Date: Wed, 26 Oct 2022 17:55:34 +0800 Subject: [PATCH] [resumes][fix] update vote query (#436) * [resumes][fix] update vote query * [resumes][fix] lint issues Co-authored-by: Terence Ho <> --- .../resumes/resumes-comments-votes-router.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/apps/portal/src/server/router/resumes/resumes-comments-votes-router.ts b/apps/portal/src/server/router/resumes/resumes-comments-votes-router.ts index 5d508c35..7c820971 100644 --- a/apps/portal/src/server/router/resumes/resumes-comments-votes-router.ts +++ b/apps/portal/src/server/router/resumes/resumes-comments-votes-router.ts @@ -1,5 +1,4 @@ import { z } from 'zod'; -import type { ResumesCommentVote } from '@prisma/client'; import { Vote } from '@prisma/client'; import { createRouter } from '../context'; @@ -20,13 +19,13 @@ export const resumesCommentsVotesRouter = createRouter().query('list', { }, }); - let userVote: ResumesCommentVote | null = null; - let numVotes = 0; - - votes.forEach((vote) => { - numVotes += vote.value === Vote.UPVOTE ? 1 : -1; - userVote = vote.userId === userId ? vote : null; - }); + const userVotes = votes.filter((vote) => vote.userId === userId); + const userVote = userVotes.length > 0 ? userVotes[0] : null; + const numVotes = votes + .map((vote) => (vote.value === Vote.UPVOTE ? 1 : -1)) + .reduce((result, current) => { + return result + current; + }, 0); const resumeCommentVote: ResumeCommentVote = { numVotes,