Skip to content

Commit

Permalink
fix(profile): removing user from all groups on profile save
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Apr 5, 2019
1 parent 643703e commit 1e9accd
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions src/controllers/api/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1e9accd

Please sign in to comment.