Skip to content

Commit

Permalink
⚡️ Send finalization emails asynchronously (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored May 12, 2024
1 parent 615c43d commit 2b11c47
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@rallly/database": "*",
"@rallly/emails": "*",
"@rallly/utils": "*",
"@vercel/functions": "^1.0.2",
"@trpc/server": "^10.13.0",
"iron-session": "^6.3.1",
"spacetime": "^7.4.7",
Expand Down
71 changes: 38 additions & 33 deletions packages/backend/trpc/routers/polls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prisma } from "@rallly/database";
import { TRPCError } from "@trpc/server";
import { waitUntil } from "@vercel/functions";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import toArray from "dayjs/plugin/toArray";
Expand Down Expand Up @@ -131,16 +132,18 @@ export const polls = router({
});

if (user) {
await ctx.emailClient.sendTemplate("NewPollEmail", {
to: user.email,
subject: `Let's find a date for ${poll.title}`,
props: {
title: poll.title,
name: user.name,
adminLink: pollLink,
participantLink,
},
});
waitUntil(
ctx.emailClient.sendTemplate("NewPollEmail", {
to: user.email,
subject: `Let's find a date for ${poll.title}`,
props: {
title: poll.title,
name: user.name,
adminLink: pollLink,
participantLink,
},
}),
);
}
}

Expand Down Expand Up @@ -875,28 +878,30 @@ export const polls = router({
});
}

const emailToHost = ctx.emailClient.sendTemplate("FinalizeHostEmail", {
subject: `Date booked for ${poll.title}`,
to: poll.user.email,
props: {
name: poll.user.name,
pollUrl: ctx.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,
day,
dow,
time,
},
attachments: [{ filename: "event.ics", content: event.value }],
});
const emailToHost = waitUntil(
ctx.emailClient.sendTemplate("FinalizeHostEmail", {
subject: `Date booked for ${poll.title}`,
to: poll.user.email,
props: {
name: poll.user.name,
pollUrl: ctx.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,
day,
dow,
time,
},
attachments: [{ filename: "event.ics", content: event.value }],
}),
);

const emailsToParticipants = participantsToEmail.map((p) => {
return ctx.emailClient.sendTemplate("FinalizeParticipantEmail", {
Expand Down Expand Up @@ -924,7 +929,7 @@ export const polls = router({
});
});

await Promise.all([emailToHost, ...emailsToParticipants]);
waitUntil(Promise.all([emailToHost, ...emailsToParticipants]));
}
}),
reopen: possiblyPublicProcedure
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4743,6 +4743,11 @@
resolved "https://registry.npmjs.org/@vercel/analytics/-/analytics-0.1.11.tgz"
integrity sha512-mj5CPR02y0BRs1tN3oZcBNAX9a8NxsIUl9vElDPcqxnMfP0RbRc9fI9Ud7+QDg/1Izvt5uMumsr+6YsmVHcyuw==

"@vercel/functions@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@vercel/functions/-/functions-1.0.2.tgz#c26ed4e3b0ed701e28c4ebd71c76b1bfe14db02a"
integrity sha512-j3udyHOv/05Y8o3WQ/ANMWa1aYagsY5B3ouImiwgYsz5z4CBUHTY5dk74oQAXYr+bgoVDpdDlmxkpnxGzKEdLQ==

"@vitest/expect@1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-1.3.1.tgz#d4c14b89c43a25fd400a6b941f51ba27fe0cb918"
Expand Down

0 comments on commit 2b11c47

Please sign in to comment.