Allow users to customize poll behaviour (#785)

This commit is contained in:
Luke Vella 2023-07-25 17:24:45 +01:00 committed by GitHub
parent 14cfa2bd50
commit b1e3f63a2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 1361 additions and 1042 deletions

View file

@ -38,6 +38,19 @@ const getPollIdFromAdminUrlId = async (urlId: string) => {
return res.id;
};
const getPro = async (userId: string) => {
return Boolean(
await prisma.userPaymentData.findFirst({
where: {
userId,
endDate: {
gt: new Date(),
},
},
}),
);
};
export const polls = router({
demo,
participants,
@ -51,12 +64,9 @@ export const polls = router({
timeZone: z.string().optional(),
location: z.string().optional(),
description: z.string().optional(),
user: z
.object({
name: z.string(),
email: z.string(),
})
.optional(),
hideParticipants: z.boolean().optional(),
hideScores: z.boolean().optional(),
disableComments: z.boolean().optional(),
options: z
.object({
startDate: z.string(),
@ -70,27 +80,8 @@ export const polls = router({
const adminToken = nanoid();
const participantUrlId = nanoid();
const pollId = nanoid();
let email: string;
let name: string;
if (input.user && ctx.user.isGuest) {
email = input.user.email;
name = input.user.name;
} else {
const user = await prisma.user.findUnique({
select: { email: true, name: true },
where: { id: ctx.user.id },
});
if (!user) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "User not found",
});
}
email = user.email;
name = user.name;
}
const isPro = await getPro(ctx.user.id);
const poll = await prisma.poll.create({
select: {
@ -133,6 +124,13 @@ export const polls = router({
})),
},
},
...(isPro
? {
hideParticipants: input.hideParticipants,
disableComments: input.disableComments,
hideScores: input.hideScores,
}
: undefined),
},
});
@ -142,17 +140,24 @@ export const polls = router({
const participantLink = shortUrl(`/invite/${pollId}`);
if (email && name) {
await sendEmail("NewPollEmail", {
to: email,
subject: `Let's find a date for ${poll.title}`,
props: {
title: poll.title,
name,
adminLink: pollLink,
participantLink,
},
if (ctx.user.isGuest === false) {
const user = await prisma.user.findUnique({
select: { email: true, name: true },
where: { id: ctx.user.id },
});
if (user) {
await sendEmail("NewPollEmail", {
to: user.email,
subject: `Let's find a date for ${poll.title}`,
props: {
title: poll.title,
name: user.name,
adminLink: pollLink,
participantLink,
},
});
}
}
return { id: poll.id };
@ -168,11 +173,16 @@ export const polls = router({
optionsToDelete: z.string().array().optional(),
optionsToAdd: z.string().array().optional(),
closed: z.boolean().optional(),
hideParticipants: z.boolean().optional(),
disableComments: z.boolean().optional(),
hideScores: z.boolean().optional(),
}),
)
.mutation(async ({ input }) => {
.mutation(async ({ ctx, input }) => {
const pollId = await getPollIdFromAdminUrlId(input.urlId);
const isPro = await getPro(ctx.user.id);
if (input.optionsToDelete && input.optionsToDelete.length > 0) {
await prisma.option.deleteMany({
where: {
@ -215,6 +225,13 @@ export const polls = router({
description: input.description,
timeZone: input.timeZone,
closed: input.closed,
...(isPro
? {
hideScores: input.hideScores,
hideParticipants: input.hideParticipants,
disableComments: input.disableComments,
}
: undefined),
},
});
}),
@ -378,6 +395,9 @@ export const polls = router({
participantUrlId: true,
closed: true,
legacy: true,
hideParticipants: true,
disableComments: true,
hideScores: true,
demo: true,
options: {
orderBy: {

View file

@ -12,11 +12,12 @@ export const comments = router({
.input(
z.object({
pollId: z.string(),
hideParticipants: z.boolean().optional(),
}),
)
.query(async ({ input: { pollId } }) => {
.query(async ({ input: { pollId, hideParticipants }, ctx }) => {
return await prisma.comment.findMany({
where: { pollId },
where: { pollId, userId: hideParticipants ? ctx.user.id : undefined },
orderBy: [
{
createdAt: "asc",