overleaf/services/web/app/coffee/Features/User/UserDeleter.coffee

34 lines
1 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
User = require("../../models/User").User
NewsletterManager = require "../Newsletter/NewsletterManager"
2014-02-12 05:23:40 -05:00
ProjectDeleter = require("../Project/ProjectDeleter")
logger = require("logger-sharelatex")
SubscriptionHandler = require("../Subscription/SubscriptionHandler")
async = require("async")
2018-07-10 15:49:24 -04:00
{ deleteAffiliations } = require("../Institutions/InstitutionsAPI")
2014-02-12 05:23:40 -05:00
module.exports =
deleteUser: (user_id, callback = ()->)->
if !user_id?
logger.err "user_id is null when trying to delete user"
return callback("no user_id")
User.findById user_id, (err, user)->
if err?
return callback(err)
logger.log user:user, "deleting user"
async.series [
(cb)->
NewsletterManager.unsubscribe user, cb
(cb)->
ProjectDeleter.deleteUsersProjects user._id, cb
(cb)->
2015-01-02 05:37:59 -05:00
SubscriptionHandler.cancelSubscription user, cb
2018-06-27 12:43:20 -04:00
(cb)->
deleteAffiliations user._id, cb
(cb)->
user.remove cb
], (err)->
2014-02-12 05:23:40 -05:00
if err?
logger.err err:err, user_id:user_id, "something went wrong deleteing the user"
callback err