[resumes][feat] fetch comments from database (#320)

* [resumes][feat] Add resume-comments type

* [resumes][feat] Add resume-comments type

* [resumes][feat] Filter comments

* [resumes][feat] Add comments render

* [resumes][refactor] rename variables

* [resumes][refactor] update invalidateQueries

* [resumes][refactor] Use resumeId in [resumeId].tsx

* [resumes][fix] fix invalidateQuery

Co-authored-by: Terence Ho <>
This commit is contained in:
Terence
2022-10-08 00:42:27 +08:00
committed by GitHub
parent b37aae2154
commit d9880dbff1
8 changed files with 197 additions and 17 deletions

View File

@ -1,19 +1,23 @@
import { z } from 'zod';
import { ResumesSection } from '@prisma/client';
import { createRouter } from './context';
import type { ResumeComment } from '~/types/resume-comments';
export const resumeReviewsRouter = createRouter().query('list', {
input: z.object({
resumeId: z.string(),
section: z.nativeEnum(ResumesSection),
}),
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { resumeId } = input;
const { resumeId, section } = input;
// For this resume, we retrieve every comment's information, along with:
// The user's name and image to render
// Number of votes, and whether the user (if-any) has voted
return await ctx.prisma.resumesComment.findMany({
const comments = await ctx.prisma.resumesComment.findMany({
include: {
_count: {
select: {
@ -38,7 +42,31 @@ export const resumeReviewsRouter = createRouter().query('list', {
},
where: {
resumeId,
section,
},
});
return comments.map((data) => {
const hasVoted = data.votes.length > 0;
const numVotes = data._count.votes;
const comment: ResumeComment = {
createdAt: data.createdAt,
description: data.description,
hasVoted,
id: data.id,
numVotes,
resumeId: data.resumeId,
section: data.section,
updatedAt: data.updatedAt,
user: {
image: data.user.image,
name: data.user.name,
userId: data.userId,
},
};
return comment;
});
},
});