{"size": 1552, "ext": "js", "max_stars_repo_path": "CandidatesBackEnd/services/emailer.service.js", "max_stars_repo_name": "Luis-C/Candidates", "max_stars_repo_stars_event_min_datetime": "2020-05-19T14:55:18.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-19T14:55:18.000Z", "max_issues_repo_path": "CandidatesBackEnd/services/emailer.service.js", "max_issues_repo_name": "Luis-C/Candidates", "max_issues_repo_issues_event_min_datetime": "2021-05-11T14:01:25.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T14:01:25.000Z", "max_forks_repo_path": "CandidatesBackEnd/services/emailer.service.js", "max_forks_repo_name": "Luis-C/Candidates", "max_forks_repo_forks_event_min_datetime": "2020-05-12T18:40:26.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-12T18:40:26.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 26.7586206897, "max_line_length": 76, "alphanum_fraction": 0.6275773196}
const nodemailer = require("nodemailer");
const db = require("../_helpers/database");
module.exports = {
  considerMail,
  mail,
};

// must use a wrapper
async function considerMail(Stalker, User, user) {
  user.skills.map((skill) => {
    if (!skill.experience) {
      skill.experience = { $lte: 0 };
    } else {
      skill.experience = { $lte: skill.experience };
    }
    return skill;
  });
  let stalkers = Stalker.find();
  let skills = user.skills;
  for (let skillIndex in skills) {
    // console.log("Skill[index]: ", skills[skillIndex])
    Stalker.find({ skillMinimums: { $elemMatch: skills[skillIndex] } });
  }
  stalkers = await stalkers;
  for (let i in stalkers) {
    console.log("Stalker: ", stalkers[i]);
    if (stalkers[i].email) {
      await mail(
        stalkers[i].email,
        "A new potential applicant has arrived! Email them at " + user.email
      );
    }
  }
  if (stalkers && stalkers.length > 0) {
  }
}
async function mail(recievers, candidateInfo, subject) {
  if (!subject) {
    subject = "New Relevant Applicant";
  }
  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    service: "gmail",
    auth: {
      user: "USERNAME",
      pass: "PASSWORD",
    },
  });

  // send mail with defined transport object
  await transporter.sendMail({
    from: '"Fred Foo 👻" <foo@example.com>', // sender address
    to: recievers, // list of receivers
    subject: subject, // Subject line
    text: candidateInfo, // plain text body
  });
}
