mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-29 10:16:32 +02:00
114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
import { prisma } from "@rallly/database";
|
|
import { z } from "zod";
|
|
|
|
import { createToken } from "../../../session";
|
|
import { publicProcedure, router } from "../../trpc";
|
|
import { DisableNotificationsPayload } from "../../types";
|
|
|
|
export const comments = router({
|
|
list: publicProcedure
|
|
.input(
|
|
z.object({
|
|
pollId: z.string(),
|
|
hideParticipants: z.boolean().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input: { pollId, hideParticipants }, ctx }) => {
|
|
return await prisma.comment.findMany({
|
|
where: { pollId, userId: hideParticipants ? ctx.user.id : undefined },
|
|
orderBy: [
|
|
{
|
|
createdAt: "asc",
|
|
},
|
|
],
|
|
});
|
|
}),
|
|
add: publicProcedure
|
|
.input(
|
|
z.object({
|
|
pollId: z.string(),
|
|
authorName: z.string(),
|
|
content: z.string(),
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input: { pollId, authorName, content } }) => {
|
|
const newComment = await prisma.comment.create({
|
|
data: {
|
|
content,
|
|
pollId,
|
|
authorName,
|
|
userId: ctx.user.id,
|
|
},
|
|
select: {
|
|
id: true,
|
|
createdAt: true,
|
|
authorName: true,
|
|
content: true,
|
|
poll: {
|
|
select: {
|
|
title: true,
|
|
id: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const watchers = await prisma.watcher.findMany({
|
|
where: {
|
|
pollId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
name: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const poll = newComment.poll;
|
|
|
|
const emailsToSend: Promise<void>[] = [];
|
|
|
|
for (const watcher of watchers) {
|
|
const email = watcher.user.email;
|
|
const token = await createToken<DisableNotificationsPayload>(
|
|
{ watcherId: watcher.id, pollId },
|
|
{ ttl: 0 },
|
|
);
|
|
emailsToSend.push(
|
|
ctx.emailClient.sendTemplate("NewCommentEmail", {
|
|
to: email,
|
|
subject: `${authorName} hat ${poll.title} kommentiert`,
|
|
props: {
|
|
name: watcher.user.name,
|
|
authorName,
|
|
pollUrl: ctx.absoluteUrl(`/poll/${poll.id}`),
|
|
disableNotificationsUrl: ctx.absoluteUrl(
|
|
`/auth/disable-notifications?token=${token}`,
|
|
),
|
|
title: poll.title,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
return newComment;
|
|
}),
|
|
delete: publicProcedure
|
|
.input(
|
|
z.object({
|
|
commentId: z.string(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input: { commentId } }) => {
|
|
await prisma.comment.delete({
|
|
where: {
|
|
id: commentId,
|
|
},
|
|
});
|
|
}),
|
|
});
|