From 52d13b8e28c06481c2e63a1ab0c8fb03c421d7a7 Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Wed, 19 Oct 2022 00:51:41 +0800 Subject: [PATCH] [offers][chore] add return types to comment api --- .../src/pages/offers/test/createProfile.tsx | 28 ++-- .../router/offers/offers-comments-router.ts | 149 +++++++++++------- 2 files changed, 102 insertions(+), 75 deletions(-) diff --git a/apps/portal/src/pages/offers/test/createProfile.tsx b/apps/portal/src/pages/offers/test/createProfile.tsx index e47980b3..d0360533 100644 --- a/apps/portal/src/pages/offers/test/createProfile.tsx +++ b/apps/portal/src/pages/offers/test/createProfile.tsx @@ -102,7 +102,7 @@ function Test() { ], experiences: [ { - companyId: 'cl98yuqk80007txhgjtjp8fk4', + companyId: 'cl9ec1mgg0000w33hg1a3612r', durationInMonths: 24, jobType: 'FULLTIME', level: 'Junior', @@ -150,8 +150,8 @@ function Test() { value: 104100, }, }, - // Comments: '', - companyId: 'cl98yuqk80007txhgjtjp8fk4', + comments: '', + companyId: 'cl9ec1mgg0000w33hg1a3612r', jobType: 'FULLTIME', location: 'Singapore, Singapore', monthYearReceived: new Date('2022-09-30T07:58:54.000Z'), @@ -179,25 +179,25 @@ function Test() { value: 104100, }, }, - comments: undefined, - companyId: 'cl98yuqk80007txhgjtjp8fk4', + comments: "", + companyId: 'cl9ec1mgg0000w33hg1a3612r', jobType: 'FULLTIME', location: 'Singapore, Singapore', monthYearReceived: new Date('2022-09-30T07:58:54.000Z'), - // NegotiationStrategy: 'Leveraged having multiple offers', + negotiationStrategy: 'Leveraged having multiple offers', }, ], }); }; - const profileId = 'cl99fhrsf00007ijpbrdk8gue'; // Remember to change this filed after testing deleting + const profileId = 'cl9efyn9p004ww3u42mjgl1vn'; // Remember to change this filed after testing deleting const data = trpc.useQuery( [ `offers.profile.listOne`, { profileId, token: - 'e7effd2a40adba2deb1ddea4fb9f1e6c3c98ab0a85a88ed1567fc2a107fdb445', + 'd14666ff76e267c9e99445844b41410e83874936d0c07e664db73ff0ea76919e', }, ], { @@ -261,7 +261,7 @@ function Test() { slug: 'meta', updatedAt: new Date('2022-10-12T16:19:05.196Z'), }, - companyId: 'cl98yuqk80007txhgjtjp8fk4', + companyId: 'cl9ec1mgg0000w33hg1a3612r', durationInMonths: 24, id: 'cl96stky6002iw32gpt6t87s2', jobType: 'FULLTIME', @@ -368,7 +368,7 @@ function Test() { slug: 'meta', updatedAt: new Date('2022-10-12T16:19:05.196Z'), }, - companyId: 'cl98yuqk80007txhgjtjp8fk4', + companyId: 'cl9ec1mgg0000w33hg1a3612r', id: 'cl976t4de00047iygl0zbce11', jobType: 'FULLTIME', location: 'Singapore, Singapore', @@ -421,7 +421,7 @@ function Test() { slug: 'meta', updatedAt: new Date('2022-10-12T16:19:05.196Z'), }, - companyId: 'cl98yuqk80007txhgjtjp8fk4', + companyId: 'cl9ec1mgg0000w33hg1a3612r', id: 'cl96stky80031w32gau9mu1gs', jobType: 'FULLTIME', location: 'Singapore, Singapore', @@ -474,7 +474,7 @@ function Test() { slug: 'meta', updatedAt: new Date('2022-10-12T16:19:05.196Z'), }, - companyId: 'cl98yuqk80007txhgjtjp8fk4', + companyId: 'cl9ec1mgg0000w33hg1a3612r', id: 'cl96stky9003bw32gc3l955vr', jobType: 'FULLTIME', location: 'Singapore, Singapore', @@ -527,7 +527,7 @@ function Test() { slug: 'meta', updatedAt: new Date('2022-10-12T16:19:05.196Z'), }, - companyId: 'cl98yuqk80007txhgjtjp8fk4', + companyId: 'cl9ec1mgg0000w33hg1a3612r', id: 'cl976wf28000t7iyga4noyz7s', jobType: 'FULLTIME', location: 'Singapore, Singapore', @@ -580,7 +580,7 @@ function Test() { slug: 'meta', updatedAt: new Date('2022-10-12T16:19:05.196Z'), }, - companyId: 'cl98yuqk80007txhgjtjp8fk4', + companyId: 'cl9ec1mgg0000w33hg1a3612r', id: 'cl96tbb3o0051w32gjrpaiiit', jobType: 'FULLTIME', location: 'Singapore, Singapore', diff --git a/apps/portal/src/server/router/offers/offers-comments-router.ts b/apps/portal/src/server/router/offers/offers-comments-router.ts index b3baeb2e..cd0b4799 100644 --- a/apps/portal/src/server/router/offers/offers-comments-router.ts +++ b/apps/portal/src/server/router/offers/offers-comments-router.ts @@ -3,6 +3,8 @@ import * as trpc from '@trpc/server'; import { createRouter } from '../context'; +import type { OffersDiscussion, Reply } from '~/types/offers'; + export const offersCommentsRouter = createRouter() .query('getComments', { input: z.object({ @@ -34,12 +36,22 @@ export const offersCommentsRouter = createRouter() }, }); - if (result) { - return result.discussion - .filter((x) => x.replyingToId === null) - .map((x) => { - if (x.user == null) { - x.user = { + result?.discussion + .filter((x) => x.replyingToId === null) + .map((x) => { + if (x.user == null) { + x.user = { + email: '', + emailVerified: null, + id: '', + image: '', + name: profile?.profileName ?? '', + }; + } + + x.replies?.map((y) => { + if (y.user == null) { + y.user = { email: '', emailVerified: null, id: '', @@ -47,23 +59,15 @@ export const offersCommentsRouter = createRouter() name: profile?.profileName ?? '', }; } - - x.replies?.map((y) => { - if (y.user == null) { - y.user = { - email: '', - emailVerified: null, - id: '', - image: '', - name: profile?.profileName ?? '', - }; - } - }); - return x; }); - } + return x; + }); - return result; + const discussions: OffersDiscussion = { + data: result ? result.discussion : [] + } + + return discussions }, }) .mutation('create', { @@ -124,34 +128,38 @@ export const offersCommentsRouter = createRouter() }, }); } - // Get replies - const result = await ctx.prisma.offersProfile.findFirst({ + + const created = await ctx.prisma.offersReply.findFirst({ include: { - discussion: { - include: { - replies: true, - replyingTo: true, - user: true, - }, - }, + user: true }, where: { - id: input.profileId, + id: createdReply.id, }, }); - if (result) { - return result.discussion.filter((x) => x.replyingToId === null); + const result: Reply = { + createdAt: created!.createdAt, + id: created!.id, + message: created!.message, + replies: [], // New message should have no replies + replyingToId: created!.replyingToId, + user: created!.user ?? { + email: '', + emailVerified: null, + id: '', + image: '', + name: profile?.profileName ?? '', + } } - return result; - } - - throw new trpc.TRPCError({ - code: 'UNAUTHORIZED', - message: 'Missing userId or wrong token.', - }); + return result + } + throw new trpc.TRPCError({ + code: 'UNAUTHORIZED', + message: 'Missing userId or wrong token.', + }); }, }) .mutation('update', { @@ -183,35 +191,54 @@ export const offersCommentsRouter = createRouter() profileEditToken === input.token || messageToUpdate?.userId === input.userId ) { - await ctx.prisma.offersReply.update({ + const updated = await ctx.prisma.offersReply.update({ data: { message: input.message, }, - where: { - id: input.id, - }, - }); - - const result = await ctx.prisma.offersProfile.findFirst({ include: { - discussion: { + replies: { include: { - replies: true, - replyingTo: true, - user: true, - }, + user: true + } }, + user: true }, where: { - id: input.profileId, + id: input.id, }, }); - if (result) { - return result.discussion.filter((x) => x.replyingToId === null); + const result: Reply = { + createdAt: updated!.createdAt, + id: updated!.id, + message: updated!.message, + replies: updated!.replies.map((x) => { + return { + createdAt: x.createdAt, + id: x.id, + message: x.message, + replies: [], + replyingToId: x.replyingToId, + user: x.user ?? { + email: '', + emailVerified: null, + id: '', + image: '', + name: profile?.profileName ?? '', + } + } + }), + replyingToId: updated!.replyingToId, + user: updated!.user ?? { + email: '', + emailVerified: null, + id: '', + image: '', + name: profile?.profileName ?? '', + } } - return result; + return result } throw new trpc.TRPCError({ @@ -253,7 +280,7 @@ export const offersCommentsRouter = createRouter() id: input.id, }, }); - const result = await ctx.prisma.offersProfile.findFirst({ + await ctx.prisma.offersProfile.findFirst({ include: { discussion: { include: { @@ -268,11 +295,11 @@ export const offersCommentsRouter = createRouter() }, }); - if (result) { - return result.discussion.filter((x) => x.replyingToId === null); - } + // If (result) { + // return result.discussion.filter((x) => x.replyingToId === null); + // } - return result; + // return result; } throw new trpc.TRPCError({