send email to all the users at specific time according to different time zones of the users
Using Express, Mongoose, node-schedule and nodemailer
git clone https://github.com/sthnaqvi/email-microservice
After repository is cloned, go inside of repository directory and install dependencies there:
cd email-microservice
npm install
To start the server
npm start
-
Create a new client: uncomment
clientCtrl.createClient()
function in app.js fileclientCtrl.createClient({name: "Sam", email: "sam@xyz.com", city: "Dubai", country: "UAE", password: "test123"});
-
Login API for client to get Authorization Token:
POST: http://localhost:9000/api/client/login
Request:
{ "email":"david@xyz.com", "password":"test123" }
Response:
{ "success": true, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImRhdmlkQHh5ei5jb20iLCJjbGllbnRJZCI6IjViMWM0NmRjYWRjYzMwNDg0NjU1Mzg3NiIsImlhdCI6MTUyODY1ODIyOH0.SBTyeyAKbwmppf8Q9WJ5vHWQNQNRY0P7DaxO9lYW0Pk" }
-
Create User API for client to create a new user: Request: with
Authorization
Header(JWT){ "name":"demo", "email":"demo@xyz.com", "offset":"-300", "city":"Lima", "country":"Peru" }
Response:
{ "success": true, "msg": "New user created.", "user": { "_id": "5b1d8699b05dec1cf3900aca", "name": "demo", "email": "demo@xyz.com", "offset": -300, "city": "Lima", "country": "Peru", "clientId": "5b1c46dcadcc304846553876", "__v": 0 } }
After run npm start
start script run node app.js
- in app.js file called
cron.startCron()
function. - in cron.js file
startCron()
function find total number of unique offset(time zone) in database. - after getting all existing offsets in Array[] call
scheduleJob()
for each offset. - in
scheduleJob()
- set key value of
scheduledOffset[offset] = true;
to store running offset(timezone) cron. - call
otherTimeToLocalTime(offset);
to get time hour & minute (8:00AM of particular time zone to local time). - call
schedule.scheduleJob(rule, function (){ })
(function of node-schedule package) to schedule job.- when cron execute call
findUsersAndSendEmail()
function to find users of particular time zone and send Email in batch 100 users.
- when cron execute call
- set key value of
- Database: Mongodb(create a test account on MLab)
- SMTP: GMAIL test account