Skip to content

Commit

Permalink
change scope of unique members assertion to fix current classes
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Oct 4, 2023
1 parent 66ea800 commit eca8570
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 43 deletions.
21 changes: 8 additions & 13 deletions src/services/sync/strategies/consumerActions/ClassAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,20 @@ class ClassAction extends BaseConsumerAction {
}

async addUsersToClass(schoolId, classId, uniqueMembers) {
if (!uniqueMembers || !uniqueMembers[0]) {
console.warn(
'In LDAP-Sync->ClassAction.js->addUsersToClass: uniqueMembers are not given: No users are added to class.'
);
return;
}

const students = [];
const teachers = [];
const ldapDns = !Array.isArray(uniqueMembers) ? [uniqueMembers] : uniqueMembers;

const users = await UserRepo.findByLdapDnsAndSchool(ldapDns, schoolId);
if (uniqueMembers[0]) {
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);
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
48 changes: 18 additions & 30 deletions test/services/sync/strategies/consumerActions/ClassAction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,30 +343,6 @@ describe('Class Actions', () => {
expect(updateClassTeachersStub.getCall(0).lastArg).to.eql(['user2', 'user3']);
});

it('should not add any user to the class, when uniqueMembers are undefined', async () => {
const uniqueMembers = undefined;
const schoolObj = { _id: new ObjectId(), currentYear: new ObjectId() };
const findByLdapDnsAndSchoolStub = sinon.stub(UserRepo, 'findByLdapDnsAndSchool');

await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers);

expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true;
expect(updateClassStudentsStub.notCalled).to.be.true;
expect(updateClassTeachersStub.notCalled).to.be.true;
});

it('should not add any user to the class, when uniqueMembers are null', async () => {
const uniqueMembers = null;
const schoolObj = { _id: new ObjectId(), currentYear: new ObjectId() };
const findByLdapDnsAndSchoolStub = sinon.stub(UserRepo, 'findByLdapDnsAndSchool');

await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers);

expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true;
expect(updateClassStudentsStub.notCalled).to.be.true;
expect(updateClassTeachersStub.notCalled).to.be.true;
});

it('should not add any user to the class, when uniqueMembers are []', async () => {
const uniqueMembers = [];
const schoolObj = { _id: new ObjectId(), currentYear: new ObjectId() };
Expand All @@ -375,8 +351,12 @@ describe('Class Actions', () => {
await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers);

expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true;
expect(updateClassStudentsStub.notCalled).to.be.true;
expect(updateClassTeachersStub.notCalled).to.be.true;

expect(updateClassStudentsStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString());
expect(updateClassStudentsStub.getCall(0).lastArg).to.eql([]);

expect(updateClassTeachersStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString());
expect(updateClassTeachersStub.getCall(0).lastArg).to.eql([]);
});

it('should not add any user to the class, when uniqueMembers are [undefined]', async () => {
Expand All @@ -387,8 +367,12 @@ describe('Class Actions', () => {
await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers);

expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true;
expect(updateClassStudentsStub.notCalled).to.be.true;
expect(updateClassTeachersStub.notCalled).to.be.true;

expect(updateClassStudentsStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString());
expect(updateClassStudentsStub.getCall(0).lastArg).to.eql([]);

expect(updateClassTeachersStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString());
expect(updateClassTeachersStub.getCall(0).lastArg).to.eql([]);
});

it('should not add any user to the class, when uniqueMembers are [null]', async () => {
Expand All @@ -399,8 +383,12 @@ describe('Class Actions', () => {
await classAction.addUsersToClass(schoolObj._id, mockClass._id, uniqueMembers);

expect(findByLdapDnsAndSchoolStub.notCalled).to.be.true;
expect(updateClassStudentsStub.notCalled).to.be.true;
expect(updateClassTeachersStub.notCalled).to.be.true;

expect(updateClassStudentsStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString());
expect(updateClassStudentsStub.getCall(0).lastArg).to.eql([]);

expect(updateClassTeachersStub.getCall(0).firstArg.toString()).to.be.equal(mockClass._id.toString());
expect(updateClassTeachersStub.getCall(0).lastArg).to.eql([]);
});
});
});

0 comments on commit eca8570

Please sign in to comment.