From f2ebec71aa35b5b95b1da457f548247f48955018 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Tue, 14 Sep 2021 11:19:44 -0700 Subject: [PATCH] sending emails when new post to profiles --- dbTools/profile.js | 11 ++++++++++ mongoDB.js | 5 +++++ notifications.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 16 +++++++++++++- package.json | 3 ++- routes/post.js | 7 +++++- 6 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 notifications.js diff --git a/dbTools/profile.js b/dbTools/profile.js index f3a36c7..002abeb 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -23,6 +23,17 @@ userDB = (DB) => { return r; } + DB.getProfileCache = async (profileId) => { + if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; + const _id = DB.ObjectID(profileId); + let r = await DB.profileCols.findOne({ _id }).catch((err) => { + console.log(err); + return false; + }); + if (r) userProfileCache[profileId] = r; + return r; + } + DB.getProfiles = async (query) => { let r = await DB.profileCols.find({isGroup: false}) .sort({ lastUpdate: -1 }).limit(20) diff --git a/mongoDB.js b/mongoDB.js index b97b739..4c1ccd3 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -34,6 +34,11 @@ const getDB = new Promise((resolve, reject) => { return DB.usersCol.findOne({ username: username }); } + DB.getUserById = (userid)=>{ + const _id = new mongo.ObjectID(userid); + return DB.usersCol.findOne({ _id }); + } + DB.newUser = (userInformation)=>{ return DB.usersCol.insertOne(userInformation).catch((err)=>{ console.log(err); diff --git a/notifications.js b/notifications.js new file mode 100644 index 0000000..cbd6d59 --- /dev/null +++ b/notifications.js @@ -0,0 +1,53 @@ +const nodemailer = require("nodemailer"); +const DBGetter = require("./mongoDB.js"); + +const Notifications = { + async sendEmail(to, subject, html) { + // create reusable transporter object using the default SMTP transport + let transporter = nodemailer.createTransport({ + host: "mail.emmint.com", + port: 465, + secure: true, // true for 465, false for other ports + auth: { + user: "noreply@emmint.com", // generated ethereal user + pass: process.env.EMAILPASS, // generated ethereal password + }, + }); + + // send mail with defined transport object + let info = await transporter.sendMail({ + from: '"EMI Social" Hello ${profile.profile.firstName},

+ +

You got a new post:

+ +
+

${Message}

+
+
— ${senderProfile.profile.firstName} ${senderProfile.profile.lastName}
+ +

Check it on the site

+ +

Blessings

+ +`; + this.sendEmail(user.username, subject, message) + } +} + +module.exports = Notifications \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 78f5a82..917f07b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,8 @@ "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", - "mongodb": "^3.6.3" + "mongodb": "^3.6.3", + "nodemailer": "^6.6.3" } }, "node_modules/@mapbox/node-pre-gyp": { @@ -775,6 +776,14 @@ "node": "4.x || >=6.0.0" } }, + "node_modules/nodemailer": { + "version": "6.6.3", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.3.tgz", + "integrity": "sha512-faZFufgTMrphYoDjvyVpbpJcYzwyFnbAMmQtj1lVBYAUSm3SOy2fIdd9+Mr4UxPosBa0JRw9bJoIwQn+nswiew==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/nopt": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", @@ -1734,6 +1743,11 @@ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" }, + "nodemailer": { + "version": "6.6.3", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.3.tgz", + "integrity": "sha512-faZFufgTMrphYoDjvyVpbpJcYzwyFnbAMmQtj1lVBYAUSm3SOy2fIdd9+Mr4UxPosBa0JRw9bJoIwQn+nswiew==" + }, "nopt": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", diff --git a/package.json b/package.json index f9e2630..70a8b70 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", - "mongodb": "^3.6.3" + "mongodb": "^3.6.3", + "nodemailer": "^6.6.3" } } diff --git a/routes/post.js b/routes/post.js index 848c8dc..56679c9 100644 --- a/routes/post.js +++ b/routes/post.js @@ -3,6 +3,7 @@ var router = express.Router() const DB = require("./../mongoDB.js"); const Post = require("./../def/post.js"); +const Notifications = require("./../notifications.js") DB.getDB.then((DB)=>{ @@ -29,9 +30,13 @@ DB.getDB.then((DB)=>{ } post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined; let postObj = new Post(post); - let dbr = await DB.newPost(postObj) + let dbr = await DB.newPost(postObj); post = postObj.toObj(); post._id = dbr.insertedId; + if(post.toProfile && post.toProfile != post.profileid){ + //send email notification + Notifications.youGotANewPost(post.toProfile, post.profileid, post.content) + } return res.json({ status: "ok", ...post