mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
only set status and merge fields if required
Not strictly nessaserry but it is a bit safer also improve error reporting for change email
This commit is contained in:
parent
75521a4c86
commit
423bc9312d
1 changed files with 24 additions and 15 deletions
|
@ -16,10 +16,10 @@ module.exports =
|
||||||
|
|
||||||
subscribe: (user, callback = () ->)->
|
subscribe: (user, callback = () ->)->
|
||||||
options = buildOptions(user, true)
|
options = buildOptions(user, true)
|
||||||
logger.log options:options, user:user, email:user.email, "trying to subscribe user to the mailing list"
|
logger.log options:options, user:user, email:user.email, "subscribing user to the mailing list"
|
||||||
mailchimp.request options, (err)->
|
mailchimp.request options, (err)->
|
||||||
if err?
|
if err?
|
||||||
logger.err err:err, "error subscribing person to newsletter"
|
logger.err err:err, user:user, "error subscribing person to newsletter"
|
||||||
else
|
else
|
||||||
logger.log user:user, "finished subscribing user to the newsletter"
|
logger.log user:user, "finished subscribing user to the newsletter"
|
||||||
callback(err)
|
callback(err)
|
||||||
|
@ -29,7 +29,7 @@ module.exports =
|
||||||
options = buildOptions(user, false)
|
options = buildOptions(user, false)
|
||||||
mailchimp.request options, (err)->
|
mailchimp.request options, (err)->
|
||||||
if err?
|
if err?
|
||||||
logger.err err:err, "error unsubscribing person to newsletter"
|
logger.err err:err, user:user, "error unsubscribing person to newsletter"
|
||||||
else
|
else
|
||||||
logger.log user:user, "finished unsubscribing user to the newsletter"
|
logger.log user:user, "finished unsubscribing user to the newsletter"
|
||||||
callback(err)
|
callback(err)
|
||||||
|
@ -39,12 +39,15 @@ module.exports =
|
||||||
delete options.body.status
|
delete options.body.status
|
||||||
options.body.email_address = newEmail
|
options.body.email_address = newEmail
|
||||||
mailchimp.request options, (err)->
|
mailchimp.request options, (err)->
|
||||||
if err? and err?.message?.indexOf("merge fields were invalid")
|
if err? and err?.message?.indexOf("merge fields were invalid") != -1
|
||||||
logger.log {oldEmail, newEmail}, "unable to change email in newsletter as user has not subscribed"
|
logger.log {oldEmail, newEmail}, "unable to change email in newsletter, user has never subscribed"
|
||||||
return callback()
|
return callback()
|
||||||
# if the user has unsubscribed mailchimp will error on email address change
|
else if err? and err?.message?.indexOf("could not be validated") != -1
|
||||||
else if err? and err?.message?.indexOf("could not be validated") == -1
|
logger.log {oldEmail, newEmail},
|
||||||
logger.err err:err, "error changing email in newsletter"
|
"unable to change email in newsletter, user has previously unsubscribed or new email already exist on list"
|
||||||
|
return callback(err)
|
||||||
|
else if err?
|
||||||
|
logger.err {err, oldEmail, newEmail}, "error changing email in newsletter"
|
||||||
return callback(err)
|
return callback(err)
|
||||||
else
|
else
|
||||||
logger.log "finished changing email in the newsletter"
|
logger.log "finished changing email in the newsletter"
|
||||||
|
@ -54,18 +57,24 @@ hashEmail = (email)->
|
||||||
crypto.createHash('md5').update(email.toLowerCase()).digest("hex")
|
crypto.createHash('md5').update(email.toLowerCase()).digest("hex")
|
||||||
|
|
||||||
buildOptions = (user, is_subscribed)->
|
buildOptions = (user, is_subscribed)->
|
||||||
status = if is_subscribed then "subscribed" else "unsubscribed"
|
|
||||||
subscriber_hash = hashEmail(user.email)
|
subscriber_hash = hashEmail(user.email)
|
||||||
opts =
|
opts =
|
||||||
method: "PUT"
|
method: "PUT"
|
||||||
path: "/lists/#{Settings.mailchimp?.list_id}/members/#{subscriber_hash}"
|
path: "/lists/#{Settings.mailchimp?.list_id}/members/#{subscriber_hash}"
|
||||||
body:
|
body:
|
||||||
status_if_new: status
|
|
||||||
status: status
|
|
||||||
email_address:user.email
|
email_address:user.email
|
||||||
merge_fields:
|
|
||||||
|
status = if is_subscribed then "subscribed" else "unsubscribed"
|
||||||
|
if is_subscribed?
|
||||||
|
opts.body.status = status
|
||||||
|
opts.body.status_if_new = status
|
||||||
|
|
||||||
|
if user._id?
|
||||||
|
opts.body.merge_fields =
|
||||||
FNAME: user.first_name
|
FNAME: user.first_name
|
||||||
LNAME: user.last_name
|
LNAME: user.last_name
|
||||||
MONGO_ID:user._id
|
MONGO_ID:user._id
|
||||||
|
|
||||||
|
console.log opts
|
||||||
return opts
|
return opts
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue