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-2197 Course sync - Update existing course immediately after sync begin #5283

Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { Group } from '@modules/group';
import { Group, GroupUser } from '@modules/group';
import { Test, TestingModule } from '@nestjs/testing';
import { NotFoundLoggableException } from '@shared/common/loggable-exception';
import { Page } from '@shared/domain/domainobject';
Expand Down Expand Up @@ -185,43 +185,78 @@ describe(CourseDoService.name, () => {
const setup = () => {
const course: Course = courseFactory.build();
const group: Group = groupFactory.build();
const students: GroupUser[] = [{ roleId: 'student-role-id', userId: 'student-user-id' }];
const teachers: GroupUser[] = [{ roleId: 'teacher-role-id', userId: 'teacher-user-id' }];

return {
course,
group,
students,
teachers,
};
};

it('should save a course with a synchronized group', async () => {
const { course, group } = setup();
it('should save a course with synchronized group, students, and teachers', async () => {
const { course, group, students, teachers } = setup();

await service.startSynchronization(course, group);
await service.startSynchronization(course, group, students, teachers);

expect(courseRepo.save).toHaveBeenCalledWith(
new Course({
...course.getProps(),
syncedWithGroup: group.id,
name: group.name,
startDate: group.validPeriod?.from,
untilDate: group.validPeriod?.until,
studentIds: students.map((student) => student.userId),
teacherIds: teachers.map((teacher) => teacher.userId),
})
);
});

it('should set an empty list of students if no teachers are present', async () => {
const { course, group, students } = setup();
const teachers: GroupUser[] = []; // No teachers

await service.startSynchronization(course, group, students, teachers);

expect(courseRepo.save).toHaveBeenCalledWith(
new Course({
...course.getProps(),
syncedWithGroup: group.id,
name: group.name,
startDate: group.validPeriod?.from,
untilDate: group.validPeriod?.until,
studentIds: [],
teacherIds: [],
})
);
});
});

describe('when a course is synchronized with a group', () => {
describe('when a course is already synchronized with a group', () => {
const setup = () => {
const course: Course = courseFactory.build({ syncedWithGroup: new ObjectId().toHexString() });
const group: Group = groupFactory.build();
const students: GroupUser[] = [{ roleId: 'student-role-id', userId: 'student-user-id' }];
const teachers: GroupUser[] = [{ roleId: 'teacher-role-id', userId: 'teacher-user-id' }];

return {
course,
group,
students,
teachers,
};
};
it('should throw an unprocessable entity exception', async () => {
const { course, group } = setup();

await expect(service.startSynchronization(course, group)).rejects.toThrow(
it('should throw a CourseAlreadySynchronizedLoggableException', async () => {
const { course, group, students, teachers } = setup();

await expect(service.startSynchronization(course, group, students, teachers)).rejects.toThrow(
CourseAlreadySynchronizedLoggableException
);

expect(courseRepo.save).not.toHaveBeenCalled();
});
});
});
Expand Down
19 changes: 17 additions & 2 deletions apps/server/src/modules/learnroom/service/course-do.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthorizationLoaderServiceGeneric } from '@modules/authorization';
import { type Group } from '@modules/group';
import { GroupUser, type Group } from '@modules/group';
import { Inject, Injectable } from '@nestjs/common';
import { Page } from '@shared/domain/domainobject';
import { IFindOptions } from '@shared/domain/interface';
Expand Down Expand Up @@ -45,12 +45,27 @@ export class CourseDoService implements AuthorizationLoaderServiceGeneric<Course
await this.courseRepo.save(course);
}

public async startSynchronization(course: Course, group: Group): Promise<void> {
public async startSynchronization(
course: Course,
group: Group,
students: GroupUser[],
teachers: GroupUser[]
): Promise<void> {
if (course.syncedWithGroup) {
throw new CourseAlreadySynchronizedLoggableException(course.id);
}

course.syncedWithGroup = group.id;
course.name = group.name;
course.startDate = group.validPeriod?.from;
course.untilDate = group.validPeriod?.until;

if (teachers.length >= 1) {
course.students = students.map((groupUser: GroupUser): EntityId => groupUser.userId);
course.teachers = teachers.map((groupUser: GroupUser): EntityId => groupUser.userId);
} else {
course.students = [];
}

await this.courseRepo.save(course);
}
Expand Down
36 changes: 31 additions & 5 deletions apps/server/src/modules/learnroom/uc/course-sync.uc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { AuthorizationContextBuilder, AuthorizationService } from '@modules/authorization';
import { GroupService } from '@modules/group';
import { RoleService } from '@modules/role';
import { Test, TestingModule } from '@nestjs/testing';
import { Permission } from '@shared/domain/interface';
import { groupFactory, setupEntities, userFactory } from '@shared/testing';
import { Permission, RoleName } from '@shared/domain/interface';
import { groupFactory, roleDtoFactory, setupEntities, userFactory } from '@shared/testing';
import { CourseDoService } from '../service';
import { courseFactory } from '../testing';
import { CourseSyncUc } from './course-sync.uc';
Expand All @@ -15,6 +16,7 @@ describe(CourseSyncUc.name, () => {
let authorizationService: DeepMocked<AuthorizationService>;
let courseService: DeepMocked<CourseDoService>;
let groupService: DeepMocked<GroupService>;
let roleService: DeepMocked<RoleService>;

beforeAll(async () => {
module = await Test.createTestingModule({
Expand All @@ -32,13 +34,18 @@ describe(CourseSyncUc.name, () => {
provide: GroupService,
useValue: createMock<GroupService>(),
},
{
provide: RoleService,
useValue: createMock<RoleService>(),
},
],
}).compile();

uc = module.get(CourseSyncUc);
authorizationService = module.get(AuthorizationService);
courseService = module.get(CourseDoService);
groupService = module.get(GroupService);
roleService = module.get(RoleService);
await setupEntities();
});

Expand Down Expand Up @@ -93,15 +100,30 @@ describe(CourseSyncUc.name, () => {
const user = userFactory.buildWithId();
const course = courseFactory.build();
const group = groupFactory.build();
const studentRole = roleDtoFactory.build({ id: 'student-role-id' });
const teacherRole = roleDtoFactory.build({ id: 'teacher-role-id' });

group.users = [
{ roleId: 'student-role-id', userId: 'student-user-id' },
{ roleId: 'teacher-role-id', userId: 'teacher-user-id' },
];

const students = group.users.filter((groupUser) => groupUser.roleId === studentRole.id);
const teachers = group.users.filter((groupUser) => groupUser.roleId === teacherRole.id);

courseService.findById.mockResolvedValueOnce(course);
groupService.findById.mockResolvedValueOnce(group);
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);
roleService.findByName.mockResolvedValueOnce(studentRole).mockResolvedValueOnce(teacherRole);

return {
user,
course,
group,
studentRole,
teacherRole,
students,
teachers,
};
};

Expand All @@ -119,14 +141,18 @@ describe(CourseSyncUc.name, () => {
);
});

it('should start the synchronization', async () => {
const { user, course, group } = setup();
it('should start the synchronization with correct roles', async () => {
const { user, course, group, students, teachers } = setup();

await uc.startSynchronization(user.id, course.id, group.id);

expect(courseService.findById).toHaveBeenCalledWith(course.id);
expect(groupService.findById).toHaveBeenCalledWith(group.id);

expect(courseService.startSynchronization).toHaveBeenCalledWith(course, group);
expect(roleService.findByName).toHaveBeenCalledWith(RoleName.STUDENT);
expect(roleService.findByName).toHaveBeenCalledWith(RoleName.TEACHER);

expect(courseService.startSynchronization).toHaveBeenCalledWith(course, group, students, teachers);
});
});
});
Expand Down
27 changes: 20 additions & 7 deletions apps/server/src/modules/learnroom/uc/course-sync.uc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AuthorizationContextBuilder, AuthorizationService } from '@modules/authorization';
import { Group, GroupService } from '@modules/group';
import { GroupService, GroupUser } from '@modules/group';
import { RoleService } from '@modules/role';
import { Injectable } from '@nestjs/common';

import { type User as UserEntity } from '@shared/domain/entity';
import { Permission } from '@shared/domain/interface';
import { Permission, RoleName } from '@shared/domain/interface';
import { EntityId } from '@shared/domain/types';
import { Course } from '../domain';
import { CourseDoService } from '../service';
Expand All @@ -12,7 +14,8 @@ export class CourseSyncUc {
constructor(
private readonly authorizationService: AuthorizationService,
private readonly courseService: CourseDoService,
private readonly groupService: GroupService
private readonly groupService: GroupService,
private readonly roleService: RoleService
) {}

public async stopSynchronization(userId: EntityId, courseId: EntityId): Promise<void> {
Expand All @@ -29,16 +32,26 @@ export class CourseSyncUc {
}

public async startSynchronization(userId: string, courseId: string, groupId: string) {
const course: Course = await this.courseService.findById(courseId);
const group: Group = await this.groupService.findById(groupId);
const user: UserEntity = await this.authorizationService.getUserWithPermissions(userId);
const [course, group, user] = await Promise.all([
this.courseService.findById(courseId),
this.groupService.findById(groupId),
this.authorizationService.getUserWithPermissions(userId),
]);

this.authorizationService.checkPermission(
user,
course,
AuthorizationContextBuilder.write([Permission.COURSE_EDIT])
);

await this.courseService.startSynchronization(course, group);
const [studentRole, teacherRole] = await Promise.all([
this.roleService.findByName(RoleName.STUDENT),
this.roleService.findByName(RoleName.TEACHER),
]);

const students = group.users.filter((groupUser: GroupUser) => groupUser.roleId === studentRole.id);
const teachers = group.users.filter((groupUser: GroupUser) => groupUser.roleId === teacherRole.id);

await this.courseService.startSynchronization(course, group, students, teachers);
}
}
Loading