All files / utils sendMail.ts

93.75% Statements 15/16
50% Branches 1/2
100% Functions 2/2
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50  4x         4x                                   4x 2x 2x             2x 2x     2x 2x 1x   1x   1x   2x     4x  
import { MailTemplates } from "@app/types/mailTemplates";
import {
  NODEMAILER_FROM,
  isMailConfigured,
  transporter,
} from "@app/config/mailer.setup";
import { NODE_ENV } from "@app/constants";
 
// -------------------Utility Function--------------------------
 
type Props<T extends keyof MailTemplates> = {
  // Receiver mail id
  to: string;
  // Template name which will be used
  template: T;
  // Data which is required by template
  context: MailTemplates[T];
};
 
/**
 * Send mail
 * @param props
 * @returns
 */
const sendMail = async <T extends keyof MailTemplates>(props: Props<T>) => {
  const { to, template, context } = props;
  const mailOption = {
    from: NODEMAILER_FROM,
    to,
    subject: context.subject,
    template,
    context,
  };
  let data = null;
  Iif (["local", "staging"].includes(NODE_ENV)) {
    console.log("Sending mail", { mailOption });
  }
  try {
    if (!isMailConfigured) {
      throw new Error("Mail is not configured");
    }
    data = await transporter.sendMail(mailOption);
  } catch (error) {
    console.error("error while sending mail", { error });
  }
  return data;
};
 
export { sendMail };