diff --git a/apps/portal/src/pages/offers/test/createProfile.tsx b/apps/portal/src/pages/offers/test/createProfile.tsx index 158412ef..b28f6a93 100644 --- a/apps/portal/src/pages/offers/test/createProfile.tsx +++ b/apps/portal/src/pages/offers/test/createProfile.tsx @@ -34,6 +34,59 @@ function Test() { }, }) + const deleteCommentMutation = trpc.useMutation(['offers.comments.delete'], { + onError(err: any) { + alert(err); + }, + onSuccess(data) { + setCreatedData(JSON.stringify(data)); + }, + }); + + const handleDeleteComment = () => { + deleteCommentMutation.mutate({ + id: 'cl97fprun001j7iyg6ev9x983', + profileId: 'cl96stky5002ew32gx2kale2x', + token: 'afca11e436d21bde24543718fa957c6c625335439dc504f24ee35eae7b5ef1', + userId: 'cl97dl51k001e7iygd5v5gt58' + }) + } + + const updateCommentMutation = trpc.useMutation(['offers.comments.update'], { + onError(err: any) { + alert(err); + }, + onSuccess(data) { + setCreatedData(JSON.stringify(data)); + }, + }); + + const handleUpdateComment = () => { + updateCommentMutation.mutate({ + id: 'cl97fxb0y001l7iyg14sdobt2', + message: 'hello hello', + profileId: 'cl96stky5002ew32gx2kale2x', + token: 'afca11e436d21bde24543718fa957c6c625335439dc504f24ee35eae7b5ef1ba' + }) + } + + const createCommentMutation = trpc.useMutation(['offers.comments.create'], { + onError(err: any) { + alert(err); + }, + onSuccess(data) { + setCreatedData(JSON.stringify(data)); + }, + }); + + const handleCreate = () => { + createCommentMutation.mutate({ + message: 'hello', + profileId: 'cl96stky5002ew32gx2kale2x', + // UserId: 'cl97dl51k001e7iygd5v5gt58' + }) + } + const handleLink = () => { addToUserProfileMutation.mutate({ profileId: 'cl96stky5002ew32gx2kale2x', @@ -552,6 +605,15 @@ function Test() { <button type="button" onClick={handleLink}> LINKKKK! </button> + <button type="button" onClick={handleCreate}> + CREATE COMMENT! + </button> + <button type="button" onClick={handleDeleteComment}> + DELETE COMMENT! + </button> + <button type="button" onClick={handleUpdateComment}> + UPDATE COMMENT! + </button> <button className="text-danger-600" type="button" 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 9f20fe57..e8308320 100644 --- a/apps/portal/src/server/router/offers/offers-comments-router.ts +++ b/apps/portal/src/server/router/offers/offers-comments-router.ts @@ -36,32 +36,70 @@ export const offersCommentsRouter = createProtectedRouter() message: z.string(), profileId: z.string(), replyingToId: z.string().optional(), - userId: z.string() + userId: z.string().optional() }), async resolve({ ctx, input }) { - await ctx.prisma.offersReply.create({ + const createdReply = await ctx.prisma.offersReply.create({ data: { message: input.message, profile: { connect: { id: input.profileId } + } + } + }) + + if (input.replyingToId) { + await ctx.prisma.offersReply.update({ + data: { + replyingTo: { + connect: { + id: input.replyingToId + } + } }, - replyingTo: { - connect: { - id: input.replyingToId + where: { + id: createdReply.id + } + }) + } + + if (input.userId) { + await ctx.prisma.offersReply.update({ + data: { + user: { + connect: { + id: input.userId + } } }, - user: { - connect: { - id: input.userId + where: { + id: createdReply.id + } + }) + } + // Get replies + const result = await ctx.prisma.offersProfile.findFirst({ + include: { + discussion: { + include: { + replies: true, + replyingTo: true, + user: true } } + }, + where: { + id: input.profileId } }) - // Get replies - return + if (result) { + return result.discussion.filter((x) => x.replyingToId === null) + } + + return result } }) .mutation("update", { @@ -90,7 +128,7 @@ export const offersCommentsRouter = createProtectedRouter() // To validate user editing, OP or correct user // TODO: improve validation process if (profileEditToken === input.token || messageToUpdate?.userId === input.userId) { - return await ctx.prisma.offersReply.update({ + await ctx.prisma.offersReply.update({ data: { message: input.message }, @@ -98,6 +136,27 @@ export const offersCommentsRouter = createProtectedRouter() id: input.id } }) + + const result = await ctx.prisma.offersProfile.findFirst({ + include: { + discussion: { + include: { + replies: true, + replyingTo: true, + user: true + } + } + }, + where: { + id: input.profileId + } + }) + + if (result) { + return result.discussion.filter((x) => x.replyingToId === null) + } + + return result } throw new trpc.TRPCError({ @@ -136,6 +195,26 @@ export const offersCommentsRouter = createProtectedRouter() id: input.id } }) + const result = await ctx.prisma.offersProfile.findFirst({ + include: { + discussion: { + include: { + replies: true, + replyingTo: true, + user: true + } + } + }, + where: { + id: input.profileId + } + }) + + if (result) { + return result.discussion.filter((x) => x.replyingToId === null) + } + + return result } throw new trpc.TRPCError({