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 () => { onClick={async () => {
console.log('clicked'); 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'); alert('sent');
}} }}

View File

@ -90,12 +90,12 @@ export const DocumensoEmail = ({
</Text> </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' {type === 'invite'
? `${name} has invited you to sign “${documentName}` ? `${name} has invited you to sign “${documentName}`
: `${documentName}” was signed by ${name}`} : `${documentName}” was signed by ${name}`}
</Text> </Text>
<Text className="my-1 text-center text-[16px] text-[#AFAFAF]"> <Text className="my-1 text-center text-[14px] text-[#AFAFAF]">
{type === 'invite' {type === 'invite'
? 'Continue by signing the document.' ? 'Continue by signing the document.'
: 'Continue by downloading or reviewing the document.'} : 'Continue by downloading or reviewing the document.'}
@ -105,7 +105,7 @@ export const DocumensoEmail = ({
<Button <Button
pX={20} pX={20}
pY={12} 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} href={documentSigningLink}
> >
Sign Document Sign Document

View File

@ -1,9 +1,19 @@
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import nodemailerSendgrid from 'nodemailer-sendgrid'; import nodemailerSendgrid from 'nodemailer-sendgrid';
import { TSendMailMutationSchema } from '@documenso/trpc/server/mail-router/schema';
import { emailHtml, emailText } from '../../mail/template'; 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; let transporter;
if (process.env.NEXT_PRIVATE_SENDGRID_API_KEY) { 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({ await transporter.sendMail({
from: 'Documenso <hi@documenso.com>', from: mail.from,
to: email, to: template.email,
subject: 'Welcome to Documenso!', subject: mail.subject,
text: emailText({ text: emailText({ ...template }),
email: 'lucas@documenso.com', html: emailHtml({ ...template }),
name: 'Lucas Smith',
documentName: 'NDA.pdf',
firstName: 'Lucas',
type: 'completed',
}),
html,
}); });
}; };

View File

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

View File

@ -2,6 +2,14 @@ import { z } from 'zod';
export const ZSendMailMutationSchema = z.object({ export const ZSendMailMutationSchema = z.object({
email: z.string().min(1).email(), 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>; export type TSendMailMutationSchema = z.infer<typeof ZSendMailMutationSchema>;