mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
Update all template imports (#414)
This commit is contained in:
parent
8e6b2589b0
commit
25d50a9768
7 changed files with 28 additions and 26 deletions
|
@ -2,6 +2,8 @@ import { TRPCError } from "@trpc/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { prisma } from "~/prisma/db";
|
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 { absoluteUrl } from "../../utils/absolute-url";
|
||||||
import { sendEmailTemplate } from "../../utils/api-utils";
|
import { sendEmailTemplate } from "../../utils/api-utils";
|
||||||
|
@ -149,7 +151,7 @@ export const polls = createRouter()
|
||||||
try {
|
try {
|
||||||
if (poll.verified) {
|
if (poll.verified) {
|
||||||
await sendEmailTemplate({
|
await sendEmailTemplate({
|
||||||
templateName: "new-poll-verified",
|
templateString: newVerfiedPollTemplate,
|
||||||
to: input.user.email,
|
to: input.user.email,
|
||||||
subject: `Rallly: ${poll.title}`,
|
subject: `Rallly: ${poll.title}`,
|
||||||
templateVars: {
|
templateVars: {
|
||||||
|
@ -167,7 +169,7 @@ export const polls = createRouter()
|
||||||
const verifyEmailUrl = `${pollUrl}?code=${verificationCode}`;
|
const verifyEmailUrl = `${pollUrl}?code=${verificationCode}`;
|
||||||
|
|
||||||
await sendEmailTemplate({
|
await sendEmailTemplate({
|
||||||
templateName: "new-poll",
|
templateString: newPollTemplate,
|
||||||
to: input.user.email,
|
to: input.user.email,
|
||||||
subject: `Rallly: ${poll.title} - Verify your email address`,
|
subject: `Rallly: ${poll.title} - Verify your email address`,
|
||||||
templateVars: {
|
templateVars: {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { TRPCError } from "@trpc/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { prisma } from "~/prisma/db";
|
import { prisma } from "~/prisma/db";
|
||||||
|
import newPollTemplate from "~/templates/new-poll";
|
||||||
|
|
||||||
import { absoluteUrl } from "../../../utils/absolute-url";
|
import { absoluteUrl } from "../../../utils/absolute-url";
|
||||||
import { sendEmailTemplate } from "../../../utils/api-utils";
|
import { sendEmailTemplate } from "../../../utils/api-utils";
|
||||||
|
@ -83,7 +84,7 @@ export const verification = createRouter()
|
||||||
const verifyEmailUrl = `${pollUrl}?code=${token}`;
|
const verifyEmailUrl = `${pollUrl}?code=${token}`;
|
||||||
|
|
||||||
await sendEmailTemplate({
|
await sendEmailTemplate({
|
||||||
templateName: "new-poll",
|
templateString: newPollTemplate,
|
||||||
to: poll.user.email,
|
to: poll.user.email,
|
||||||
subject: `Rallly: ${poll.title} - Verify your email address`,
|
subject: `Rallly: ${poll.title} - Verify your email address`,
|
||||||
templateVars: {
|
templateVars: {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
import * as Eta from "eta";
|
import * as Eta from "eta";
|
||||||
import { readFileSync } from "fs";
|
|
||||||
import path from "path";
|
|
||||||
|
|
||||||
import { prisma } from "~/prisma/db";
|
import { prisma } from "~/prisma/db";
|
||||||
|
import newCommentTemplate from "~/templates/new-comment";
|
||||||
|
|
||||||
import { absoluteUrl } from "./absolute-url";
|
import { absoluteUrl } from "./absolute-url";
|
||||||
import { sendEmail } from "./send-email";
|
import { sendEmail } from "./send-email";
|
||||||
|
@ -47,7 +46,7 @@ export const sendNotification = async (
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case "newParticipant":
|
case "newParticipant":
|
||||||
await sendEmailTemplate({
|
await sendEmailTemplate({
|
||||||
templateName: "new-participant",
|
templateString: newCommentTemplate,
|
||||||
to: poll.user.email,
|
to: poll.user.email,
|
||||||
subject: `Rallly: ${poll.title} - New Participant`,
|
subject: `Rallly: ${poll.title} - New Participant`,
|
||||||
templateVars: {
|
templateVars: {
|
||||||
|
@ -63,7 +62,7 @@ export const sendNotification = async (
|
||||||
break;
|
break;
|
||||||
case "newComment":
|
case "newComment":
|
||||||
await sendEmailTemplate({
|
await sendEmailTemplate({
|
||||||
templateName: "new-comment",
|
templateString: newCommentTemplate,
|
||||||
to: poll.user.email,
|
to: poll.user.email,
|
||||||
subject: `Rallly: ${poll.title} - New Comment`,
|
subject: `Rallly: ${poll.title} - New Comment`,
|
||||||
templateVars: {
|
templateVars: {
|
||||||
|
@ -85,27 +84,19 @@ export const sendNotification = async (
|
||||||
};
|
};
|
||||||
|
|
||||||
interface SendEmailTemplateParams {
|
interface SendEmailTemplateParams {
|
||||||
templateName?: string;
|
templateString: string;
|
||||||
templateString?: string;
|
|
||||||
to: string;
|
to: string;
|
||||||
subject: string;
|
subject: string;
|
||||||
templateVars: Record<string, string | undefined>;
|
templateVars: Record<string, string | undefined>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sendEmailTemplate = async ({
|
export const sendEmailTemplate = async ({
|
||||||
templateName,
|
|
||||||
templateString,
|
templateString,
|
||||||
templateVars,
|
templateVars,
|
||||||
to,
|
to,
|
||||||
subject,
|
subject,
|
||||||
}: SendEmailTemplateParams) => {
|
}: SendEmailTemplateParams) => {
|
||||||
const template =
|
const rendered = await Eta.render(templateString, templateVars);
|
||||||
templateString ??
|
|
||||||
readFileSync(
|
|
||||||
path.join(process.cwd(), "templates", `${templateName}.html`),
|
|
||||||
).toString();
|
|
||||||
|
|
||||||
const rendered = await Eta.render(template, templateVars);
|
|
||||||
|
|
||||||
if (rendered) {
|
if (rendered) {
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
|
@ -114,6 +105,6 @@ export const sendEmailTemplate = async ({
|
||||||
subject,
|
subject,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Failed to render email template: ${templateName}`);
|
throw new Error(`Failed to render email template`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<!DOCTYPE html>
|
const template = `<!DOCTYPE html>
|
||||||
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
|
@ -149,4 +149,6 @@
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>`;
|
||||||
|
|
||||||
|
export default template;
|
|
@ -1,4 +1,4 @@
|
||||||
<!DOCTYPE html>
|
const template = `<!DOCTYPE html>
|
||||||
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
|
@ -149,4 +149,6 @@
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>`;
|
||||||
|
|
||||||
|
export default template;
|
|
@ -1,4 +1,4 @@
|
||||||
<!DOCTYPE html>
|
const template = `<!DOCTYPE html>
|
||||||
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
|
@ -146,4 +146,6 @@
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>`;
|
||||||
|
|
||||||
|
export default template;
|
|
@ -1,4 +1,4 @@
|
||||||
<!DOCTYPE html>
|
const template = `<!DOCTYPE html>
|
||||||
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
|
@ -156,4 +156,6 @@
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>`;
|
||||||
|
|
||||||
|
export default template;
|
Loading…
Add table
Add a link
Reference in a new issue