Skip to content
Permalink
Browse files

fix(profile): removing user from all groups on profile save

  • Loading branch information...
polonel committed Apr 5, 2019
1 parent 643703e commit 1e9accda01b29ef215891a46bc809bde12f24f8a
Showing with 48 additions and 45 deletions.
  1. +48 −45 src/controllers/api/v1/users.js
@@ -366,7 +366,7 @@ apiUsers.update = function (req, res) {

var data = req.body
// saveGroups - Profile saving where groups are not sent
var saveGroups = data.saveGroups || true
var saveGroups = !_.isUndefined(data.saveGroups) ? data.saveGroups : true
var obj = {
fullname: data.aFullname,
title: data.aTitle,
@@ -421,54 +421,57 @@ apiUsers.update = function (req, res) {
})
},
groups: function (done) {
if (!saveGroups) return done()
var userGroups = []
groupSchema.getAllGroups(function (err, groups) {
if (err) return done(err)
async.each(
groups,
function (grp, callback) {
if (_.includes(obj.groups, grp._id.toString())) {
if (grp.isMember(obj._id)) {
userGroups.push(grp)
return callback()
}
grp.addMember(obj._id, function (err, result) {
if (err) return callback(err)

if (result) {
grp.save(function (err) {
if (err) return callback(err)
userGroups.push(grp)
return callback()
})
} else {
if (!saveGroups) {
groupSchema.getAllGroupsOfUser(obj._id, done)
} else {
var userGroups = []
groupSchema.getAllGroups(function (err, groups) {
if (err) return done(err)
async.each(
groups,
function (grp, callback) {
if (_.includes(obj.groups, grp._id.toString())) {
if (grp.isMember(obj._id)) {
userGroups.push(grp)
return callback()
}
})
} else {
// Remove Member from group
grp.removeMember(obj._id, function (err, result) {
if (err) return callback(err)
if (result) {
grp.save(function (err) {
if (err) return callback(err)

grp.addMember(obj._id, function (err, result) {
if (err) return callback(err)

if (result) {
grp.save(function (err) {
if (err) return callback(err)
userGroups.push(grp)
return callback()
})
} else {
return callback()
})
} else {
return callback()
}
})
}
},
function (err) {
if (err) return done(err)
}
})
} else {
// Remove Member from group
grp.removeMember(obj._id, function (err, result) {
if (err) return callback(err)
if (result) {
grp.save(function (err) {
if (err) return callback(err)

return callback()
})
} else {
return callback()
}
})
}
},
function (err) {
if (err) return done(err)

return done(null, userGroups)
}
)
})
return done(null, userGroups)
}
)
})
}
}
},
function (err, results) {

0 comments on commit 1e9accd

Please sign in to comment.
You can’t perform that action at this time.