Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- new CronJob('00 28 23 * * *', function () {
- User.find(function (err, users) {
- if (err) res.send(err);
- for (let i = 0; i < users.length; i++) {
- Receita.find({ paciente: users[i].id }, function (err, receitasPessoa) {
- if (err) res.send(err);
- var prescricoes_alerta = [];
- receitasPessoa.forEach(function (receita) {
- receita.prescricoes.forEach(function (prescricao) {
- if (prescricao.closed == false) {
- var data_atual = new Date().toISOString().substring(0, 10);
- var data_validade = new Date(prescricao.validade).toISOString().substring(0, 10);
- var data_pesquisa = new Date(new Date().setTime(new Date().getTime() + config.num_dias_alerta * 86400000)).toISOString().substring(0, 10);
- if (data_pesquisa >= data_validade && data_atual <= data_validade) { // && data_validade >= data_pesquisa ) {
- if (prescricao.closed === false) {
- var presc = {
- "prescricao_id": prescricao._id,
- "prescricao_validade": prescricao.validade,
- "receita_id": receita._id,
- }
- prescricoes_alerta.push(presc);
- }
- }
- }
- });
- });
- if (prescricoes_alerta.length > 0) {
- console.log(prescricoes_alerta.length);
- var pres = [];
- for (let i = 0; i < prescricoes_alerta.length; i++) {
- var aux = prescricoes_alerta[i];
- var aux_data = prescricoes_alerta[i].prescricao_validade.toISOString().substring(0, 10);
- pres[i] = "Prescrição "
- + prescricoes_alerta[i].prescricao_id
- + " da Receita " + prescricoes_alerta[i].receita_id
- + " com a validade até " + aux_data;
- }
- console.log('a enviar email');
- var env = pres.toString();
- var formattedString = env.split(",").join("\n");
- let transporter = nodemailer.createTransport({
- service: 'gmail',
- secure: false,
- port: 25,
- auth: {
- user: "arqsiteste3@gmail.com",
- pass: "!arqsiteste"
- },
- tls: {
- rejectUnauthorized: false
- }
- });
- let HelperOptions = {
- from: "arqsiteste3@gmail.com",
- //to: todasPessoas[i].email,
- to: "arqsiteste3@gmail.com",
- subject: "Receitas a Expirar [ARQSI]",
- text: formattedString
- };
- transporter.sendMail(HelperOptions, (error, info) => {
- if (error) {
- return console.log(error);
- } else {
- res.json(info)
- }
- });
- }
- });
- }
- });
- }, null,
- true /* Start the job right now */
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement