[resumes][feat] Add resume-comments type

pull/320/head
Terence Ho 3 years ago
parent 29ec081180
commit 17c68852c2

@ -2,6 +2,8 @@ import { z } from 'zod';
import { createRouter } from './context'; import { createRouter } from './context';
import type { ResumeComment } from '~/types/resume-comments';
export const resumeReviewsRouter = createRouter().query('list', { export const resumeReviewsRouter = createRouter().query('list', {
input: z.object({ input: z.object({
resumeId: z.string(), resumeId: z.string(),
@ -13,7 +15,7 @@ export const resumeReviewsRouter = createRouter().query('list', {
// For this resume, we retrieve every comment's information, along with: // For this resume, we retrieve every comment's information, along with:
// The user's name and image to render // The user's name and image to render
// Number of votes, and whether the user (if-any) has voted // 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: { include: {
_count: { _count: {
select: { select: {
@ -40,5 +42,29 @@ export const resumeReviewsRouter = createRouter().query('list', {
resumeId, resumeId,
}, },
}); });
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,
resumesProfileId: data.resumesProfileId,
section: data.section,
updatedAt: data.updatedAt,
user: {
image: data.resumesProfile.user.image,
name: data.resumesProfile.user.name,
userId: data.resumesProfile.userId,
},
};
return comment;
});
}, },
}); });

@ -1,11 +1,10 @@
import type { ResumesSection } from '@prisma/client'; import type { ResumesSection } from '@prisma/client';
declare module 'resume-comments' {
/** /**
* Returned by `resumeReviewsRouter` (query for 'resumes.reviews.list') and received as prop by `Comment` in `CommentsList` * Returned by `resumeReviewsRouter` (query for 'resumes.reviews.list') and received as prop by `Comment` in `CommentsList`
* frontend-friendly representation of the query * frontend-friendly representation of the query
*/ */
type ResumeComment = { export type ResumeComment = {
createdAt: Date; createdAt: Date;
description: string; description: string;
hasVoted: boolean; hasVoted: boolean;
@ -16,9 +15,8 @@ declare module 'resume-comments' {
section: ResumesSection; section: ResumesSection;
updatedAt: Date; updatedAt: Date;
user: { user: {
image: string; image: string?;
name: string; name: string?;
userId: string; userId: string;
}; };
}; };
}

Loading…
Cancel
Save