mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
561f54c1a3
Don't fail to delete a user if unsubscribing from newsletter fails GitOrigin-RevId: 8764717220d906a20a7dd0a90879a45d9dd8a7ce
69 lines
2.3 KiB
CoffeeScript
69 lines
2.3 KiB
CoffeeScript
User = require("../../models/User").User
|
|
NewsletterManager = require "../Newsletter/NewsletterManager"
|
|
ProjectDeleter = require("../Project/ProjectDeleter")
|
|
logger = require("logger-sharelatex")
|
|
SubscriptionHandler = require("../Subscription/SubscriptionHandler")
|
|
SubscriptionUpdater = require("../Subscription/SubscriptionUpdater")
|
|
UserMembershipsHandler = require("../UserMembership/UserMembershipsHandler")
|
|
async = require("async")
|
|
InstitutionsAPI = require("../Institutions/InstitutionsAPI")
|
|
Errors = require("../Errors/Errors")
|
|
{db, ObjectId} = require("../../infrastructure/mongojs")
|
|
|
|
module.exports = UserDeleter =
|
|
|
|
softDeleteUserForMigration: (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.softDeleteUsersProjectsForMigration user._id, cb
|
|
(cb) ->
|
|
user.deletedAt = new Date()
|
|
db.usersDeletedByMigration.insert user, cb
|
|
(cb) ->
|
|
user.remove cb
|
|
], callback)
|
|
|
|
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)->
|
|
UserDeleter._cleanupUser user, cb
|
|
(cb)->
|
|
ProjectDeleter.deleteUsersProjects user._id, cb
|
|
(cb)->
|
|
user.remove cb
|
|
], (err)->
|
|
if err?
|
|
logger.err err:err, user_id:user_id, "something went wrong deleteing the user"
|
|
callback err
|
|
|
|
_cleanupUser: (user, callback) ->
|
|
return callback(new Error("no user supplied")) unless user?
|
|
async.series([
|
|
(cb)->
|
|
NewsletterManager.unsubscribe user, (err) ->
|
|
logger.err("Failed to unsubscribe user from newsletter", user_id: user._id, error: err)
|
|
cb()
|
|
(cb)->
|
|
SubscriptionHandler.cancelSubscription user, cb
|
|
(cb)->
|
|
InstitutionsAPI.deleteAffiliations user._id, cb
|
|
(cb)->
|
|
SubscriptionUpdater.removeUserFromAllGroups user._id, cb
|
|
(cb)->
|
|
UserMembershipsHandler.removeUserFromAllEntities user._id, cb
|
|
], callback)
|