Unlink group tags when a user leaves it

This commit is contained in:
nikosmonaut 2019-12-14 14:53:54 +01:00
parent 023173fada
commit 1c719a8dbb

View file

@ -1345,6 +1345,8 @@ schema.methods.leave = async function leaveGroup (user, keep = 'keep-all', keepC
.map(task => this.unlinkTask(task, user, keep, false));
await Promise.all(assignedTasksToRemoveUserFrom);
this.unlinkTags(user);
// the user could be modified by calls to `unlinkTask` for challenge and group tasks
// it has not been saved before to avoid multiple saves in parallel
const promises = user.isModified() ? [user.save()] : [];
@ -1407,6 +1409,15 @@ schema.methods.leave = async function leaveGroup (user, keep = 'keep-all', keepC
return Promise.all(promises);
};
schema.methods.unlinkTags = function unlinkTags (user) {
const group = this;
user.tags.forEach(tag => {
if (tag.group && tag.group === group._id) {
tag.group = undefined;
}
});
};
/**
* Updates all linked tasks for a group task
*