<commit_before>const debug = require('debug')('app:email');

const config = require('smart-config');
const emailConfig = config.get('email');
const { promisifyAll } = require('bluebird');
const postmark = require('postmark');
const getTemplateId = require('./get-template-id');
const getTemplateVars = require('./get-template-vars');
const client = promisifyAll(new postmark.Client(emailConfig.key));

const send = async ({ email, from, to, cc, bcc, subject, message, template, vars, recipientId, locationGroup }) => {
  try {
    const toEmail = email || to;
    
    let fromEmail = from || emailConfig.from;
    if (emailConfig.from_label && emailConfig.from_label !== 'FROM_LABEL') {
      fromEmail = emailConfig.from_label.replace('/[email]/', from || emailConfig.from);
    }

    const options = {
      From: fromEmail,
      To: toEmail, Cc: cc, Bcc: bcc
    };

    if (template) {
      const templateId = getTemplateId(template);
      const resultVars = await getTemplateVars({ vars, toEmail, recipientId, locationGroup });

      return await client.sendEmailWithTemplateAsync(Object.assign({
        TemplateId: templateId,
        TemplateModel: resultVars,
      }, options));
    }

    return await client.sendEmailAsync(Object.assign({
      TextBody: message,
      Subject: subject
    }, options));

  } catch (err) {
    debug(err, { locationGroup });
    return err;
  }
};

module.exports = { send };<commit_msg>- use search<commit_after>const debug = require('debug')('app:email');

const config = require('smart-config');
const emailConfig = config.get('email');
const { promisifyAll } = require('bluebird');
const postmark = require('postmark');
const getTemplateId = require('./get-template-id');
const getTemplateVars = require('./get-template-vars');
const client = promisifyAll(new postmark.Client(emailConfig.key));

const send = async ({ email, from, to, cc, bcc, subject, message, template, vars, recipientId, locationGroup }) => {
  try {
    const toEmail = email || to;

    let fromEmail = from || emailConfig.from;
    if (emailConfig.from_label && emailConfig.from_label.search('[email]') !== -1) {
      fromEmail = emailConfig.from_label.replace('/[email]/', from || emailConfig.from);
    }

    const options = {
      From: fromEmail,
      To: toEmail, Cc: cc, Bcc: bcc
    };

    if (template) {
      const templateId = getTemplateId(template);
      const resultVars = await getTemplateVars({ vars, toEmail, recipientId, locationGroup });

      return await client.sendEmailWithTemplateAsync(Object.assign({
        TemplateId: templateId,
        TemplateModel: resultVars,
      }, options));
    }

    return await client.sendEmailAsync(Object.assign({
      TextBody: message,
      Subject: subject
    }, options));

  } catch (err) {
    debug(err, { locationGroup });
    return err;
  }
};

module.exports = { send };
{'fraction_non_alphanumeric': 0.14196459562651856, 'fraction_numerical': 0.0003471017007983339, 'mean_word_length': 3.6483870967741936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2982088', 'n_tokens_mistral': 839, 'n_tokens_neox': 835, 'n_words': 208}