diff --git a/app/models/user.server.model.js b/app/models/user.server.model.js index 51fa4f0f..b2da78df 100755 --- a/app/models/user.server.model.js +++ b/app/models/user.server.model.js @@ -9,7 +9,13 @@ var mongoose = require('mongoose'), config = require('../../config/config'), fs = require('fs-extra'), mUtilities = require('mongoose-utilities'), - path = require('path'); + path = require('path'), + querystring = require('querystring'), + config = require('../../config/config'), + nodemailer = require('nodemailer'); + +var smtpTransport = nodemailer.createTransport(config.mailer.options); + /** * A Validation function for local strategy properties @@ -126,8 +132,29 @@ UserSchema.plugin(mUtilities.timestamp, { useVirtual: false }); -//Create folder for user's pdfs + UserSchema.pre('save', function (next) { + + //Change username if it is still the user's email + if(this.username === this.email){ + var emailUsername = this.email.split('@')[0]; + this.username = querystring.stringify({ query: emailUsername }); + + var mailOptions = { + from: '"TellForm Support" ', // sender address + to: this.email, // list of receivers + subject: 'Your TellForm Username has Changed', // Subject line + text: 'Due to upgrades, your TellForm username has change from ' + this.email + ' to ' + this.username + '. Please use your new username to login.\n Using your old username will not work.\n We apologize for the inconvenience,\n - the TellForm team', // plaintext body + }; + transporter.sendMail(mailOptions, function(error, info){ + if(error){ + return console.error(error); + } + console.log('Username change message sent: ' + info.response); + }); + } + + //Create folder for user's pdfs if(process.env.NODE_ENV === 'local-development'){ var newDestination = path.join(config.pdfUploadPath, this.username.replace(/ /g,'')), stat = null;