mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-24 14:26:23 +02:00
⬆️ v3.0.0 (#704)
This commit is contained in:
parent
735056f25f
commit
c22b3abc4d
385 changed files with 19912 additions and 5250 deletions
|
@ -3,14 +3,24 @@ import { sendEmail } from "@rallly/emails";
|
|||
import { absoluteUrl } from "@rallly/utils";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import dayjs from "dayjs";
|
||||
import timezone from "dayjs/plugin/timezone";
|
||||
import toArray from "dayjs/plugin/toArray";
|
||||
import utc from "dayjs/plugin/utc";
|
||||
import * as ics from "ics";
|
||||
import { z } from "zod";
|
||||
|
||||
import { printDate } from "../../utils/date";
|
||||
import { nanoid } from "../../utils/nanoid";
|
||||
import { possiblyPublicProcedure, publicProcedure, router } from "../trpc";
|
||||
import { comments } from "./polls/comments";
|
||||
import { demo } from "./polls/demo";
|
||||
import { options } from "./polls/options";
|
||||
import { participants } from "./polls/participants";
|
||||
|
||||
dayjs.extend(toArray);
|
||||
dayjs.extend(timezone);
|
||||
dayjs.extend(utc);
|
||||
|
||||
const getPollIdFromAdminUrlId = async (urlId: string) => {
|
||||
const res = await prisma.poll.findUnique({
|
||||
select: {
|
||||
|
@ -22,12 +32,17 @@ const getPollIdFromAdminUrlId = async (urlId: string) => {
|
|||
if (!res) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Poll not found",
|
||||
});
|
||||
}
|
||||
return res.id;
|
||||
};
|
||||
|
||||
export const polls = router({
|
||||
demo,
|
||||
participants,
|
||||
comments,
|
||||
options,
|
||||
// START LEGACY ROUTES
|
||||
create: possiblyPublicProcedure
|
||||
.input(
|
||||
|
@ -51,89 +66,97 @@ export const polls = router({
|
|||
demo: z.boolean().optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(
|
||||
async ({ ctx, input }): Promise<{ id: string; urlId: string }> => {
|
||||
const adminUrlId = nanoid();
|
||||
const participantUrlId = nanoid();
|
||||
|
||||
let email = input.user?.email;
|
||||
let name = input.user?.name;
|
||||
|
||||
if (!ctx.user.isGuest) {
|
||||
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 poll = await prisma.poll.create({
|
||||
select: {
|
||||
adminUrlId: true,
|
||||
id: true,
|
||||
title: true,
|
||||
},
|
||||
data: {
|
||||
id: nanoid(),
|
||||
title: input.title,
|
||||
timeZone: input.timeZone,
|
||||
location: input.location,
|
||||
description: input.description,
|
||||
demo: input.demo,
|
||||
adminUrlId,
|
||||
participantUrlId,
|
||||
userId: ctx.user.id,
|
||||
watchers: !ctx.user.isGuest
|
||||
? {
|
||||
create: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
options: {
|
||||
createMany: {
|
||||
data: input.options.map((option) => ({
|
||||
start: new Date(`${option.startDate}Z`),
|
||||
duration: option.endDate
|
||||
? dayjs(option.endDate).diff(
|
||||
dayjs(option.startDate),
|
||||
"minute",
|
||||
)
|
||||
: 0,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
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 },
|
||||
});
|
||||
|
||||
const adminLink = absoluteUrl(`/admin/${adminUrlId}`);
|
||||
const participantLink = absoluteUrl(`/p/${participantUrlId}`);
|
||||
|
||||
if (email && name) {
|
||||
await sendEmail("NewPollEmail", {
|
||||
to: email,
|
||||
subject: `Let's find a date for ${poll.title}`,
|
||||
props: {
|
||||
title: poll.title,
|
||||
name,
|
||||
adminLink,
|
||||
participantLink,
|
||||
},
|
||||
if (!user) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "User not found",
|
||||
});
|
||||
}
|
||||
|
||||
return { id: poll.id, urlId: adminUrlId };
|
||||
},
|
||||
),
|
||||
email = user.email;
|
||||
name = user.name;
|
||||
}
|
||||
|
||||
const poll = await prisma.poll.create({
|
||||
select: {
|
||||
adminUrlId: true,
|
||||
id: true,
|
||||
title: true,
|
||||
options: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
id: pollId,
|
||||
title: input.title,
|
||||
timeZone: input.timeZone,
|
||||
location: input.location,
|
||||
description: input.description,
|
||||
demo: input.demo,
|
||||
adminUrlId: adminToken,
|
||||
participantUrlId,
|
||||
userId: ctx.user.id,
|
||||
watchers: !ctx.user.isGuest
|
||||
? {
|
||||
create: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
options: {
|
||||
createMany: {
|
||||
data: input.options.map((option) => ({
|
||||
start: new Date(`${option.startDate}Z`),
|
||||
duration: option.endDate
|
||||
? dayjs(option.endDate).diff(
|
||||
dayjs(option.startDate),
|
||||
"minute",
|
||||
)
|
||||
: 0,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const pollLink = ctx.user.isGuest
|
||||
? absoluteUrl(`/admin/${adminToken}`)
|
||||
: absoluteUrl(`/poll/${pollId}`);
|
||||
|
||||
const participantLink = absoluteUrl(`/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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return { id: poll.id };
|
||||
}),
|
||||
update: possiblyPublicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
|
@ -221,10 +244,23 @@ export const polls = router({
|
|||
},
|
||||
});
|
||||
}),
|
||||
demo,
|
||||
participants,
|
||||
comments,
|
||||
// END LEGACY ROUTES
|
||||
getWatchers: possiblyPublicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
pollId: z.string(),
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { pollId } }) => {
|
||||
return await prisma.watcher.findMany({
|
||||
where: {
|
||||
pollId,
|
||||
},
|
||||
select: {
|
||||
userId: true,
|
||||
},
|
||||
});
|
||||
}),
|
||||
watch: possiblyPublicProcedure
|
||||
.input(z.object({ pollId: z.string() }))
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
|
@ -323,10 +359,11 @@ export const polls = router({
|
|||
|
||||
return res;
|
||||
}),
|
||||
getByParticipantUrlId: publicProcedure
|
||||
get: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
urlId: z.string(),
|
||||
adminToken: z.string().optional(),
|
||||
}),
|
||||
)
|
||||
.query(async ({ input, ctx }) => {
|
||||
|
@ -351,6 +388,13 @@ export const polls = router({
|
|||
user: true,
|
||||
userId: true,
|
||||
deleted: true,
|
||||
event: {
|
||||
select: {
|
||||
start: true,
|
||||
duration: true,
|
||||
optionId: true,
|
||||
},
|
||||
},
|
||||
watchers: {
|
||||
select: {
|
||||
userId: true,
|
||||
|
@ -358,7 +402,7 @@ export const polls = router({
|
|||
},
|
||||
},
|
||||
where: {
|
||||
participantUrlId: input.urlId,
|
||||
id: input.urlId,
|
||||
},
|
||||
rejectOnNotFound: false,
|
||||
});
|
||||
|
@ -370,10 +414,377 @@ export const polls = router({
|
|||
});
|
||||
}
|
||||
|
||||
if (ctx.user.id === res.userId) {
|
||||
if (ctx.user.id === res.userId || res.adminUrlId === input.adminToken) {
|
||||
return res;
|
||||
} else {
|
||||
return { ...res, adminUrlId: "" };
|
||||
}
|
||||
}),
|
||||
transfer: possiblyPublicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
pollId: z.string(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
await prisma.poll.update({
|
||||
where: {
|
||||
id: input.pollId,
|
||||
},
|
||||
data: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
});
|
||||
}),
|
||||
list: possiblyPublicProcedure.query(async ({ ctx }) => {
|
||||
const polls = await prisma.poll.findMany({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
deleted: false,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
location: true,
|
||||
createdAt: true,
|
||||
timeZone: true,
|
||||
adminUrlId: true,
|
||||
participantUrlId: true,
|
||||
event: {
|
||||
select: {
|
||||
start: true,
|
||||
duration: true,
|
||||
},
|
||||
},
|
||||
options: {
|
||||
select: {
|
||||
id: true,
|
||||
start: true,
|
||||
duration: true,
|
||||
},
|
||||
},
|
||||
closed: true,
|
||||
participants: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
createdAt: "desc",
|
||||
},
|
||||
{ name: "desc" },
|
||||
],
|
||||
},
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
createdAt: "desc",
|
||||
},
|
||||
{ title: "asc" },
|
||||
],
|
||||
});
|
||||
|
||||
return polls;
|
||||
}),
|
||||
book: possiblyPublicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
pollId: z.string(),
|
||||
optionId: z.string(),
|
||||
notify: z.enum(["none", "all", "attendees"]),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
if (process.env.NEXT_PUBLIC_ENABLE_FINALIZATION !== "true") {
|
||||
throw new TRPCError({
|
||||
code: "FORBIDDEN",
|
||||
message: "This feature is not enabled",
|
||||
});
|
||||
}
|
||||
const poll = await prisma.poll.findUnique({
|
||||
where: {
|
||||
id: input.pollId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
timeZone: true,
|
||||
title: true,
|
||||
location: true,
|
||||
user: {
|
||||
select: {
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
participants: {
|
||||
select: {
|
||||
name: true,
|
||||
email: true,
|
||||
votes: {
|
||||
select: {
|
||||
optionId: true,
|
||||
type: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!poll) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Poll not found",
|
||||
});
|
||||
}
|
||||
|
||||
if (!poll.user) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Poll has no user",
|
||||
});
|
||||
}
|
||||
|
||||
// create event in database
|
||||
const option = await prisma.option.findUnique({
|
||||
where: {
|
||||
id: input.optionId,
|
||||
},
|
||||
select: {
|
||||
start: true,
|
||||
duration: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!option) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Option not found",
|
||||
});
|
||||
}
|
||||
|
||||
const eventStart = poll.timeZone
|
||||
? dayjs(option.start).utc().tz(poll.timeZone, true).toDate()
|
||||
: option.start;
|
||||
|
||||
await prisma.event.create({
|
||||
data: {
|
||||
pollId: poll.id,
|
||||
optionId: input.optionId,
|
||||
start: eventStart,
|
||||
duration: option.duration,
|
||||
title: poll.title,
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
});
|
||||
|
||||
const attendees = poll.participants.filter((p) =>
|
||||
p.votes.some((v) => v.optionId === input.optionId && v.type !== "no"),
|
||||
);
|
||||
|
||||
let event: ics.ReturnObject;
|
||||
if (option.duration > 0) {
|
||||
// we need to remember to call .utc() on the dayjs() object
|
||||
// to make sure we get the correct time because dayjs() will
|
||||
// use the local timezone
|
||||
const start = poll.timeZone
|
||||
? dayjs(option.start).utc().tz(poll.timeZone, true).utc()
|
||||
: dayjs(option.start).utc();
|
||||
|
||||
event = ics.createEvent({
|
||||
title: poll.title,
|
||||
start: [
|
||||
start.year(),
|
||||
start.month() + 1,
|
||||
start.date(),
|
||||
start.hour(),
|
||||
start.minute(),
|
||||
],
|
||||
organizer: {
|
||||
name: poll.user.name,
|
||||
email: poll.user.email,
|
||||
},
|
||||
startInputType: poll.timeZone ? "utc" : "local",
|
||||
duration: { minutes: option.duration },
|
||||
attendees: attendees
|
||||
.filter((a) => !!a.email) // remove participants without email
|
||||
.map((a) => ({
|
||||
name: a.name,
|
||||
email: a.email ?? undefined,
|
||||
})),
|
||||
});
|
||||
} else {
|
||||
const start = dayjs(option.start);
|
||||
const end = start.add(1, "day");
|
||||
event = ics.createEvent({
|
||||
title: poll.title,
|
||||
start: [start.year(), start.month() + 1, start.date()],
|
||||
end: [end.year(), end.month() + 1, end.date()],
|
||||
});
|
||||
}
|
||||
|
||||
if (event.error) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: event.error.message,
|
||||
});
|
||||
}
|
||||
|
||||
if (!event.value) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Failed to generate ics",
|
||||
});
|
||||
} else {
|
||||
const formattedDate = printDate(
|
||||
eventStart,
|
||||
option.duration,
|
||||
poll.timeZone ?? undefined,
|
||||
);
|
||||
// const formatDate = (
|
||||
// date: Date,
|
||||
// duration: number,
|
||||
// timeZone?: string | null,
|
||||
// ) => {
|
||||
// if (duration > 0) {
|
||||
// if (timeZone) {
|
||||
// return `${dayjs(date)
|
||||
// .utc()
|
||||
// .format(
|
||||
// "dddd, MMMM D, YYYY, HH:mm",
|
||||
// )} (${getTimeZoneAbbreviation(timeZone)})`;
|
||||
// } else {
|
||||
// return dayjs(date).utc().format("dddd, MMMM D, YYYY, HH:mm");
|
||||
// }
|
||||
// } else {
|
||||
// return dayjs(date).format("dddd, MMMM D, YYYY");
|
||||
// }
|
||||
// };
|
||||
|
||||
const participantsToEmail: Array<{ name: string; email: string }> = [];
|
||||
|
||||
if (input.notify === "all") {
|
||||
poll.participants.forEach((p) => {
|
||||
if (p.email) {
|
||||
participantsToEmail.push({
|
||||
name: p.name,
|
||||
email: p.email,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (input.notify === "attendees") {
|
||||
attendees.forEach((p) => {
|
||||
if (p.email) {
|
||||
participantsToEmail.push({
|
||||
name: p.name,
|
||||
email: p.email,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const emailToHost = sendEmail("FinalizeHostEmail", {
|
||||
subject: `Date booked for ${poll.title}`,
|
||||
to: poll.user.email,
|
||||
props: {
|
||||
name: poll.user.name,
|
||||
pollUrl: absoluteUrl(`/poll/${poll.id}`),
|
||||
location: poll.location,
|
||||
title: poll.title,
|
||||
attendees: poll.participants
|
||||
.filter((p) =>
|
||||
p.votes.some(
|
||||
(v) => v.optionId === input.optionId && v.type !== "no",
|
||||
),
|
||||
)
|
||||
.map((p) => p.name),
|
||||
date: formattedDate,
|
||||
},
|
||||
attachments: [{ filename: "event.ics", content: event.value }],
|
||||
});
|
||||
|
||||
const emailsToParticipants = participantsToEmail.map((p) => {
|
||||
return sendEmail("FinalizeHostEmail", {
|
||||
subject: `Date booked for ${poll.title}`,
|
||||
to: p.email,
|
||||
props: {
|
||||
name: p.name,
|
||||
pollUrl: absoluteUrl(`/poll/${poll.id}`),
|
||||
location: poll.location,
|
||||
title: poll.title,
|
||||
attendees: poll.participants
|
||||
.filter((p) =>
|
||||
p.votes.some(
|
||||
(v) => v.optionId === input.optionId && v.type !== "no",
|
||||
),
|
||||
)
|
||||
.map((p) => p.name),
|
||||
date: formattedDate,
|
||||
},
|
||||
attachments: [{ filename: "event.ics", content: event.value }],
|
||||
});
|
||||
});
|
||||
|
||||
await Promise.all([emailToHost, ...emailsToParticipants]);
|
||||
}
|
||||
}),
|
||||
reopen: possiblyPublicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
pollId: z.string(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
await prisma.$transaction([
|
||||
prisma.event.delete({
|
||||
where: {
|
||||
pollId: input.pollId,
|
||||
},
|
||||
}),
|
||||
prisma.poll.update({
|
||||
where: {
|
||||
id: input.pollId,
|
||||
},
|
||||
data: {
|
||||
eventId: null,
|
||||
closed: false,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
}),
|
||||
pause: possiblyPublicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
pollId: z.string(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
await prisma.poll.update({
|
||||
where: {
|
||||
id: input.pollId,
|
||||
},
|
||||
data: {
|
||||
closed: true,
|
||||
},
|
||||
});
|
||||
}),
|
||||
resume: possiblyPublicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
pollId: z.string(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
await prisma.poll.update({
|
||||
where: {
|
||||
id: input.pollId,
|
||||
},
|
||||
data: {
|
||||
closed: false,
|
||||
},
|
||||
});
|
||||
}),
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue