samedi 19 mars 2022

Javascript classes - what solution is more professional in sending emails

I have two classes: MailsTemplates and EmailService. First class is creating templates. Every template has its own method, because every template needs other params:

class MailsTemplates {

  createTemplate1(param1) {
    // some code
  }

  createTemplate2(param2) {
    // some code
  }

  createTemplate3(param3) {
    // some code
  }

  createTemplate4(param4) {
    // some code
  }

}

Second class EmailService has two methods: createConfigurationForEmail and sendEmail. createConfigurationForEmail has three params:

  • userEmailsArr,
  • emailSubject,
  • template (html code provided in a string type, generated by a one of the MailsTemplates method)

My code:

const MailsTemplates = require("./mailTemplates.js");

class EmailService {

  mailTemplates;
  constructor() {
    this.mailTemplates = new MailsTemplates();
  }
  
  #createConfigurationForEmail = (userEmailsArr, subject, template) => {
    // my code
  }

  sendEmail = async (
    userEmailsArr, 
    subject, 
    getTemplate, 
    param1=null, 
    param2=null, 
    param3=null,
    param4=null, 
  ) => {
    const template = getTemplate(param1, param2, param3, param4);
    const emailConfig = this.#createConfigurationForEmail(userEmailsArr, subject, template);
    return await AWSSESSendEmailMethod(emailConfig).promise();
  }

}

const emailService = new EmailService();

And now when I want to send email with template1 I use this code:

emailService.sendEmail(["email"], "Subject", emailService.mailTemplates.createTemplate1, param1);

When email with template2 this code:

emailService.sendEmail(["email"], "Subject", emailService.mailTemplates.createTemplate2, param2);

Is this proffessional enough? What to change to make my code better? Maybe should I provide in invoking sendEmail method my params in createTemplate methods?

Aucun commentaire:

Enregistrer un commentaire