2014-02-12 05:23:40 -05:00
|
|
|
User = require("../../models/User").User
|
2014-04-04 10:47:27 -04:00
|
|
|
NewsletterManager = require "../Newsletter/NewsletterManager"
|
2014-02-12 05:23:40 -05:00
|
|
|
ProjectDeleter = require("../Project/ProjectDeleter")
|
|
|
|
logger = require("logger-sharelatex")
|
2014-03-03 11:22:55 -05:00
|
|
|
SubscriptionHandler = require("../Subscription/SubscriptionHandler")
|
|
|
|
async = require("async")
|
2019-03-26 09:43:59 -04:00
|
|
|
InstitutionsAPI = require("../Institutions/InstitutionsAPI")
|
|
|
|
Errors = require("../Errors/Errors")
|
|
|
|
{db, ObjectId} = require("../../infrastructure/mongojs")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2019-03-26 09:43:59 -04:00
|
|
|
module.exports = UserDeleter =
|
|
|
|
|
|
|
|
softDeleteUser: (user_id, callback = (err)->)->
|
|
|
|
if !user_id?
|
|
|
|
logger.err "user_id is null when trying to delete user"
|
|
|
|
return callback(new Error("no user_id"))
|
|
|
|
User.findById user_id, (err, user)->
|
|
|
|
return callback(err) if err?
|
|
|
|
return callback(new Errors.NotFoundError("user not found")) unless user?
|
|
|
|
async.series([
|
|
|
|
(cb) ->
|
|
|
|
UserDeleter._cleanupUser user, cb
|
|
|
|
(cb) ->
|
|
|
|
ProjectDeleter.softDeleteUsersProjects user._id, cb
|
|
|
|
(cb) ->
|
|
|
|
user.deletedAt = new Date()
|
|
|
|
db.deletedUsers.insert user, cb
|
|
|
|
(cb) ->
|
|
|
|
user.remove cb
|
|
|
|
], callback)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
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)
|
2014-03-03 11:22:55 -05:00
|
|
|
logger.log user:user, "deleting user"
|
|
|
|
async.series [
|
|
|
|
(cb)->
|
2019-03-26 09:43:59 -04:00
|
|
|
UserDeleter._cleanupUser user, cb
|
2014-03-03 11:22:55 -05:00
|
|
|
(cb)->
|
|
|
|
ProjectDeleter.deleteUsersProjects user._id, cb
|
|
|
|
(cb)->
|
|
|
|
user.remove cb
|
|
|
|
], (err)->
|
2014-02-12 05:23:40 -05:00
|
|
|
if err?
|
2014-03-03 11:22:55 -05:00
|
|
|
logger.err err:err, user_id:user_id, "something went wrong deleteing the user"
|
|
|
|
callback err
|
2019-03-26 09:43:59 -04:00
|
|
|
|
|
|
|
_cleanupUser: (user, callback) ->
|
|
|
|
return callback(new Error("no user supplied")) unless user?
|
|
|
|
async.series([
|
|
|
|
(cb)->
|
|
|
|
NewsletterManager.unsubscribe user, cb
|
|
|
|
(cb)->
|
|
|
|
SubscriptionHandler.cancelSubscription user, cb
|
|
|
|
(cb)->
|
|
|
|
InstitutionsAPI.deleteAffiliations user._id, cb
|
|
|
|
], callback)
|