[resumes][feat] Add resume comment upvote/downvote (#389)
* [resumes][feat] Add upvote/downvote * [resumes][refactor] abstract comment votes fetching from comments * [resumes][chore] remove votes from comments query Co-authored-by: Terence Ho <>pull/394/head
parent
925ba937b4
commit
22805022a9
@ -0,0 +1,38 @@
|
||||
import { z } from 'zod';
|
||||
import type { ResumesCommentVote } from '@prisma/client';
|
||||
import { Vote } from '@prisma/client';
|
||||
|
||||
import { createRouter } from '../context';
|
||||
|
||||
import type { ResumeCommentVote } from '~/types/resume-comments';
|
||||
|
||||
export const resumesCommentsVotesRouter = createRouter().query('list', {
|
||||
input: z.object({
|
||||
commentId: z.string(),
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
const userId = ctx.session?.user?.id;
|
||||
const { commentId } = input;
|
||||
|
||||
const votes = await ctx.prisma.resumesCommentVote.findMany({
|
||||
where: {
|
||||
commentId,
|
||||
},
|
||||
});
|
||||
|
||||
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 resumeCommentVote: ResumeCommentVote = {
|
||||
numVotes,
|
||||
userVote,
|
||||
};
|
||||
|
||||
return resumeCommentVote;
|
||||
},
|
||||
});
|
@ -0,0 +1,45 @@
|
||||
import { z } from 'zod';
|
||||
import { Vote } from '@prisma/client';
|
||||
|
||||
import { createProtectedRouter } from '../context';
|
||||
|
||||
export const resumesCommentsVotesUserRouter = createProtectedRouter()
|
||||
.mutation('upsert', {
|
||||
input: z.object({
|
||||
commentId: z.string(),
|
||||
value: z.nativeEnum(Vote),
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
const userId = ctx.session.user.id;
|
||||
const { commentId, value } = input;
|
||||
|
||||
await ctx.prisma.resumesCommentVote.upsert({
|
||||
create: {
|
||||
commentId,
|
||||
userId,
|
||||
value,
|
||||
},
|
||||
update: {
|
||||
value,
|
||||
},
|
||||
where: {
|
||||
userId_commentId: { commentId, userId },
|
||||
},
|
||||
});
|
||||
},
|
||||
})
|
||||
.mutation('delete', {
|
||||
input: z.object({
|
||||
commentId: z.string(),
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
const userId = ctx.session.user.id;
|
||||
const { commentId } = input;
|
||||
|
||||
await ctx.prisma.resumesCommentVote.delete({
|
||||
where: {
|
||||
userId_commentId: { commentId, userId },
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
Loading…
Reference in new issue