All files / emp_audit_system/services emailService.js

18.75% Statements 3/16
100% Branches 0/0
0% Functions 0/3
18.75% Lines 3/16

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 816x 6x                                                                                                                                                             6x
const nodemailer = require('nodemailer');
const logger = require('../utils/logger');
 
class EmailService {
  constructor() {
    this.transporter = nodemailer.createTransport({
      service: process.env.EMAIL_SERVICE,
      auth: {
        user: process.env.EMAIL_USER,
        pass: process.env.EMAIL_PASSWORD
      },
      pool: true,
      rateLimit: true,
      maxConnections: 1,
      maxMessages: 5
    });
  }
 
  static async sendWelcomeEmail(email, username) {
    try {
      const mailOptions = {
        from: process.env.EMAIL_FROM,
        to: email,
        subject: 'Welcome to EMP Audit System',
        html: `
          <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
            <h2 style="color: #2c3e50;">Welcome, ${username}!</h2>
            <p>Your account has been successfully created with the EMP Audit System.</p>
            <p>You can now login to start conducting environmental compliance audits.</p>
            <p style="margin-top: 30px;">
              <a href="${process.env.BASE_URL}/auth/login" 
                 style="background-color: #3498db; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;">
                Login to Your Account
              </a>
            </p>
            <p style="margin-top: 30px; font-size: 12px; color: #7f8c8d;">
              If you did not request this account, please contact our support team immediately.
            </p>
          </div>
        `
      };
 
      await this.transporter.sendMail(mailOptions);
      logger.info(`Welcome email sent to ${email}`);
    } catch (error) {
      logger.error(`Error sending welcome email: ${error.message}`, { email });
      throw error;
    }
  }
 
  static async sendAuditSubmissionEmail(email, auditId, establishmentName) {
    try {
      const mailOptions = {
        from: process.env.EMAIL_FROM,
        to: email,
        subject: `Audit Submitted for ${establishmentName}`,
        html: `
          <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
            <h2 style="color: #2c3e50;">Audit Submission Confirmation</h2>
            <p>Your environmental audit for <strong>${establishmentName}</strong> has been successfully submitted.</p>
            <p>Audit ID: ${auditId}</p>
            <p style="margin-top: 30px;">
              <a href="${process.env.BASE_URL}/audits/${auditId}" 
                 style="background-color: #3498db; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;">
                View Audit Details
              </a>
            </p>
          </div>
        `
      };
 
      await this.transporter.sendMail(mailOptions);
      logger.info(`Audit submission email sent to ${email} for audit ${auditId}`);
    } catch (error) {
      logger.error(`Error sending audit submission email: ${error.message}`, { email, auditId });
      throw error;
    }
  }
}
 
module.exports = EmailService;