Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - Booking reminder Nodemailer node-cron MongoDB

I am having problem sending a booking reminder email. I use nodemailer and node-cron. It works good. I would like to read date from database and e.g. send email one week before reservation. I have no idea how to do it. My model:

  start_time: {
    type: String,
    required: true,
  },
  hour: {
    type: String,
    required: true,
  },
  courtId: {
    type: String,
    required: true,
  },
  userId: {
    type: ObjectId,
    ref: 'userModel',
    required: true,
  },
});

enter image description here

const cron = require('node-cron');
const nodemailer = require('nodemailer');
const { getMaxListeners } = require('../config/database');

const sendEmail = function () {
  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: `${process.env.EMAIL_ADDRESS}`,
      pass: `${process.env.EMAIL_PASSWORD}`,
    },
  });

  const mailOptions = {
    from: `${process.env.EMAIL_ADDRESS}`,
    to: `${''}`,
    subject: 'Link To Reset Password',
    text:
      'wiadomos?',
  };

  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    } else {
      console.log('Email sent: ' + info.response);
    }
  });
};

module.exports.task = function () {
  cron.schedule('00 * * * * *', () => {
    console.log('send');
    //sendEmail();
  });
};

I think start time parse to date? How read only this reservation which will be in a week?

question from:https://stackoverflow.com/questions/65647286/booking-reminder-nodemailer-node-cron-mongodb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

model: ***(i changed start_time to type: Date and i added email_sent)

  start_time: {
    type: Date,
    required: true,
  },
  email_sent: {
   type:Boolean,
   default:"false",
   required:true
   },
  hour: {
    type: String,
    required: true,
  },
  courtId: {
    type: String,
    required: true,
  },
  userId: {
    type: ObjectId,
    ref: 'userModel',
    required: true,
  },
});

Server:

cron.schedule('00 * * * * *', () => {
Modal.find({}).
  then(allUsers => {
   for(let user of allUsers){              
    if(Date.now() < (user.start_time - (24*60*60*1000) * 7) && user.email_sent === false){
     //sendEmail();
     // here you need to update user.email_sent: true. and save.
    }
   }
  })
});

*Also i will suggest to do, find only email_sent : false. you can check how to do it with mongoose. (then you dont need to check it in the if statement) *Model.find({ email_sent: "false" })

i just did it out of my mind, i didnt check, errors you can face:

  1. you will need to convert the start_time
  2. check the boolean email_sent how you get it (as a string or boolen) it will affect the if statement.

this is just the idea how to achieve it. i didnt test the code. Please try the code and let me know if you face any issues


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...