Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 0066242

Browse files
authored
Merge pull request #60 from SpringRoll/bugfix/162931909_group_twice
Bugfix/162931909 group twice
2 parents 28ae6ff + 1525226 commit 0066242

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

app/models/game.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,30 @@ GameSchema.methods.hasPermission = function(token, callback) {
474474
* @param {function} callback The callback with result
475475
* @return {Promise} The promise object for async action
476476
*/
477-
GameSchema.statics.addGroup = function(ids, groupId, permission, callback) {
478-
if (!Array.isArray(ids)) ids = [ids];
479-
480-
var group = {
481-
group: groupId,
482-
permission: permission
483-
};
484-
485-
this.update(
486-
{ _id: { $in: ids } },
487-
{ $push: { groups: group } },
488-
{ multi: true },
489-
callback
490-
);
477+
GameSchema.statics.addGroup = function(ids, groupId, permission, callback)
478+
{
479+
if (!Array.isArray(ids)) ids = [ids];
480+
481+
var group = {
482+
group: groupId,
483+
permission: permission
484+
};
485+
486+
// logic implements overwrite of existing with incoming
487+
// first remove any old entries associated with the group in question
488+
this.update(
489+
{_id: {$in: ids}},
490+
{$pull: {groups: {group: {$in: [groupId]}}}}
491+
)
492+
// then add the 'new' permission level
493+
.then(()=>{
494+
this.update(
495+
{_id: {$in: ids}},
496+
{$push: {groups: group}},
497+
{multi: true},
498+
callback
499+
);
500+
});
491501
};
492502

493503
/**

0 commit comments

Comments
 (0)