mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-14 20:40:17 -05:00
6232854ff7
Remove User Memberships on Deletion GitOrigin-RevId: d6e6a4cc01e31288f660c2fd36a0cee174ee5dd1
32 lines
1.4 KiB
CoffeeScript
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
|