const nodemailer = require("nodemailer"); const DBGetter = require("./mongoDB.js"); const Notifications = { async sendEmail(to, subject, html) { let transporter = nodemailer.createTransport({ host: "mail.emmint.com", port: 465, secure: true, auth: { user: "noreply@emmint.com", pass: process.env.EMAILPASS, }, }); let info = await transporter.sendMail({ from: '"EMI Social" { return DB.getProfileCache(profileid); }); let subscribed = await Promise.all(subscribedPromise); let usersPromise = subscribed.map((profile)=>{ return DB.getUsernameByIdCache(profile.userid); }); let users = await Promise.all(usersPromise); const senderProfile = await DB.getProfileCache(whoPostedId); const postPprofile = await DB.getProfileCache(post.profileid); users.forEach((user, index)=>{ let profile = subscribed[index]; if(profile._id == senderProfile._id) return 0; let subject = senderProfile.profile.firstName + " commented on the post you follow"; let html = `

Hello ${profile.profile.firstName},

One of the post you bookmarked has a new comment:

${post.content}

— ${postPprofile.profile.firstName} ${postPprofile.profile.lastName}

Comment:

${message}

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

Check it on the site

Blessings

`; this.sendEmail(user, subject, html) }) }, async youGotANewPostComment(postId, whoPostedId, message){ const DB = await DBGetter.getDB; const post = await DB.getPost(postId) if(post.bookmarks){ //send notification to boorkmaked profiles this.yourBookmarkedPostGotAComment(post, whoPostedId, message) } const profile = await DB.getProfileCache(post.profileid); if(profile.isCourse) return 0; //Course owners do not need to receive notifs const user = await DB.getUserById(profile.userid); const senderProfile = await DB.getProfileCache(whoPostedId); let subject = senderProfile.profile.firstName + " comment on your post"; let html = `

Hello ${profile.profile.firstName},

You got a comment on your post:

${post.content}

— You

Comment:

${message}

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

Check it on the site

Blessings

`; this.sendEmail(user.username, subject, html) }, async yourGroupGotANewPost(groupProfile, whoPostedId, message){ const DB = await DBGetter.getDB; let subscribedPromise = Object.keys(groupProfile.subscribed).map((profileid)=>{ return DB.getProfileCache(profileid); }); let subscribed = await Promise.all(subscribedPromise); let usersPromise = subscribed.map((profile)=>{ return DB.getUsernameByIdCache(profile.userid); }); let users = await Promise.all(usersPromise); const senderProfile = await DB.getProfileCache(whoPostedId); users.forEach((user, index)=>{ let profile = subscribed[index]; if(profile._id == senderProfile._id) return 0; let subject = senderProfile.profile.firstName + " posted on one of the groups you follow"; let html = `

Hello ${profile.profile.firstName},

${groupProfile.profile.firstName} ${groupProfile.profile.lastName} have new post:

${message}

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

Check it on the site

Blessings

`; this.sendEmail(user, subject, html) }) }, async youGotANewPost(toProfileId, whoPostedId, message){ const DB = await DBGetter.getDB; const profile = await DB.getProfileCache(toProfileId); if(profile.isGroup){ return this.yourGroupGotANewPost(profile, whoPostedId, message); } const user = await DB.getUserById(profile.userid); const senderProfile = await DB.getProfileCache(whoPostedId); let subject = senderProfile.profile.firstName + " post on your profile"; let html = `

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, html) } } module.exports = Notifications