[offers][chore] add return types to comment api

pull/390/head
Stuart Long Chay Boon 3 years ago
parent 2ad84aad91
commit 52d13b8e28

@ -102,7 +102,7 @@ function Test() {
], ],
experiences: [ experiences: [
{ {
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
durationInMonths: 24, durationInMonths: 24,
jobType: 'FULLTIME', jobType: 'FULLTIME',
level: 'Junior', level: 'Junior',
@ -150,8 +150,8 @@ function Test() {
value: 104100, value: 104100,
}, },
}, },
// Comments: '', comments: '',
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
jobType: 'FULLTIME', jobType: 'FULLTIME',
location: 'Singapore, Singapore', location: 'Singapore, Singapore',
monthYearReceived: new Date('2022-09-30T07:58:54.000Z'), monthYearReceived: new Date('2022-09-30T07:58:54.000Z'),
@ -179,25 +179,25 @@ function Test() {
value: 104100, value: 104100,
}, },
}, },
comments: undefined, comments: "",
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
jobType: 'FULLTIME', jobType: 'FULLTIME',
location: 'Singapore, Singapore', location: 'Singapore, Singapore',
monthYearReceived: new Date('2022-09-30T07:58:54.000Z'), 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( const data = trpc.useQuery(
[ [
`offers.profile.listOne`, `offers.profile.listOne`,
{ {
profileId, profileId,
token: token:
'e7effd2a40adba2deb1ddea4fb9f1e6c3c98ab0a85a88ed1567fc2a107fdb445', 'd14666ff76e267c9e99445844b41410e83874936d0c07e664db73ff0ea76919e',
}, },
], ],
{ {
@ -261,7 +261,7 @@ function Test() {
slug: 'meta', slug: 'meta',
updatedAt: new Date('2022-10-12T16:19:05.196Z'), updatedAt: new Date('2022-10-12T16:19:05.196Z'),
}, },
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
durationInMonths: 24, durationInMonths: 24,
id: 'cl96stky6002iw32gpt6t87s2', id: 'cl96stky6002iw32gpt6t87s2',
jobType: 'FULLTIME', jobType: 'FULLTIME',
@ -368,7 +368,7 @@ function Test() {
slug: 'meta', slug: 'meta',
updatedAt: new Date('2022-10-12T16:19:05.196Z'), updatedAt: new Date('2022-10-12T16:19:05.196Z'),
}, },
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
id: 'cl976t4de00047iygl0zbce11', id: 'cl976t4de00047iygl0zbce11',
jobType: 'FULLTIME', jobType: 'FULLTIME',
location: 'Singapore, Singapore', location: 'Singapore, Singapore',
@ -421,7 +421,7 @@ function Test() {
slug: 'meta', slug: 'meta',
updatedAt: new Date('2022-10-12T16:19:05.196Z'), updatedAt: new Date('2022-10-12T16:19:05.196Z'),
}, },
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
id: 'cl96stky80031w32gau9mu1gs', id: 'cl96stky80031w32gau9mu1gs',
jobType: 'FULLTIME', jobType: 'FULLTIME',
location: 'Singapore, Singapore', location: 'Singapore, Singapore',
@ -474,7 +474,7 @@ function Test() {
slug: 'meta', slug: 'meta',
updatedAt: new Date('2022-10-12T16:19:05.196Z'), updatedAt: new Date('2022-10-12T16:19:05.196Z'),
}, },
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
id: 'cl96stky9003bw32gc3l955vr', id: 'cl96stky9003bw32gc3l955vr',
jobType: 'FULLTIME', jobType: 'FULLTIME',
location: 'Singapore, Singapore', location: 'Singapore, Singapore',
@ -527,7 +527,7 @@ function Test() {
slug: 'meta', slug: 'meta',
updatedAt: new Date('2022-10-12T16:19:05.196Z'), updatedAt: new Date('2022-10-12T16:19:05.196Z'),
}, },
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
id: 'cl976wf28000t7iyga4noyz7s', id: 'cl976wf28000t7iyga4noyz7s',
jobType: 'FULLTIME', jobType: 'FULLTIME',
location: 'Singapore, Singapore', location: 'Singapore, Singapore',
@ -580,7 +580,7 @@ function Test() {
slug: 'meta', slug: 'meta',
updatedAt: new Date('2022-10-12T16:19:05.196Z'), updatedAt: new Date('2022-10-12T16:19:05.196Z'),
}, },
companyId: 'cl98yuqk80007txhgjtjp8fk4', companyId: 'cl9ec1mgg0000w33hg1a3612r',
id: 'cl96tbb3o0051w32gjrpaiiit', id: 'cl96tbb3o0051w32gjrpaiiit',
jobType: 'FULLTIME', jobType: 'FULLTIME',
location: 'Singapore, Singapore', location: 'Singapore, Singapore',

@ -3,6 +3,8 @@ import * as trpc from '@trpc/server';
import { createRouter } from '../context'; import { createRouter } from '../context';
import type { OffersDiscussion, Reply } from '~/types/offers';
export const offersCommentsRouter = createRouter() export const offersCommentsRouter = createRouter()
.query('getComments', { .query('getComments', {
input: z.object({ input: z.object({
@ -34,12 +36,22 @@ export const offersCommentsRouter = createRouter()
}, },
}); });
if (result) { result?.discussion
return result.discussion .filter((x) => x.replyingToId === null)
.filter((x) => x.replyingToId === null) .map((x) => {
.map((x) => { if (x.user == null) {
if (x.user == null) { x.user = {
x.user = { email: '',
emailVerified: null,
id: '',
image: '',
name: profile?.profileName ?? '<missing name>',
};
}
x.replies?.map((y) => {
if (y.user == null) {
y.user = {
email: '', email: '',
emailVerified: null, emailVerified: null,
id: '', id: '',
@ -47,23 +59,15 @@ export const offersCommentsRouter = createRouter()
name: profile?.profileName ?? '<missing name>', name: profile?.profileName ?? '<missing name>',
}; };
} }
x.replies?.map((y) => {
if (y.user == null) {
y.user = {
email: '',
emailVerified: null,
id: '',
image: '',
name: profile?.profileName ?? '<missing name>',
};
}
});
return x;
}); });
} return x;
});
return result; const discussions: OffersDiscussion = {
data: result ? result.discussion : []
}
return discussions
}, },
}) })
.mutation('create', { .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: { include: {
discussion: { user: true
include: {
replies: true,
replyingTo: true,
user: true,
},
},
}, },
where: { where: {
id: input.profileId, id: createdReply.id,
}, },
}); });
if (result) { const result: Reply = {
return result.discussion.filter((x) => x.replyingToId === null); 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 ?? '<missing name>',
}
} }
return result; return result
} }
throw new trpc.TRPCError({
code: 'UNAUTHORIZED',
message: 'Missing userId or wrong token.',
});
throw new trpc.TRPCError({
code: 'UNAUTHORIZED',
message: 'Missing userId or wrong token.',
});
}, },
}) })
.mutation('update', { .mutation('update', {
@ -183,35 +191,54 @@ export const offersCommentsRouter = createRouter()
profileEditToken === input.token || profileEditToken === input.token ||
messageToUpdate?.userId === input.userId messageToUpdate?.userId === input.userId
) { ) {
await ctx.prisma.offersReply.update({ const updated = await ctx.prisma.offersReply.update({
data: { data: {
message: input.message, message: input.message,
}, },
where: {
id: input.id,
},
});
const result = await ctx.prisma.offersProfile.findFirst({
include: { include: {
discussion: { replies: {
include: { include: {
replies: true, user: true
replyingTo: true, }
user: true,
},
}, },
user: true
}, },
where: { where: {
id: input.profileId, id: input.id,
}, },
}); });
if (result) { const result: Reply = {
return result.discussion.filter((x) => x.replyingToId === null); 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 ?? '<missing name>',
}
}
}),
replyingToId: updated!.replyingToId,
user: updated!.user ?? {
email: '',
emailVerified: null,
id: '',
image: '',
name: profile?.profileName ?? '<missing name>',
}
} }
return result; return result
} }
throw new trpc.TRPCError({ throw new trpc.TRPCError({
@ -253,7 +280,7 @@ export const offersCommentsRouter = createRouter()
id: input.id, id: input.id,
}, },
}); });
const result = await ctx.prisma.offersProfile.findFirst({ await ctx.prisma.offersProfile.findFirst({
include: { include: {
discussion: { discussion: {
include: { include: {
@ -268,11 +295,11 @@ export const offersCommentsRouter = createRouter()
}, },
}); });
if (result) { // If (result) {
return result.discussion.filter((x) => x.replyingToId === null); // return result.discussion.filter((x) => x.replyingToId === null);
} // }
return result; // return result;
} }
throw new trpc.TRPCError({ throw new trpc.TRPCError({

Loading…
Cancel
Save