Update all template imports (#414)

This commit is contained in:
Luke Vella 2023-01-20 12:17:58 +00:00 committed by GitHub
parent 8e6b2589b0
commit 25d50a9768
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 26 deletions

View file

@ -2,6 +2,8 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { prisma } from "~/prisma/db";
import newPollTemplate from "~/templates/new-poll";
import newVerfiedPollTemplate from "~/templates/new-poll-verified";
import { absoluteUrl } from "../../utils/absolute-url";
import { sendEmailTemplate } from "../../utils/api-utils";
@ -149,7 +151,7 @@ export const polls = createRouter()
try {
if (poll.verified) {
await sendEmailTemplate({
templateName: "new-poll-verified",
templateString: newVerfiedPollTemplate,
to: input.user.email,
subject: `Rallly: ${poll.title}`,
templateVars: {
@ -167,7 +169,7 @@ export const polls = createRouter()
const verifyEmailUrl = `${pollUrl}?code=${verificationCode}`;
await sendEmailTemplate({
templateName: "new-poll",
templateString: newPollTemplate,
to: input.user.email,
subject: `Rallly: ${poll.title} - Verify your email address`,
templateVars: {

View file

@ -2,6 +2,7 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { prisma } from "~/prisma/db";
import newPollTemplate from "~/templates/new-poll";
import { absoluteUrl } from "../../../utils/absolute-url";
import { sendEmailTemplate } from "../../../utils/api-utils";
@ -83,7 +84,7 @@ export const verification = createRouter()
const verifyEmailUrl = `${pollUrl}?code=${token}`;
await sendEmailTemplate({
templateName: "new-poll",
templateString: newPollTemplate,
to: poll.user.email,
subject: `Rallly: ${poll.title} - Verify your email address`,
templateVars: {

View file

@ -1,8 +1,7 @@
import * as Eta from "eta";
import { readFileSync } from "fs";
import path from "path";
import { prisma } from "~/prisma/db";
import newCommentTemplate from "~/templates/new-comment";
import { absoluteUrl } from "./absolute-url";
import { sendEmail } from "./send-email";
@ -47,7 +46,7 @@ export const sendNotification = async (
switch (action.type) {
case "newParticipant":
await sendEmailTemplate({
templateName: "new-participant",
templateString: newCommentTemplate,
to: poll.user.email,
subject: `Rallly: ${poll.title} - New Participant`,
templateVars: {
@ -63,7 +62,7 @@ export const sendNotification = async (
break;
case "newComment":
await sendEmailTemplate({
templateName: "new-comment",
templateString: newCommentTemplate,
to: poll.user.email,
subject: `Rallly: ${poll.title} - New Comment`,
templateVars: {
@ -85,27 +84,19 @@ export const sendNotification = async (
};
interface SendEmailTemplateParams {
templateName?: string;
templateString?: string;
templateString: string;
to: string;
subject: string;
templateVars: Record<string, string | undefined>;
}
export const sendEmailTemplate = async ({
templateName,
templateString,
templateVars,
to,
subject,
}: SendEmailTemplateParams) => {
const template =
templateString ??
readFileSync(
path.join(process.cwd(), "templates", `${templateName}.html`),
).toString();
const rendered = await Eta.render(template, templateVars);
const rendered = await Eta.render(templateString, templateVars);
if (rendered) {
await sendEmail({
@ -114,6 +105,6 @@ export const sendEmailTemplate = async ({
subject,
});
} else {
throw new Error(`Failed to render email template: ${templateName}`);
throw new Error(`Failed to render email template`);
}
};

View file

@ -1,4 +1,4 @@
<!DOCTYPE html>
const template = `<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta charset="utf-8">
@ -149,4 +149,6 @@
</table>
</div>
</body>
</html>
</html>`;
export default template;

View file

@ -1,4 +1,4 @@
<!DOCTYPE html>
const template = `<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta charset="utf-8">
@ -149,4 +149,6 @@
</table>
</div>
</body>
</html>
</html>`;
export default template;

View file

@ -1,4 +1,4 @@
<!DOCTYPE html>
const template = `<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta charset="utf-8">
@ -146,4 +146,6 @@
</table>
</div>
</body>
</html>
</html>`;
export default template;

View file

@ -1,4 +1,4 @@
<!DOCTYPE html>
const template = `<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta charset="utf-8">
@ -156,4 +156,6 @@
</table>
</div>
</body>
</html>
</html>`;
export default template;