Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N21-2287 Fix partial course sync start for admins #5343

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions apps/server/src/modules/group/domain/group.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { RoleReference, UserDO } from '@shared/domain/domainobject';
import { groupFactory, roleFactory, userDoFactory } from '@shared/testing';

import { ObjectId } from '@mikro-orm/mongodb';
import { Group } from './group';
import { GroupUser } from './group-user';

describe('Group (Domain Object)', () => {
describe(Group.name, () => {
describe('removeUser', () => {
describe('when the user is in the group', () => {
const setup = () => {
Expand Down Expand Up @@ -193,4 +192,117 @@ describe('Group (Domain Object)', () => {
});
});
});

describe('removeUser', () => {
MarvinOehlerkingCap marked this conversation as resolved.
Show resolved Hide resolved
describe('when the user is a member of the group', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const roleId = new ObjectId().toHexString();
const groupUser = new GroupUser({
userId,
roleId,
});
const group = groupFactory.build({
users: [groupUser],
});

return {
group,
userId,
};
};

it('should return true', () => {
const { group, userId } = setup();

const result = group.isMember(userId);

expect(result).toEqual(true);
});
});

describe('when the user is a member of the group and has the requested role', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const roleId = new ObjectId().toHexString();
const groupUser = new GroupUser({
userId,
roleId,
});
const group = groupFactory.build({
users: [groupUser],
});

return {
group,
userId,
roleId,
};
};

it('should return true', () => {
const { group, userId, roleId } = setup();

const result = group.isMember(userId, roleId);

expect(result).toEqual(true);
});
});

describe('when the user is a member of the group, but has a different role', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const roleId = new ObjectId().toHexString();
const groupUser = new GroupUser({
userId,
roleId: new ObjectId().toHexString(),
});
const group = groupFactory.build({
users: [groupUser],
});

return {
group,
userId,
roleId,
};
};

it('should return false', () => {
const { group, userId, roleId } = setup();

const result = group.isMember(userId, roleId);

expect(result).toEqual(false);
});
});

describe('when the user is not a member of the group', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const roleId = new ObjectId().toHexString();
const groupUser = new GroupUser({
userId: new ObjectId().toHexString(),
roleId: new ObjectId().toHexString(),
});
const group = groupFactory.build({
users: [groupUser],
});

return {
group,
userId,
roleId,
};
};

it('should return false', () => {
const { group, userId } = setup();

const result = group.isMember(userId);

expect(result).toEqual(false);
});
});
});
});
14 changes: 11 additions & 3 deletions apps/server/src/modules/group/domain/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,25 @@ export class Group extends DomainObject<GroupProps> {
return this.props.validPeriod;
}

removeUser(user: UserDO): void {
public removeUser(user: UserDO): void {
this.props.users = this.props.users.filter((groupUser: GroupUser): boolean => groupUser.userId !== user.id);
}

isEmpty(): boolean {
public isEmpty(): boolean {
return this.props.users.length === 0;
}

addUser(user: GroupUser): void {
public addUser(user: GroupUser): void {
if (!this.users.find((u: GroupUser): boolean => u.userId === user.userId)) {
this.users.push(user);
}
}

public isMember(userId: EntityId, roleId?: EntityId): boolean {
const isMember: boolean = this.users.some(
(groupUser: GroupUser) => groupUser.userId === userId && (roleId ? groupUser.roleId === roleId : true)
);

return isMember;
}
}
53 changes: 53 additions & 0 deletions apps/server/src/modules/learnroom/domain/do/course.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { courseFactory } from '../../testing';
import { Course } from './course';

describe(Course.name, () => {
describe('isTeacher', () => {
describe('when the user is a teacher in the course', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const course = courseFactory.build({
teacherIds: [userId],
});

return {
course,
userId,
};
};

it('should return true', () => {
const { course, userId } = setup();

const result = course.isTeacher(userId);

expect(result).toEqual(true);
});
});

describe('when the user is not a teacher in the course', () => {
const setup = () => {
const userId = new ObjectId().toHexString();
const course = courseFactory.build({
teacherIds: [new ObjectId().toHexString()],
studentIds: [userId],
substitutionTeacherIds: [userId],
});

return {
course,
userId,
};
};

it('should return false', () => {
const { course, userId } = setup();

const result = course.isTeacher(userId);

expect(result).toEqual(false);
});
});
});
});
6 changes: 6 additions & 0 deletions apps/server/src/modules/learnroom/domain/do/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@ export class Course extends DomainObject<CourseProps> {
get excludeFromSync(): SyncAttribute[] | undefined {
return this.props.excludeFromSync;
}

public isTeacher(userId: EntityId): boolean {
const isMember: boolean = this.teachers.some((teacherId: EntityId) => teacherId === userId);
MarvinOehlerkingCap marked this conversation as resolved.
Show resolved Hide resolved

return isMember;
}
}
Loading
Loading