Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinOehlerkingCap committed Aug 31, 2023
1 parent 1f1e261 commit 4c1458c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
15 changes: 12 additions & 3 deletions src/services/sync/repo/user.repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,19 @@ const findByPreviousExternalIdAndSchool = async (previousExternalId, schoolId) =
const findByLdapDnsAndSchool = async (ldapDns, schoolId) =>
userModel
.find({
schoolId,
ldapDn: { $in: ldapDns },
$or: [
{
schoolId,
ldapDn: { $in: ldapDns },
},
{
schoolId,
previousExternalId: { $in: ldapDns },
'schoolId.features': '???',
},
],
})
.populate('roles')
.populate('roles', 'schoolId')
.lean()
.exec();

Expand Down
38 changes: 27 additions & 11 deletions src/services/sync/strategies/consumerActions/ClassAction.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const BaseConsumerAction = require('./BaseConsumerAction');
// TODO: place from where it is importat must be fixed later
// TODO: place from where it is important must be fixed later
const { LDAP_SYNC_ACTIONS } = require('../SyncMessageBuilder');
const { SchoolRepo, ClassRepo, UserRepo } = require('../../repo');
const { NotFound } = require('../../../../errors');
Expand All @@ -9,11 +9,11 @@ const defaultOptions = {
};

// TODO: in all actions it looks not nice that filterActive is not passed as option
// the additional this it not really nessasry because options pass all to this.
// the additional this it not really necessary because options pass all to this.
// but we must also keep in mind that we do not hide options behind BaseConsumerStrategie class.
// But Enable / Disable filter should come from top this is fine.
// If we pass filterActive to options, we must destructure options -> set defaults (that we do not must set repos if we pass this) -> put the combind keys as optiosn to super
// -> i do not like it because we put knowlege and logic handling to the constructor and every they want to add a new action must do it in the same way and implement the same.
// If we pass filterActive to options, we must destructure options -> set defaults (that we do not must set repos if we pass this) -> put the combined keys as option to super
// -> i do not like it because we put knowledge and logic handling to the constructor and every they want to add a new action must do it in the same way and implement the same.
// --> sound weird...
class ClassAction extends BaseConsumerAction {
constructor(filterActive = true, options = defaultOptions) {
Expand All @@ -24,24 +24,36 @@ class ClassAction extends BaseConsumerAction {
async action(data = {}) {
const { class: classData = {} } = data;

const school = await SchoolRepo.findSchoolByLdapIdAndSystem(classData.schoolDn, classData.systemId);
let school = await SchoolRepo.findSchoolByLdapIdAndSystem(classData.schoolDn, classData.systemId);

if (!school) {
throw new NotFound(
`School for schoolDn ${classData.schoolDn} and system ${classData.systemId} couldn't be found.`,
{
schoolDn: classData.schoolDn,
systemId: classData.systemId,
}
const migratedSchool = await SchoolRepo.findSchoolByPreviousExternalIdAndSystem(
classData.schoolDn,
classData.systemId
);

if (migratedSchool) {
school = migratedSchool;
} else {
throw new NotFound(
`School for schoolDn ${classData.schoolDn} and system ${classData.systemId} couldn't be found.`,
{
schoolDn: classData.schoolDn,
systemId: classData.systemId,
}
);
}
}

if (school.inUserMigration === true) {
await UserRepo.addClassToImportUsers(school._id, classData.name, classData.uniqueMembers);
return;
}

if (school.inMaintenance) {
return;
}

// default: update classes
let classId;
const existingClass = await ClassRepo.findClassByYearAndLdapDn(school.currentYear, classData.ldapDN);
Expand All @@ -54,20 +66,24 @@ class ClassAction extends BaseConsumerAction {
const createdClass = await ClassRepo.createClass(classData, school);
classId = createdClass._id;
}

await this.addUsersToClass(school._id, classId, classData.uniqueMembers);
}

async addUsersToClass(schoolId, classId, uniqueMembers) {
const students = [];
const teachers = [];
const ldapDns = !Array.isArray(uniqueMembers) ? [uniqueMembers] : uniqueMembers;

const users = await UserRepo.findByLdapDnsAndSchool(ldapDns, schoolId);

users.forEach((user) => {
user.roles.forEach((role) => {
if (role.name === 'student') students.push(user._id);
if (role.name === 'teacher') teachers.push(user._id);
});
});

await ClassRepo.updateClassStudents(classId, students);
await ClassRepo.updateClassTeachers(classId, teachers);
}
Expand Down

0 comments on commit 4c1458c

Please sign in to comment.