Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. new CronJob('00 28 23 * * *', function () {
  2. User.find(function (err, users) {
  3. if (err) res.send(err);
  4. for (let i = 0; i < users.length; i++) {
  5. Receita.find({ paciente: users[i].id }, function (err, receitasPessoa) {
  6. if (err) res.send(err);
  7. var prescricoes_alerta = [];
  8. receitasPessoa.forEach(function (receita) {
  9. receita.prescricoes.forEach(function (prescricao) {
  10. if (prescricao.closed == false) {
  11. var data_atual = new Date().toISOString().substring(0, 10);
  12. var data_validade = new Date(prescricao.validade).toISOString().substring(0, 10);
  13. var data_pesquisa = new Date(new Date().setTime(new Date().getTime() + config.num_dias_alerta * 86400000)).toISOString().substring(0, 10);
  14. if (data_pesquisa >= data_validade && data_atual <= data_validade) { // && data_validade >= data_pesquisa ) {
  15. if (prescricao.closed === false) {
  16. var presc = {
  17. "prescricao_id": prescricao._id,
  18. "prescricao_validade": prescricao.validade,
  19. "receita_id": receita._id,
  20. }
  21. prescricoes_alerta.push(presc);
  22. }
  23. }
  24. }
  25. });
  26.  
  27. });
  28. if (prescricoes_alerta.length > 0) {
  29. console.log(prescricoes_alerta.length);
  30. var pres = [];
  31. for (let i = 0; i < prescricoes_alerta.length; i++) {
  32. var aux = prescricoes_alerta[i];
  33. var aux_data = prescricoes_alerta[i].prescricao_validade.toISOString().substring(0, 10);
  34. pres[i] = "Prescrição "
  35. + prescricoes_alerta[i].prescricao_id
  36. + " da Receita " + prescricoes_alerta[i].receita_id
  37. + " com a validade até " + aux_data;
  38. }
  39. console.log('a enviar email');
  40. var env = pres.toString();
  41. var formattedString = env.split(",").join("\n");
  42. let transporter = nodemailer.createTransport({
  43. service: 'gmail',
  44. secure: false,
  45. port: 25,
  46. auth: {
  47. user: "arqsiteste3@gmail.com",
  48. pass: "!arqsiteste"
  49. },
  50. tls: {
  51. rejectUnauthorized: false
  52. }
  53. });
  54.  
  55. let HelperOptions = {
  56. from: "arqsiteste3@gmail.com",
  57. //to: todasPessoas[i].email,
  58. to: "arqsiteste3@gmail.com",
  59. subject: "Receitas a Expirar [ARQSI]",
  60. text: formattedString
  61. };
  62.  
  63. transporter.sendMail(HelperOptions, (error, info) => {
  64. if (error) {
  65. return console.log(error);
  66. } else {
  67. res.json(info)
  68. }
  69. });
  70. }
  71.  
  72.  
  73. });
  74. }
  75.  
  76. });
  77. }, null,
  78. true /* Start the job right now */
  79. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement