overleaf/services/web/app/coffee/Features/UserMembership/UserMembershipsHandler.coffee
Timothée Alby 6232854ff7 Remove User Memberships on Deletion (#1688)
Remove User Memberships on Deletion

GitOrigin-RevId: d6e6a4cc01e31288f660c2fd36a0cee174ee5dd1
2019-04-23 14:34:40 +00:00

32 lines
1.4 KiB
CoffeeScript

async = require("async")
EntityModels =
Institution: require('../../models/Institution').Institution
Subscription: require('../../models/Subscription').Subscription
Publisher: require('../../models/Publisher').Publisher
UserMembershipEntityConfigs = require "./UserMembershipEntityConfigs"
module.exports = UserMembershipsHandler =
removeUserFromAllEntities: (userId, callback = (error) ->) ->
# get all writable entity types
entityConfigs = []
for key, entityConfig of UserMembershipEntityConfigs
entityConfigs.push(entityConfig) if entityConfig.fields.write?
# remove the user from all entities types
async.map entityConfigs, ((entityConfig, innerCallback) ->
UserMembershipsHandler.removeUserFromEntities entityConfig, userId, innerCallback
), callback
removeUserFromEntities: (entityConfig, userId, callback = (error) ->) ->
removeOperation = "$pull": {}
removeOperation["$pull"][entityConfig.fields.write] = userId
EntityModels[entityConfig.modelName].updateMany {}, removeOperation, callback
getEntitiesByUser: (entityConfig, userId, callback = (error, entities) ->) ->
query = Object.assign({}, entityConfig.baseQuery)
query[entityConfig.fields.access] = userId
EntityModels[entityConfig.modelName].find query, (error, entities = []) ->
return callback(error) if error?
async.mapSeries entities,
(entity, cb) -> entity.fetchV1Data(cb),
callback