[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,24 +1,22 @@
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 */
*/ export type ResumeComment = {
type ResumeComment = { createdAt: Date;
createdAt: Date; description: string;
description: string; hasVoted: boolean;
hasVoted: boolean; id: string;
id: string; numVotes: number;
numVotes: number; resumeId: string;
resumeId: string; resumesProfileId: string;
resumesProfileId: string; 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