[web] workspace/discussions: add trigger for activity (#1691)
This commit is contained in:
parent
25e658c0a3
commit
d5374147b3
|
|
@ -0,0 +1,110 @@
|
|||
import prisma from '~/server/prisma';
|
||||
|
||||
export async function interviewsActivityForNewComment({
|
||||
actorId,
|
||||
commentId,
|
||||
}: Readonly<{
|
||||
actorId: string;
|
||||
commentId: string;
|
||||
}>) {
|
||||
await prisma.interviewsActivity.create({
|
||||
data: {
|
||||
actorId,
|
||||
category: 'DISCUSSION',
|
||||
commentId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function interviewsActivityForReplyingToComment({
|
||||
actorId,
|
||||
commentId,
|
||||
}: Readonly<{
|
||||
actorId: string;
|
||||
commentId: string;
|
||||
}>) {
|
||||
const comment = await prisma.interviewsDiscussionComment.findUnique({
|
||||
include: {
|
||||
parentComment: {
|
||||
select: {
|
||||
profileId: true,
|
||||
},
|
||||
},
|
||||
repliedTo: {
|
||||
select: {
|
||||
profileId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
where: { id: commentId },
|
||||
});
|
||||
|
||||
if (!comment) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activities = [];
|
||||
|
||||
// Add reply activity
|
||||
activities.push({
|
||||
actorId,
|
||||
category: 'DISCUSSION' as const,
|
||||
commentId,
|
||||
recipientId:
|
||||
comment.parentComment?.profileId &&
|
||||
actorId !== comment.parentComment.profileId
|
||||
? comment.parentComment.profileId
|
||||
: null,
|
||||
});
|
||||
|
||||
// Add activity for replying to reply if it exists and recipient is not actor
|
||||
if (comment.repliedTo?.profileId && actorId !== comment.repliedTo.profileId) {
|
||||
activities.push({
|
||||
actorId,
|
||||
category: 'DISCUSSION' as const,
|
||||
commentId,
|
||||
recipientId: comment.repliedTo.profileId,
|
||||
});
|
||||
}
|
||||
|
||||
// Bulk create activities if any
|
||||
if (activities.length > 0) {
|
||||
await prisma.interviewsActivity.createMany({
|
||||
data: activities,
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function interviewsActivityForUpvotingComment({
|
||||
actorId,
|
||||
voteId,
|
||||
}: Readonly<{
|
||||
actorId: string;
|
||||
voteId: string;
|
||||
}>) {
|
||||
const upvote = await prisma.interviewsDiscussionCommentVote.findUnique({
|
||||
include: {
|
||||
comment: {
|
||||
select: {
|
||||
profileId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
where: { id: voteId },
|
||||
});
|
||||
|
||||
if (!upvote) {
|
||||
return;
|
||||
}
|
||||
|
||||
await prisma.interviewsActivity.create({
|
||||
data: {
|
||||
actorId,
|
||||
category: 'DISCUSSION_UPVOTE',
|
||||
recipientId:
|
||||
actorId !== upvote.comment.profileId ? upvote.comment.profileId : null,
|
||||
voteId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -1,6 +1,11 @@
|
|||
import { InterviewsDiscussionCommentDomain, Prisma } from '@prisma/client';
|
||||
import { z } from 'zod';
|
||||
|
||||
import {
|
||||
interviewsActivityForNewComment,
|
||||
interviewsActivityForReplyingToComment,
|
||||
interviewsActivityForUpvotingComment,
|
||||
} from '~/components/interviews/activity/InterviewsActivityUtils';
|
||||
import { discussionsCommentBodySchemaServer } from '~/components/workspace/common/discussions/CodingWorkspaceDiscussionsComentBodySchema';
|
||||
|
||||
import prisma from '~/server/prisma';
|
||||
|
|
@ -27,7 +32,11 @@ export const questionCommentsRouter = router({
|
|||
},
|
||||
});
|
||||
|
||||
// TODO(discussions): Add activity triggers
|
||||
// Trigger activity for new comment.
|
||||
interviewsActivityForNewComment({
|
||||
actorId: viewer.id,
|
||||
commentId: comment.id,
|
||||
});
|
||||
|
||||
return comment;
|
||||
},
|
||||
|
|
@ -175,7 +184,11 @@ export const questionCommentsRouter = router({
|
|||
},
|
||||
});
|
||||
|
||||
// TODO(discussions): Add activity triggers
|
||||
// Trigger activity for replying to a comment.
|
||||
interviewsActivityForReplyingToComment({
|
||||
actorId: viewer.id,
|
||||
commentId: comment.id,
|
||||
});
|
||||
|
||||
return comment;
|
||||
},
|
||||
|
|
@ -222,14 +235,20 @@ export const questionCommentsRouter = router({
|
|||
)
|
||||
.mutation(async ({ ctx: { viewer }, input: { commentId } }) => {
|
||||
try {
|
||||
return await prisma.interviewsDiscussionCommentVote.create({
|
||||
const upvote = await prisma.interviewsDiscussionCommentVote.create({
|
||||
data: {
|
||||
commentId,
|
||||
profileId: viewer.id,
|
||||
},
|
||||
});
|
||||
|
||||
// TODO(discussions): Add activity triggers
|
||||
// Trigger activity for upvoting a comment
|
||||
interviewsActivityForUpvotingComment({
|
||||
actorId: viewer.id,
|
||||
voteId: upvote.id,
|
||||
});
|
||||
|
||||
return upvote;
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
|
|
|
|||
Loading…
Reference in New Issue