chore: refactor sendMail to make it reusable

This commit is contained in:
Ephraim Atta-Duncan
2023-06-21 14:09:42 +00:00
parent 2798cd624d
commit 0456fe9826
5 changed files with 41 additions and 27 deletions

View File

@ -14,7 +14,13 @@ export default function Send() {
onClick={async () => {
console.log('clicked');
await sendMail({ email: 'duncan@documenso.com' });
await sendMail({
email: 'duncan@documenso.com',
type: 'invite',
documentName: 'Documsenso Salary Invoice.pdf',
name: 'Mark Cuban',
firstName: 'Mark',
});
alert('sent');
}}

View File

@ -90,12 +90,12 @@ export const DocumensoEmail = ({
</Text>
)}
<Text className="mx-0 mb-0 text-center text-[18px] font-semibold text-[#27272A]">
<Text className="mx-0 mb-0 text-center text-[16px] font-semibold text-[#27272A]">
{type === 'invite'
? `${name} has invited you to sign “${documentName}`
: `${documentName}” was signed by ${name}`}
</Text>
<Text className="my-1 text-center text-[16px] text-[#AFAFAF]">
<Text className="my-1 text-center text-[14px] text-[#AFAFAF]">
{type === 'invite'
? 'Continue by signing the document.'
: 'Continue by downloading or reviewing the document.'}
@ -105,7 +105,7 @@ export const DocumensoEmail = ({
<Button
pX={20}
pY={12}
className="rounded bg-[#A2E771] px-10 text-center text-[14px] font-medium text-black no-underline"
className="rounded bg-[#A2E771] text-center text-[14px] font-medium text-black no-underline"
href={documentSigningLink}
>
Sign Document

View File

@ -1,9 +1,19 @@
import nodemailer from 'nodemailer';
import nodemailerSendgrid from 'nodemailer-sendgrid';
import { TSendMailMutationSchema } from '@documenso/trpc/server/mail-router/schema';
import { emailHtml, emailText } from '../../mail/template';
export const sendMail = async ({ email }: { email: string }) => {
interface SendMail {
template: TSendMailMutationSchema;
mail: {
from: string;
subject: string;
};
}
export const sendMail = async ({ template, mail }: SendMail) => {
let transporter;
if (process.env.NEXT_PRIVATE_SENDGRID_API_KEY) {
@ -31,25 +41,11 @@ export const sendMail = async ({ email }: { email: string }) => {
);
}
const html = emailHtml({
email: 'lucas@documenso.com',
name: 'Lucas Smith',
documentName: 'NDA.pdf',
firstName: 'Lucas',
type: 'signed',
});
await transporter.sendMail({
from: 'Documenso <hi@documenso.com>',
to: email,
subject: 'Welcome to Documenso!',
text: emailText({
email: 'lucas@documenso.com',
name: 'Lucas Smith',
documentName: 'NDA.pdf',
firstName: 'Lucas',
type: 'completed',
}),
html,
from: mail.from,
to: template.email,
subject: mail.subject,
text: emailText({ ...template }),
html: emailHtml({ ...template }),
});
};

View File

@ -8,9 +8,13 @@ import { ZSendMailMutationSchema } from './schema';
export const mailRouter = router({
send: authenticatedProcedure.input(ZSendMailMutationSchema).mutation(async ({ input }) => {
try {
const { email } = input;
return await sendMail({ email });
return await sendMail({
template: input,
mail: {
from: '<hi@documenso>',
subject: 'Documeso Invite',
},
});
} catch (err) {
console.error(err);

View File

@ -2,6 +2,14 @@ import { z } from 'zod';
export const ZSendMailMutationSchema = z.object({
email: z.string().min(1).email(),
name: z.string().min(1).optional(),
firstName: z.string().min(1).optional(),
documentSigningLink: z.string().min(1).optional(),
documentName: z.string().min(1).optional(),
downloadLink: z.string().min(1).optional(),
reviewLink: z.string().min(1).optional(),
numberOfSigners: z.number().int().min(1).optional(),
type: z.enum(['invite', 'signed', 'completed']),
});
export type TSendMailMutationSchema = z.infer<typeof ZSendMailMutationSchema>;