[resumes][fix] update vote query (#436)

* [resumes][fix] update vote query

* [resumes][fix] lint issues

Co-authored-by: Terence Ho <>
pull/437/head
Terence 2 years ago committed by GitHub
parent 248c1e3584
commit b3d110f90f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,4 @@
import { z } from 'zod'; import { z } from 'zod';
import type { ResumesCommentVote } from '@prisma/client';
import { Vote } from '@prisma/client'; import { Vote } from '@prisma/client';
import { createRouter } from '../context'; import { createRouter } from '../context';
@ -20,13 +19,13 @@ export const resumesCommentsVotesRouter = createRouter().query('list', {
}, },
}); });
let userVote: ResumesCommentVote | null = null; const userVotes = votes.filter((vote) => vote.userId === userId);
let numVotes = 0; const userVote = userVotes.length > 0 ? userVotes[0] : null;
const numVotes = votes
votes.forEach((vote) => { .map((vote) => (vote.value === Vote.UPVOTE ? 1 : -1))
numVotes += vote.value === Vote.UPVOTE ? 1 : -1; .reduce((result, current) => {
userVote = vote.userId === userId ? vote : null; return result + current;
}); }, 0);
const resumeCommentVote: ResumeCommentVote = { const resumeCommentVote: ResumeCommentVote = {
numVotes, numVotes,

Loading…
Cancel
Save