diff --git a/apps/portal/src/server/router/resumes-reviews-router.ts b/apps/portal/src/server/router/resumes-reviews-router.ts index 8e681326..24c7c830 100644 --- a/apps/portal/src/server/router/resumes-reviews-router.ts +++ b/apps/portal/src/server/router/resumes-reviews-router.ts @@ -2,6 +2,8 @@ import { z } from 'zod'; import { createRouter } from './context'; +import type { ResumeComment } from '~/types/resume-comments'; + export const resumeReviewsRouter = createRouter().query('list', { input: z.object({ resumeId: z.string(), @@ -13,7 +15,7 @@ export const resumeReviewsRouter = createRouter().query('list', { // 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: { @@ -40,5 +42,29 @@ export const resumeReviewsRouter = createRouter().query('list', { 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; + }); }, }); diff --git a/apps/portal/src/types/resume-comments.d.ts b/apps/portal/src/types/resume-comments.d.ts index 6c55947e..7f8397a3 100644 --- a/apps/portal/src/types/resume-comments.d.ts +++ b/apps/portal/src/types/resume-comments.d.ts @@ -1,24 +1,22 @@ 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` - * frontend-friendly representation of the query - */ - type ResumeComment = { - createdAt: Date; - description: string; - hasVoted: boolean; - id: string; - numVotes: number; - resumeId: string; - resumesProfileId: string; - section: ResumesSection; - updatedAt: Date; - user: { - image: string; - name: string; - userId: string; - }; +/** + * Returned by `resumeReviewsRouter` (query for 'resumes.reviews.list') and received as prop by `Comment` in `CommentsList` + * frontend-friendly representation of the query + */ +export type ResumeComment = { + createdAt: Date; + description: string; + hasVoted: boolean; + id: string; + numVotes: number; + resumeId: string; + resumesProfileId: string; + section: ResumesSection; + updatedAt: Date; + user: { + image: string?; + name: string?; + userId: string; }; -} +};