[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: [
{
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',

@ -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,8 +36,7 @@ export const offersCommentsRouter = createRouter()
},
});
if (result) {
return result.discussion
result?.discussion
.filter((x) => x.replyingToId === null)
.map((x) => {
if (x.user == null) {
@ -61,9 +62,12 @@ export const offersCommentsRouter = createRouter()
});
return x;
});
const discussions: OffersDiscussion = {
data: result ? result.discussion : []
}
return result;
return discussions
},
})
.mutation('create', {
@ -124,34 +128,38 @@ export const offersCommentsRouter = createRouter()
},
});
}
// Get replies
const result = await ctx.prisma.offersProfile.findFirst({
include: {
discussion: {
const created = await ctx.prisma.offersReply.findFirst({
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 ?? '<missing name>',
}
}
return result;
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 ?? '<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({
@ -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({

Loading…
Cancel
Save