From a1af305115808f44ca95556a54666aba4dfff0fb Mon Sep 17 00:00:00 2001 From: Malte Berg Date: Mon, 16 Oct 2023 16:16:09 +0200 Subject: [PATCH] add group removel --- src/services/user-group/hooks/courses.js | 30 ++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/services/user-group/hooks/courses.js b/src/services/user-group/hooks/courses.js index 41e2014feb0..92a5b987a7c 100644 --- a/src/services/user-group/hooks/courses.js +++ b/src/services/user-group/hooks/courses.js @@ -67,16 +67,42 @@ const addWholeClassToCourse = async (hook) => { * @param hook - contains and request body */ const deleteWholeClassFromCourse = (hook) => { + const { app } = hook; const requestBody = hook.data; const courseId = hook.id; - if (requestBody.classIds === undefined && requestBody.user === undefined) { + if (requestBody.classIds === undefined && requestBody.user === undefined && requestBody.groupIds === undefined) { return hook; } return CourseModel.findById(courseId) .exec() - .then((course) => { + .then(async (course) => { if (!course) return hook; + const removedGroups = _.differenceBy(course.groupIds, requestBody.groupIds, (v) => JSON.stringify(v)); + if (Configuration.get('FEATURE_GROUPS_IN_COURSE_ENABLED') && removedGroups.length > 0) { + await Promise.all( + removedGroups.map((groupId) => + app + .service('nest-group-service') + .findById(groupId) + .then((group) => group.users) + ) + ).then(async (groupUsers) => { + // flatten deep arrays and remove duplicates + const userIds = _.flattenDeep(groupUsers).map((groupUser) => groupUser.userId); + const uniqueUserIds = _.uniqWith(userIds, (a, b) => a === b); + + await CourseModel.update( + { _id: course._id }, + { $pull: { userIds: { $in: uniqueUserIds } } }, + { multi: true } + ).exec(); + hook.data.userIds = hook.data.userIds.filter((value) => !uniqueUserIds.some((id) => equalIds(id, value))); + + return undefined; + }); + } + const removedClasses = _.differenceBy(course.classIds, requestBody.classIds, (v) => JSON.stringify(v)); if (removedClasses.length < 1) return hook; return Promise.all(