♻️ Improve email abstraction (#863)

This commit is contained in:
Luke Vella 2023-09-15 20:24:46 +01:00 committed by GitHub
parent 516a4114d0
commit 8cad515dc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 751 additions and 152 deletions

View file

@ -1,3 +1,4 @@
import { EmailClient, SupportedEmailProviders } from "@rallly/emails";
import { createProxySSGHelpers } from "@trpc/react-query/ssg";
import * as trpc from "@trpc/server";
import * as trpcNext from "@trpc/server/adapters/next";
@ -7,6 +8,9 @@ import superjson from "superjson";
import { randomid } from "../utils/nanoid";
import { appRouter } from "./routers";
// Avoid use NODE_ENV directly because it will be replaced when using the dev server for e2e tests
const env = process.env["NODE" + "_ENV"];
export async function createContext(
opts: trpcNext.CreateNextContextOptions | GetServerSidePropsContext,
) {
@ -19,12 +23,30 @@ export async function createContext(
opts.req.session.user = user;
await opts.req.session.save();
}
const emailClient = new EmailClient({
openPreviews: env === "development",
useTestServer: env === "test",
provider: {
name: (process.env.EMAIL_PROVIDER as SupportedEmailProviders) ?? "smtp",
},
mail: {
from: {
name: "Rallly",
address:
(process.env.NOREPLY_EMAIL as string) ||
(process.env.SUPPORT_EMAIL as string),
},
},
});
return {
user,
session: opts.req.session,
req: opts.req,
res: opts.res,
isSelfHosted: process.env.NEXT_PUBLIC_SELF_HOSTED === "true",
emailClient,
};
}

View file

@ -1,5 +1,4 @@
import { prisma } from "@rallly/database";
import { sendEmail } from "@rallly/emails";
import { absoluteUrl } from "@rallly/utils";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
@ -78,6 +77,7 @@ export const auth = router({
.mutation(
async ({
input,
ctx,
}): Promise<
| { ok: true; token: string }
| { ok: false; reason: "userAlreadyExists" | "emailNotAllowed" }
@ -107,7 +107,7 @@ export const auth = router({
code,
});
await sendEmail("RegisterEmail", {
await ctx.emailClient.sendTemplate("RegisterEmail", {
to: input.email,
subject: `${input.name}, please verify your email address`,
props: {
@ -168,6 +168,7 @@ export const auth = router({
.mutation(
async ({
input,
ctx,
}): Promise<
| { ok: true; token: string }
| { ok: false; reason: "emailNotAllowed" | "userNotFound" }
@ -193,7 +194,7 @@ export const auth = router({
code,
});
await sendEmail("LoginEmail", {
await ctx.emailClient.sendTemplate("LoginEmail", {
to: input.email,
subject: `${code} is your 6-digit code`,
props: {

View file

@ -1,5 +1,4 @@
import { prisma } from "@rallly/database";
import { sendEmail } from "@rallly/emails";
import { absoluteUrl, shortUrl } from "@rallly/utils";
import { TRPCError } from "@trpc/server";
import dayjs from "dayjs";
@ -133,7 +132,7 @@ export const polls = router({
});
if (user) {
await sendEmail("NewPollEmail", {
await ctx.emailClient.sendTemplate("NewPollEmail", {
to: user.email,
subject: `Let's find a date for ${poll.title}`,
props: {
@ -678,7 +677,7 @@ export const polls = router({
});
}
const emailToHost = sendEmail("FinalizeHostEmail", {
const emailToHost = ctx.emailClient.sendTemplate("FinalizeHostEmail", {
subject: `Date booked for ${poll.title}`,
to: poll.user.email,
props: {
@ -702,7 +701,7 @@ export const polls = router({
});
const emailsToParticipants = participantsToEmail.map((p) => {
return sendEmail("FinalizeParticipantEmail", {
return ctx.emailClient.sendTemplate("FinalizeParticipantEmail", {
subject: `Date booked for ${poll.title}`,
to: p.email,
props: {

View file

@ -1,5 +1,4 @@
import { prisma } from "@rallly/database";
import { sendEmail } from "@rallly/emails";
import { absoluteUrl } from "@rallly/utils";
import { z } from "zod";
@ -82,7 +81,7 @@ export const comments = router({
{ ttl: 0 },
);
emailsToSend.push(
sendEmail("NewCommentEmail", {
ctx.emailClient.sendTemplate("NewCommentEmail", {
to: email,
subject: `${authorName} has commented on ${poll.title}`,
props: {

View file

@ -1,5 +1,4 @@
import { prisma } from "@rallly/database";
import { sendEmail } from "@rallly/emails";
import { absoluteUrl } from "@rallly/utils";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
@ -107,7 +106,7 @@ export const participants = router({
);
emailsToSend.push(
sendEmail("NewParticipantConfirmationEmail", {
ctx.emailClient.sendTemplate("NewParticipantConfirmationEmail", {
to: email,
subject: `Thanks for responding to ${poll.title}`,
props: {
@ -144,7 +143,7 @@ export const participants = router({
{ ttl: 0 },
);
emailsToSend.push(
sendEmail("NewParticipantEmail", {
ctx.emailClient.sendTemplate("NewParticipantEmail", {
to: email,
subject: `${participant.name} has responded to ${poll.title}`,
props: {