Skip to content

Commit

Permalink
fix(mailcheck): #170
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Mar 20, 2019
1 parent 23d80c4 commit 10c36fa
Showing 1 changed file with 76 additions and 72 deletions.
148 changes: 76 additions & 72 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,96 +522,100 @@ userSchema.statics.createUserFromEmail = function (email, callback) {
if (_.isUndefined(email)) {
return callback('Invalid User Data - UserSchema.CreatePublicUser()', null)
}
var settingSchema = require('./setting')
settingSchema.getSetting('role:user:default', function (err, userRoleDefault) {
if (err || !userRoleDefault) return callback('Invalid Setting - UserRoleDefault')

var self = this
var Chance = require('chance')

var chance = new Chance()
var self = this
var Chance = require('chance')

var plainTextPass = chance.string({
length: 6,
pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
})
var chance = new Chance()

var user = new this({
username: email,
email: email,
password: plainTextPass,
fullname: email,
role: 'user'
})
var plainTextPass = chance.string({
length: 6,
pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
})

self.model(COLLECTION).find({ username: user.username }, function (err, items) {
if (err) return callback(err)
if (_.size(items) > 0) return callback('Username already exists')
var user = new this({
username: email,
email: email,
password: plainTextPass,
fullname: email,
role: userRoleDefault.value
})

user.save(function (err, savedUser) {
self.model(COLLECTION).find({ username: user.username }, function (err, items) {
if (err) return callback(err)
if (_.size(items) > 0) return callback('Username already exists')

// Create a group for this user
var GroupSchema = require('./group')
var group = new GroupSchema({
name: savedUser.email,
members: [savedUser._id],
sendMailTo: [savedUser._id],
public: true
})

group.save(function (err, group) {
user.save(function (err, savedUser) {
if (err) return callback(err)

// Send welcome email
var path = require('path')
var mailer = require('../mailer')
var Email = require('email-templates')
var templateDir = path.resolve(__dirname, '..', 'mailer', 'templates')

var email = new Email({
views: {
root: templateDir,
options: {
extension: 'handlebars'
}
}
// Create a group for this user
var GroupSchema = require('./group')
var group = new GroupSchema({
name: savedUser.email,
members: [savedUser._id],
sendMailTo: [savedUser._id],
public: true
})

var settingSchema = require('./setting')
settingSchema.getSetting('gen:siteurl', function (err, setting) {
group.save(function (err, group) {
if (err) return callback(err)

if (!setting) {
setting = { value: '' }
}

var dataObject = {
user: savedUser,
plainTextPassword: plainTextPass,
baseUrl: setting.value
}

email
.render('public-account-created', dataObject)
.then(function (html) {
var mailOptions = {
to: savedUser.email,
subject: 'Welcome to trudesk! - Here are your account details.',
html: html,
generateTextFromHTML: true
// Send welcome email
var path = require('path')
var mailer = require('../mailer')
var Email = require('email-templates')
var templateDir = path.resolve(__dirname, '..', 'mailer', 'templates')

var email = new Email({
views: {
root: templateDir,
options: {
extension: 'handlebars'
}
}
})

var settingSchema = require('./setting')
settingSchema.getSetting('gen:siteurl', function (err, setting) {
if (err) return callback(err)

if (!setting) {
setting = { value: '' }
}

mailer.sendMail(mailOptions, function (err) {
if (err) {
winston.warn(err)
return callback(err)
var dataObject = {
user: savedUser,
plainTextPassword: plainTextPass,
baseUrl: setting.value
}

email
.render('public-account-created', dataObject)
.then(function (html) {
var mailOptions = {
to: savedUser.email,
subject: 'Welcome to trudesk! - Here are your account details.',
html: html,
generateTextFromHTML: true
}

return callback(null, { user: savedUser, group: group })
mailer.sendMail(mailOptions, function (err) {
if (err) {
winston.warn(err)
return callback(err)
}

return callback(null, { user: savedUser, group: group })
})
})
.catch(function (err) {
winston.warn(err)
return callback(err)
})
})
.catch(function (err) {
winston.warn(err)
return callback(err)
})
})
})
})
})
Expand Down

0 comments on commit 10c36fa

Please sign in to comment.