Skip to content

Commit

Permalink
N21-2192 Configure teachers in partial sync (#5337)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinOehlerkingCap authored Nov 12, 2024
1 parent 9bb5d3f commit 3d722f1
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions src/services/user-group/hooks/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const restrictToCurrentSchool = globalHooks.ifNotLocal(globalHooks.restrictToCur
const restrictToUsersOwnCourses = globalHooks.ifNotLocal(globalHooks.restrictToUsersOwnCourses);

const { checkScopePermissions } = require('../../helpers/scopePermissions/hooks');
const { SYNC_ATTRIBUTE } = require('../model');
/**
* adds all students to a course when a class is added to the course
* @param hook - contains created/patched object and request body
Expand Down Expand Up @@ -153,33 +154,58 @@ const deleteWholeClassFromCourse = (hook) => {
});
};

const compareIdArr = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
const isEditing = (hook, attribute) => {
return attribute in hook.data;
};

const hasArrayValueChanged = (hook, course, attribute) => {
const currentArray = course[attribute];
const patchArray = hook.data[attribute];

if (currentArray.length !== patchArray.length) {
return true;
}

return currentArray.some((element) => !patchArray.includes(toStringId(element)));
};

const isValidSyncChange = (hook, course) => {
if (isEditing(hook, 'classIds') && hasArrayValueChanged(hook, course, 'classIds')) {
return false;
}
if (isEditing(hook, 'groupIds') && hasArrayValueChanged(hook, course, 'groupIds')) {
return false;
}
if (isEditing(hook, 'substitutionIds') && hasArrayValueChanged(hook, course, 'substitutionIds')) {
return false;
}
if (
!course.excludeFromSync?.includes(SYNC_ATTRIBUTE.TEACHERS) &&
isEditing(hook, 'teacherIds') &&
hasArrayValueChanged(hook, course, 'teacherIds')
) {
return false;
}

const dateFormat = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]';
const courseStartDate = course.startDate ? moment.utc(course.startDate).format(dateFormat) : undefined;
if (isEditing(hook, 'startDate') && courseStartDate !== hook.data.startDate) {
return false;
}
const courseUntilDate = course.untilDate ? moment.utc(course.untilDate).format(dateFormat) : undefined;
if (isEditing(hook, 'untilDate') && courseUntilDate !== hook.data.untilDate) {
return false;
}
return arr1.every((element) => arr2.includes(toStringId(element)));

return true;
};

const restrictChangesToSyncedCourse = async (hook) => {
const { app } = hook;
const courseId = hook.id;
const course = await app.service('courses').get(courseId);

if (course.syncedWithGroup) {
const dateFormat = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]';
const courseStartDate = course.startDate ? moment.utc(course.startDate).format(dateFormat) : undefined;
const courseUntilDate = course.untilDate ? moment.utc(course.untilDate).format(dateFormat) : undefined;

if (
compareIdArr(course.classIds, hook.data.classIds) &&
compareIdArr(course.groupIds, hook.data.groupIds) &&
compareIdArr(course.substitutionIds, hook.data.substitutionIds) &&
compareIdArr(course.teacherIds, hook.data.teacherIds) &&
courseStartDate === hook.data.startDate &&
courseUntilDate === hook.data.untilDate
) {
return hook;
}
if (course.syncedWithGroup && !isValidSyncChange(hook, course)) {
throw new Forbidden("The course doesn't match the synchronized course");
}
return hook;
Expand Down

0 comments on commit 3d722f1

Please sign in to comment.