From ecf67bc23d6611c33dbdbaa1337c0425f0a96900 Mon Sep 17 00:00:00 2001 From: Terence Ho <> Date: Thu, 20 Oct 2022 18:54:33 +0800 Subject: [PATCH] [resumes][feat] add resume comment parent --- .../migration.sql | 5 +++ apps/portal/prisma/schema.prisma | 3 ++ .../router/resumes/resumes-comments-router.ts | 33 ++++++++++++++++++- apps/portal/src/types/resume-comments.d.ts | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 apps/portal/prisma/migrations/20221020101123_add_resume_comment_parent/migration.sql diff --git a/apps/portal/prisma/migrations/20221020101123_add_resume_comment_parent/migration.sql b/apps/portal/prisma/migrations/20221020101123_add_resume_comment_parent/migration.sql new file mode 100644 index 00000000..018ad4cd --- /dev/null +++ b/apps/portal/prisma/migrations/20221020101123_add_resume_comment_parent/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "ResumesComment" ADD COLUMN "parentId" TEXT; + +-- AddForeignKey +ALTER TABLE "ResumesComment" ADD CONSTRAINT "ResumesComment_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "ResumesComment"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 48e068ca..7f968286 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -140,6 +140,7 @@ model ResumesComment { id String @id @default(cuid()) userId String resumeId String + parentId String? description String @db.Text section ResumesSection createdAt DateTime @default(now()) @@ -147,6 +148,8 @@ model ResumesComment { resume ResumesResume @relation(fields: [resumeId], references: [id], onDelete: Cascade) votes ResumesCommentVote[] user User @relation(fields: [userId], references: [id], onDelete: Cascade) + parent ResumesComment? @relation("parentComment", fields: [parentId], references: [id]) + children ResumesComment[] @relation("parentComment") } enum ResumesSection { diff --git a/apps/portal/src/server/router/resumes/resumes-comments-router.ts b/apps/portal/src/server/router/resumes/resumes-comments-router.ts index 096e8323..32084966 100644 --- a/apps/portal/src/server/router/resumes/resumes-comments-router.ts +++ b/apps/portal/src/server/router/resumes/resumes-comments-router.ts @@ -15,6 +15,19 @@ export const resumeCommentsRouter = createRouter().query('list', { // The user's name and image to render const comments = await ctx.prisma.resumesComment.findMany({ include: { + children: { + include: { + user: { + select: { + image: true, + name: true, + }, + }, + }, + orderBy: { + createdAt: 'desc', + }, + }, user: { select: { image: true, @@ -26,12 +39,30 @@ export const resumeCommentsRouter = createRouter().query('list', { createdAt: 'desc', }, where: { - resumeId, + AND: [{ resumeId }, { parentId: null }], }, }); return comments.map((data) => { + const children: Array = data.children.map((child) => { + return { + children: [], + createdAt: child.createdAt, + description: child.description, + id: child.id, + resumeId: child.resumeId, + section: child.section, + updatedAt: child.updatedAt, + user: { + image: child.user.image, + name: child.user.name, + userId: child.userId, + }, + }; + }); + const comment: ResumeComment = { + children, createdAt: data.createdAt, description: data.description, id: data.id, diff --git a/apps/portal/src/types/resume-comments.d.ts b/apps/portal/src/types/resume-comments.d.ts index 335948c3..c8d4d403 100644 --- a/apps/portal/src/types/resume-comments.d.ts +++ b/apps/portal/src/types/resume-comments.d.ts @@ -5,6 +5,7 @@ import type { ResumesCommentVote, ResumesSection } from '@prisma/client'; * frontend-friendly representation of the query */ export type ResumeComment = Readonly<{ + children: Array; createdAt: Date; description: string; id: string;