️ Send finalization emails asynchronously (#1095)

This commit is contained in:
Luke Vella 2024-05-12 14:13:56 +08:00 committed by GitHub
parent 615c43d777
commit 2b11c47a97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 33 deletions

View file

@ -12,6 +12,7 @@
"@rallly/database": "*", "@rallly/database": "*",
"@rallly/emails": "*", "@rallly/emails": "*",
"@rallly/utils": "*", "@rallly/utils": "*",
"@vercel/functions": "^1.0.2",
"@trpc/server": "^10.13.0", "@trpc/server": "^10.13.0",
"iron-session": "^6.3.1", "iron-session": "^6.3.1",
"spacetime": "^7.4.7", "spacetime": "^7.4.7",

View file

@ -1,5 +1,6 @@
import { prisma } from "@rallly/database"; import { prisma } from "@rallly/database";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { waitUntil } from "@vercel/functions";
import dayjs from "dayjs"; import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone"; import timezone from "dayjs/plugin/timezone";
import toArray from "dayjs/plugin/toArray"; import toArray from "dayjs/plugin/toArray";
@ -131,16 +132,18 @@ export const polls = router({
}); });
if (user) { if (user) {
await ctx.emailClient.sendTemplate("NewPollEmail", { waitUntil(
to: user.email, ctx.emailClient.sendTemplate("NewPollEmail", {
subject: `Let's find a date for ${poll.title}`, to: user.email,
props: { subject: `Let's find a date for ${poll.title}`,
title: poll.title, props: {
name: user.name, title: poll.title,
adminLink: pollLink, name: user.name,
participantLink, adminLink: pollLink,
}, participantLink,
}); },
}),
);
} }
} }
@ -875,28 +878,30 @@ export const polls = router({
}); });
} }
const emailToHost = ctx.emailClient.sendTemplate("FinalizeHostEmail", { const emailToHost = waitUntil(
subject: `Date booked for ${poll.title}`, ctx.emailClient.sendTemplate("FinalizeHostEmail", {
to: poll.user.email, subject: `Date booked for ${poll.title}`,
props: { to: poll.user.email,
name: poll.user.name, props: {
pollUrl: ctx.absoluteUrl(`/poll/${poll.id}`), name: poll.user.name,
location: poll.location, pollUrl: ctx.absoluteUrl(`/poll/${poll.id}`),
title: poll.title, location: poll.location,
attendees: poll.participants title: poll.title,
.filter((p) => attendees: poll.participants
p.votes.some( .filter((p) =>
(v) => v.optionId === input.optionId && v.type !== "no", p.votes.some(
), (v) => v.optionId === input.optionId && v.type !== "no",
) ),
.map((p) => p.name), )
date, .map((p) => p.name),
day, date,
dow, day,
time, dow,
}, time,
attachments: [{ filename: "event.ics", content: event.value }], },
}); attachments: [{ filename: "event.ics", content: event.value }],
}),
);
const emailsToParticipants = participantsToEmail.map((p) => { const emailsToParticipants = participantsToEmail.map((p) => {
return ctx.emailClient.sendTemplate("FinalizeParticipantEmail", { return ctx.emailClient.sendTemplate("FinalizeParticipantEmail", {
@ -924,7 +929,7 @@ export const polls = router({
}); });
}); });
await Promise.all([emailToHost, ...emailsToParticipants]); waitUntil(Promise.all([emailToHost, ...emailsToParticipants]));
} }
}), }),
reopen: possiblyPublicProcedure reopen: possiblyPublicProcedure

View file

@ -4743,6 +4743,11 @@
resolved "https://registry.npmjs.org/@vercel/analytics/-/analytics-0.1.11.tgz" resolved "https://registry.npmjs.org/@vercel/analytics/-/analytics-0.1.11.tgz"
integrity sha512-mj5CPR02y0BRs1tN3oZcBNAX9a8NxsIUl9vElDPcqxnMfP0RbRc9fI9Ud7+QDg/1Izvt5uMumsr+6YsmVHcyuw== 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": "@vitest/expect@1.3.1":
version "1.3.1" version "1.3.1"
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-1.3.1.tgz#d4c14b89c43a25fd400a6b941f51ba27fe0cb918" resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-1.3.1.tgz#d4c14b89c43a25fd400a6b941f51ba27fe0cb918"