2014-01-03 13:32:13 -05:00
|
|
|
mongoose = require('mongoose')
|
2014-01-22 17:57:41 -05:00
|
|
|
jsonschema = require('./user_schema')
|
2014-01-03 13:32:13 -05:00
|
|
|
crypto = require('crypto')
|
2014-01-03 17:30:39 -05:00
|
|
|
{salt, isProduction} = require('../../server_config')
|
2014-01-24 14:47:14 -05:00
|
|
|
mail = require '../commons/mail'
|
2014-03-11 22:17:58 -04:00
|
|
|
log = require 'winston'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-01-17 12:53:17 -05:00
|
|
|
sendwithus = require '../sendwithus'
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
UserSchema = new mongoose.Schema({
|
|
|
|
dateCreated:
|
|
|
|
type: Date
|
|
|
|
'default': Date.now
|
|
|
|
}, {strict: false})
|
|
|
|
|
|
|
|
UserSchema.pre('init', (next) ->
|
|
|
|
return next() unless jsonschema.properties?
|
|
|
|
for prop, sch of jsonschema.properties
|
|
|
|
@set(prop, sch.default) if sch.default?
|
2014-03-16 12:26:29 -04:00
|
|
|
@set('permissions', ['admin']) if not isProduction
|
2014-01-03 13:32:13 -05:00
|
|
|
next()
|
|
|
|
)
|
|
|
|
|
|
|
|
UserSchema.post('init', ->
|
|
|
|
@set('anonymous', false) if @get('email')
|
|
|
|
@currentSubscriptions = JSON.stringify(@get('emailSubscriptions'))
|
|
|
|
)
|
|
|
|
|
|
|
|
UserSchema.methods.isAdmin = ->
|
|
|
|
p = @get('permissions')
|
|
|
|
return p and 'admin' in p
|
2014-03-11 22:17:58 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
UserSchema.statics.updateMailChimp = (doc, callback) ->
|
2014-01-03 17:30:39 -05:00
|
|
|
return callback?() unless isProduction
|
2014-01-03 13:32:13 -05:00
|
|
|
return callback?() if doc.updatedMailChimp
|
|
|
|
return callback?() unless doc.get('email')
|
|
|
|
existingProps = doc.get('mailChimp')
|
|
|
|
emailChanged = (not existingProps) or existingProps?.email isnt doc.get('email')
|
|
|
|
emailSubs = doc.get('emailSubscriptions')
|
2014-01-24 14:47:14 -05:00
|
|
|
gm = mail.MAILCHIMP_GROUP_MAP
|
|
|
|
newGroups = (gm[name] for name in emailSubs when gm[name]?)
|
2014-01-03 13:32:13 -05:00
|
|
|
if (not existingProps) and newGroups.length is 0
|
|
|
|
return callback?() # don't add totally unsubscribed people to the list
|
|
|
|
subsChanged = doc.currentSubscriptions isnt JSON.stringify(emailSubs)
|
|
|
|
return callback?() unless emailChanged or subsChanged
|
2014-03-11 22:17:58 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
params = {}
|
2014-01-24 14:47:14 -05:00
|
|
|
params.id = mail.MAILCHIMP_LIST_ID
|
2014-01-03 13:32:13 -05:00
|
|
|
params.email = if existingProps then {leid:existingProps.leid} else {email:doc.get('email')}
|
2014-01-24 14:47:14 -05:00
|
|
|
params.merge_vars = { groupings: [ {id: mail.MAILCHIMP_GROUP_ID, groups: newGroups} ] }
|
2014-01-03 13:32:13 -05:00
|
|
|
params.update_existing = true
|
2014-01-23 12:28:21 -05:00
|
|
|
params.double_optin = false
|
2014-03-11 22:17:58 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onSuccess = (data) ->
|
|
|
|
doc.set('mailChimp', data)
|
|
|
|
doc.updatedMailChimp = true
|
|
|
|
doc.save()
|
|
|
|
callback?()
|
2014-03-11 22:17:58 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onFailure = (error) ->
|
2014-03-11 22:17:58 -04:00
|
|
|
log.error 'failed to subscribe', error, callback?
|
2014-01-03 13:32:13 -05:00
|
|
|
doc.updatedMailChimp = true
|
|
|
|
callback?()
|
2014-03-11 22:17:58 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
mc.lists.subscribe params, onSuccess, onFailure
|
|
|
|
|
|
|
|
|
|
|
|
UserSchema.pre('save', (next) ->
|
|
|
|
@set('emailLower', @get('email')?.toLowerCase())
|
|
|
|
@set('nameLower', @get('name')?.toLowerCase())
|
|
|
|
pwd = @get('password')
|
|
|
|
if @get('password')
|
|
|
|
@set('passwordHash', User.hashPassword(pwd))
|
|
|
|
@set('password', undefined)
|
2014-01-17 12:53:17 -05:00
|
|
|
if @get('email') and @get('anonymous')
|
|
|
|
@set('anonymous', false)
|
|
|
|
data =
|
|
|
|
email_id: sendwithus.templates.welcome_email
|
|
|
|
recipient:
|
2014-03-11 22:17:58 -04:00
|
|
|
address: @get 'email'
|
2014-01-17 12:53:17 -05:00
|
|
|
sendwithus.api.send data, (err, result) ->
|
2014-03-11 22:17:58 -04:00
|
|
|
log.error 'error', err, 'result', result if err
|
2014-01-03 13:32:13 -05:00
|
|
|
next()
|
|
|
|
)
|
|
|
|
|
|
|
|
UserSchema.post 'save', (doc) ->
|
|
|
|
UserSchema.statics.updateMailChimp(doc)
|
|
|
|
|
|
|
|
UserSchema.statics.hashPassword = (password) ->
|
|
|
|
password = password.toLowerCase()
|
|
|
|
shasum = crypto.createHash('sha512')
|
|
|
|
shasum.update(salt + password)
|
|
|
|
shasum.digest('hex')
|
|
|
|
|
2014-03-11 22:17:58 -04:00
|
|
|
module.exports = User = mongoose.model('User', UserSchema)
|