Skip to content

Commit

Permalink
mail templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Aug 28, 2024
1 parent 50abcc8 commit 8923b0b
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import { MailTemplateKey } from "~/utils/enums";

type IMailTemplate = {
subject: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ const DeviceIpChangeNotificationTemplate = () => {
refetch: refetchMailTemplates,
isLoading: loadingTemplates,
} = api.admin.getMailTemplates.useQuery({
template: "deviceIpChangeNotificationTemplate",
template: MailTemplateKey.DeviceIpChangeNotification,
});

const changeTemplateHandler = (
Expand Down Expand Up @@ -103,7 +104,7 @@ const DeviceIpChangeNotificationTemplate = () => {
setMailTemplates(
{
template: JSON.stringify(stateTemplate),
type: "deviceIpChangeNotificationTemplate",
type: MailTemplateKey.DeviceIpChangeNotification,
},
{
onSuccess: handleApiSuccess({
Expand Down Expand Up @@ -179,7 +180,7 @@ const DeviceIpChangeNotificationTemplate = () => {
className="btn btn-sm"
onClick={() =>
getDefaultMailTemplate({
template: "deviceIpChangeNotificationTemplate",
template: MailTemplateKey.DeviceIpChangeNotification,
})
}
>
Expand All @@ -190,7 +191,9 @@ const DeviceIpChangeNotificationTemplate = () => {
<button
className="btn btn-sm"
disabled={changes.subject || changes.body || sendingMailLoading}
onClick={() => sendTestMail({ type: "deviceIpChangeNotificationTemplate" })}
onClick={() =>
sendTestMail({ type: MailTemplateKey.DeviceIpChangeNotification })
}
>
{sendingMailLoading
? t("mail.templates.sendTestMailButtonLoading")
Expand Down
9 changes: 5 additions & 4 deletions src/components/adminPage/mail/mailForgotPasswordTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import { MailTemplateKey } from "~/utils/enums";

type InviteUserTemplate = {
subject: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ const ForgotPasswordMailTemplate = () => {
refetch: refetchMailTemplates,
isLoading: loadingTemplates,
} = api.admin.getMailTemplates.useQuery({
template: "forgotPasswordTemplate",
template: MailTemplateKey.ForgotPassword,
});

const changeTemplateHandler = (
Expand Down Expand Up @@ -104,7 +105,7 @@ const ForgotPasswordMailTemplate = () => {
setMailTemplates(
{
template: JSON.stringify(emailTemplate),
type: "forgotPasswordTemplate",
type: MailTemplateKey.ForgotPassword,
},
{
onSuccess: () => {
Expand Down Expand Up @@ -178,7 +179,7 @@ const ForgotPasswordMailTemplate = () => {
className="btn btn-sm"
onClick={() =>
getDefaultMailTemplate({
template: "forgotPasswordTemplate",
template: MailTemplateKey.ForgotPassword,
})
}
>
Expand All @@ -189,7 +190,7 @@ const ForgotPasswordMailTemplate = () => {
<button
className="btn btn-sm"
disabled={changes.subject || changes.body || sendingMailLoading}
onClick={() => sendTestMail({ type: "forgotPasswordTemplate" })}
onClick={() => sendTestMail({ type: MailTemplateKey.ForgotPassword })}
>
{sendingMailLoading
? t("mail.templates.sendTestMailButtonLoading")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import { MailTemplateKey } from "~/utils/enums";

type IMailTemplate = {
subject: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ const NewDeviceNotificationTemplate = () => {
refetch: refetchMailTemplates,
isLoading: loadingTemplates,
} = api.admin.getMailTemplates.useQuery({
template: "newDeviceNotificationTemplate",
template: MailTemplateKey.NewDeviceNotification,
});

const changeTemplateHandler = (
Expand Down Expand Up @@ -103,7 +104,7 @@ const NewDeviceNotificationTemplate = () => {
setMailTemplates(
{
template: JSON.stringify(stateTemplate),
type: "newDeviceNotificationTemplate",
type: MailTemplateKey.NewDeviceNotification,
},
{
onSuccess: handleApiSuccess({
Expand Down Expand Up @@ -179,7 +180,7 @@ const NewDeviceNotificationTemplate = () => {
className="btn btn-sm"
onClick={() =>
getDefaultMailTemplate({
template: "newDeviceNotificationTemplate",
template: MailTemplateKey.NewDeviceNotification,
})
}
>
Expand All @@ -190,7 +191,7 @@ const NewDeviceNotificationTemplate = () => {
<button
className="btn btn-sm"
disabled={changes.subject || changes.body || sendingMailLoading}
onClick={() => sendTestMail({ type: "newDeviceNotificationTemplate" })}
onClick={() => sendTestMail({ type: MailTemplateKey.NewDeviceNotification })}
>
{sendingMailLoading
? t("mail.templates.sendTestMailButtonLoading")
Expand Down
9 changes: 5 additions & 4 deletions src/components/adminPage/mail/mailNotificationTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import { MailTemplateKey } from "~/utils/enums";

type IMailTemplate = {
subject: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ const NotificationTemplate = () => {
refetch: refetchMailTemplates,
isLoading: loadingTemplates,
} = api.admin.getMailTemplates.useQuery({
template: "notificationTemplate",
template: MailTemplateKey.Notification,
});

const changeTemplateHandler = (
Expand Down Expand Up @@ -103,7 +104,7 @@ const NotificationTemplate = () => {
setMailTemplates(
{
template: JSON.stringify(stateTemplate),
type: "notificationTemplate",
type: MailTemplateKey.Notification,
},
{
onSuccess: handleApiSuccess({
Expand Down Expand Up @@ -176,7 +177,7 @@ const NotificationTemplate = () => {
className="btn btn-sm"
onClick={() =>
getDefaultMailTemplate({
template: "notificationTemplate",
template: MailTemplateKey.Notification,
})
}
>
Expand All @@ -187,7 +188,7 @@ const NotificationTemplate = () => {
<button
className="btn btn-sm"
disabled={changes.subject || changes.body || sendingMailLoading}
onClick={() => sendTestMail({ type: "notificationTemplate" })}
onClick={() => sendTestMail({ type: MailTemplateKey.Notification })}
>
{sendingMailLoading
? t("mail.templates.sendTestMailButtonLoading")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import { MailTemplateKey } from "~/utils/enums";

type IMailTemplate = {
subject: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ const OrganizationInviteTemplate = () => {
refetch: refetchMailTemplates,
isLoading: loadingTemplates,
} = api.admin.getMailTemplates.useQuery({
template: "inviteOrganizationTemplate",
template: MailTemplateKey.InviteOrganization,
});

const changeTemplateHandler = (
Expand Down Expand Up @@ -103,7 +104,7 @@ const OrganizationInviteTemplate = () => {
setMailTemplates(
{
template: JSON.stringify(stateTemplate),
type: "inviteOrganizationTemplate",
type: MailTemplateKey.InviteOrganization,
},
{
onSuccess: handleApiSuccess({
Expand Down Expand Up @@ -178,7 +179,7 @@ const OrganizationInviteTemplate = () => {
className="btn btn-sm"
onClick={() =>
getDefaultMailTemplate({
template: "inviteOrganizationTemplate",
template: MailTemplateKey.InviteOrganization,
})
}
>
Expand All @@ -189,7 +190,7 @@ const OrganizationInviteTemplate = () => {
<button
className="btn btn-sm"
disabled={changes.subject || changes.body || sendingMailLoading}
onClick={() => sendTestMail({ type: "inviteOrganizationTemplate" })}
onClick={() => sendTestMail({ type: MailTemplateKey.InviteOrganization })}
>
{sendingMailLoading
? t("mail.templates.sendTestMailButtonLoading")
Expand Down
9 changes: 5 additions & 4 deletions src/components/adminPage/mail/mailUserInviteTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import { MailTemplateKey } from "~/utils/enums";

type InviteUserTemplate = {
subject: string;
Expand All @@ -30,7 +31,7 @@ const MailUserInviteTemplate = () => {
refetch: refetchMailTemplates,
isLoading: loadingTemplates,
} = api.admin.getMailTemplates.useQuery({
template: "inviteUserTemplate",
template: MailTemplateKey.InviteUser,
});

const [emailTemplate, setEmailTemplate] = useState({
Expand Down Expand Up @@ -99,7 +100,7 @@ const MailUserInviteTemplate = () => {
setMailTemplates(
{
template: JSON.stringify(emailTemplate),
type: "inviteUserTemplate",
type: MailTemplateKey.InviteUser,
},
{
onSuccess: handleApiSuccess({
Expand Down Expand Up @@ -175,7 +176,7 @@ const MailUserInviteTemplate = () => {
className="btn btn-sm"
onClick={() =>
getDefaultMailTemplate({
template: "inviteUserTemplate",
template: MailTemplateKey.InviteUser,
})
}
>
Expand All @@ -186,7 +187,7 @@ const MailUserInviteTemplate = () => {
<button
className="btn btn-sm"
disabled={changes.subject || changes.body || sendingMailLoading}
onClick={() => sendTestMail({ type: "inviteUserTemplate" })}
onClick={() => sendTestMail({ type: MailTemplateKey.InviteUser })}
>
{sendingMailLoading
? t("mail.templates.sendTestMailButtonLoading")
Expand Down
9 changes: 5 additions & 4 deletions src/components/adminPage/mail/mailVerifyEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import { MailTemplateKey } from "~/utils/enums";

type InviteUserTemplate = {
subject: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ const VerifyEmailTemplate = () => {
refetch: refetchMailTemplates,
isLoading: loadingTemplates,
} = api.admin.getMailTemplates.useQuery({
template: "verifyEmailTemplate",
template: MailTemplateKey.VerifyEmail,
});

const changeTemplateHandler = (
Expand Down Expand Up @@ -104,7 +105,7 @@ const VerifyEmailTemplate = () => {
setMailTemplates(
{
template: JSON.stringify(emailTemplate),
type: "verifyEmailTemplate",
type: MailTemplateKey.VerifyEmail,
},
{
onSuccess: () => {
Expand Down Expand Up @@ -178,7 +179,7 @@ const VerifyEmailTemplate = () => {
className="btn btn-sm"
onClick={() =>
getDefaultMailTemplate({
template: "verifyEmailTemplate",
template: MailTemplateKey.VerifyEmail,
})
}
>
Expand All @@ -189,7 +190,7 @@ const VerifyEmailTemplate = () => {
<button
className="btn btn-sm"
disabled={changes.subject || changes.body || sendingMailLoading}
onClick={() => sendTestMail({ type: "verifyEmailTemplate" })}
onClick={() => sendTestMail({ type: MailTemplateKey.VerifyEmail })}
>
{sendingMailLoading
? t("mail.templates.sendTestMailButtonLoading")
Expand Down
5 changes: 3 additions & 2 deletions src/server/api/routers/adminRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ZT_FOLDER } from "~/utils/ztApi";
import { isRunningInDocker } from "~/utils/docker";
import { getNetworkClassCIDR } from "~/utils/IPv4gen";
import { InvitationLinkType } from "~/types/invitation";
import { MailTemplateKey } from "~/utils/enums";

type WithError<T> = T & { error?: boolean; message?: string };

Expand Down Expand Up @@ -434,7 +435,7 @@ export const adminRouter = createTRPCRouter({
sendTestMail: adminRoleProtectedRoute
.input(
z.object({
type: z.string(),
type: z.nativeEnum(MailTemplateKey),
}),
)
.mutation(async ({ ctx, input }) => {
Expand Down Expand Up @@ -473,7 +474,7 @@ export const adminRouter = createTRPCRouter({
verifyLink: "https://ztnet.network/verify-email",
};

await sendMailWithTemplate(mailTemplateMap[type], {
await sendMailWithTemplate(type, {
to: user.email,
userId: user.id,
templateData,
Expand Down
14 changes: 5 additions & 9 deletions src/server/api/routers/authRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import {
import { TRPCError } from "@trpc/server";
import { throwError } from "~/server/helpers/errorHandler";
import jwt from "jsonwebtoken";
import {
forgotPasswordTemplate,
notificationTemplate,
sendMailWithTemplate,
verifyEmailTemplate,
} from "~/utils/mail";
import { sendMailWithTemplate } from "~/utils/mail";
import * as ztController from "~/utils/ztApi";
import {
API_TOKEN_SECRET,
Expand All @@ -28,6 +23,7 @@ import { Invitation, User, UserDevice, UserOptions } from "@prisma/client";
import { validateOrganizationToken } from "../services/organizationAuthService";
import rateLimit from "~/utils/rateLimit";
import { ErrorCode } from "~/utils/errorCode";
import { MailTemplateKey } from "~/utils/enums";

// This regular expression (regex) is used to validate a password based on the following criteria:
// - The password must be at least 6 characters long.
Expand Down Expand Up @@ -304,7 +300,7 @@ export const authRouter = createTRPCRouter({
});

for (const adminUser of adminUsers) {
await sendMailWithTemplate(notificationTemplate, {
await sendMailWithTemplate(MailTemplateKey.Notification, {
to: adminUser.email,
userId: adminUser.id,
templateData: {
Expand Down Expand Up @@ -546,7 +542,7 @@ export const authRouter = createTRPCRouter({
const resetLink = `${process.env.NEXTAUTH_URL}/auth/forgotPassword/reset?token=${validationToken}`;
// Send email
try {
await sendMailWithTemplate(forgotPasswordTemplate, {
await sendMailWithTemplate(MailTemplateKey.ForgotPassword, {
to: email,
userId: user.id,
templateData: {
Expand Down Expand Up @@ -656,7 +652,7 @@ export const authRouter = createTRPCRouter({
const verifyLink = `${process.env.NEXTAUTH_URL}/auth/verifyEmail?token=${validationToken}`;
// Send email
try {
await sendMailWithTemplate(verifyEmailTemplate, {
await sendMailWithTemplate(MailTemplateKey.VerifyEmail, {
to: user.email,
userId: user.id,
templateData: {
Expand Down
Loading

0 comments on commit 8923b0b

Please sign in to comment.